如何在Array(或某些数据结构?)中获取Assets.xcassets文件名?

我正在尝试使用Swift迭代我放入Assets文件夹的图像。 我想迭代它们并稍后将它们插入到.nib文件中,但到目前为止我找不到如何得到类似的东西:

let assetArray = ["image1.gif", "image2.gif", ...]

这可能吗? 我一直在玩NSBundle.mainBundle()但在这方面找不到任何东西。 请告诉我。 谢谢!

Assets.xcassets不是文件夹,而是包含使用Assets.car作为其文件名的所有图像的存档。

如果你真的想要阅读资产文件,那么你需要使用一些可以像这样提取文件内容的库。

或者您可以在项目中创建一个包并拖动您在那里的所有图像。 就我而言,我的项目中有Images.bundle。 要获取文件名,您可以执行以下操作:

 let fileManager = NSFileManager.defaultManager() let bundleURL = NSBundle.mainBundle().bundleURL let assetURL = bundleURL.URLByAppendingPathComponent("Images.bundle") let contents = try! fileManager.contentsOfDirectoryAtURL(assetURL, includingPropertiesForKeys: [NSURLNameKey, NSURLIsDirectoryKey], options: .SkipsHiddenFiles) for item in contents { print(item.lastPathComponent) } 

SWIFT 3版本:

 let fileManager = FileManager.default let bundleURL = Bundle.main.bundleURL let assetURL = bundleURL.appendingPathComponent("Images.bundle") let contents = try! fileManager.contentsOfDirectory(at: assetURL, includingPropertiesForKeys: [URLResourceKey.nameKey, URLResourceKey.isDirectoryKey], options: .skipsHiddenFiles) for item in contents { print(item.lastPathComponent) }