【发布时间】:2015-09-23 04:56:10
【问题描述】:
几个小时以来,我浏览了许多 Swift 和 Objective-C 帖子,以帮助我创建一个函数,该函数返回 xcassets 中目录的字符串文件名数组。然后我可以使用文件名数组创建另一个函数,向我的用户显示随机图像。
var imageDirectory = [String]()
func getContentOfImageDirectory() -> [String] {
imageDirectory.removeAll(keepCapacity: false)
let fm = NSFileManager.defaultManager()
let sourcePath = NSBundle.mainBundle().resourcePath!.stringByAppendingPathComponent("DeviceImagesFolder")
var tempArray = fm.contentsOfDirectoryAtPath(sourcePath, error: nil)
if tempArray != nil {
self.imageDirectory = tempArray as! [String]
return imageDirectory
}
else {
println("tempArray was nil")
if fm.fileExistsAtPath(sourcePath){
println("There is a file")
return []
}
else {
println("There is not a file")
return []
}
}
}
但是 tempArray 不断返回 nil 和“没有文件”。显然,我的函数返回“DeviceImagesFolder”的整个包含,而不是文件名数组。
如果有人能提供解决此问题的方法,将不胜感激。 干杯。
【问题讨论】:
-
stackoverflow.com/questions/19346394/…。尝试保存一个图像名称数组并从数组中随机获取图像名称。
-
但是如何在不手动将每个图像的文件名输入到数组中的情况下获得图像名称数组?
标签: ios swift path uiimage xcasset