Get image name list from assets catalogue in xcode

Viewed 1467

I am new to xCode. I want to create an array of UIImage from the folder in Assets Catalogue - see screenshot ("Brands" folder).

Assets folder details

To load it in a UITableView like this:

private func loadLocalBrands() {

    // Load images names from assets and add them to array
    let names : [String] = ["Avon","Dove","Faberlic","Grand","GreenLight","Loreal",
                            "Loreal", "MaxFactor", "Nivea", "Olay", "Oriflame",
                            "Rexona","Schwarzkopf","Spaquatoria","Wella"]

        for name in names{
            brands.append(Brand.init(name: name, photo: 
            UIImage(named: name), rating: Int(arc4random_uniform(5) + 1))!)
        }
    }

and it works properly - see screenshot

simulator screenshot

But if I will add sooner more images, I will need to expand "names" array too.

So I want to get list of files that located in "Brands" folder within the Assets catalogue.

1 Answers

You are over-engineering this, its best to maintain a list of your assets that you want to display to the user. You will have to add assets to the project and repackage which is a good time to add new items to the list.

Also this is why things like this are manged from server in case you didn't think of it.

Another issue is that the asset catalogue is a product of Xcode whereas its not available when the IPA file of the app is created in the same format (in my understanding). So this approach will fail even if you were to work around and read the file list somehow.

Related