Search code examples
swiftxcodeimagebundleassets

How to find specific images which start with a string in Swift Assets.xcassets


I am developing a swift application and need to find some images which start with a specific string, but i dont know how to find that images from Assets.xcassets.

All of the my images is in the Assets.xcassets

Thanks


Solution

  • Assets folder cannot be accessed this way. One way to do that is creating a folder with all the images on your computer, and drag into the Xcode project. Dont forget to select the "Create folder references for any added folders" option. With that referenced folder, you can access all images:

    guard let _resourcePath = Bundle.main.resourcePath else{
            return
        }
        do{
            if let url = NSURL(string: _resourcePath)?.appendingPathComponent("YOUR FOLDER NAME"){
                let resourcesContent = try FileManager().contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: .skipsHiddenFiles)
    
                for imageUrl in resourcesContent {
                    let imageName = imageUrl.lastPathComponent
                    print(imageName)
                    //CHECK IMAGE NAME STRING
                }
            }
        }catch let error{
            print(error.localizedDescription)
        }