Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 函数,该函数返回xcassets中目录的字符串文件名数组_Ios_Swift_Path_Uiimage_Xcasset - Fatal编程技术网

Ios 函数,该函数返回xcassets中目录的字符串文件名数组

Ios 函数,该函数返回xcassets中目录的字符串文件名数组,ios,swift,path,uiimage,xcasset,Ios,Swift,Path,Uiimage,Xcasset,几个小时以来,我一直在阅读许多Swift和Objective-C文章,以帮助我创建一个函数,该函数返回xcassets中目录的字符串文件名数组。然后,我可以使用文件名数组创建另一个函数,向用户显示随机图像 var imageDirectory = [String]() func getContentOfImageDirectory() -> [String] { imageDirectory.removeAll(keepCapacity: false) let

几个小时以来,我一直在阅读许多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”的全部内容,而不是文件名数组

如果有人能为这个问题提供解决方案,我们将不胜感激。
干杯。

Xcode将xcassets文件夹中的文件编译成一个文件(名为“Assets.car”)


因此,您不应该使用xcassets,而应该将文件添加到“普通”文件夹中。确保在“复制捆绑资源”构建阶段复制它们。

。尝试保存图像名称数组并从数组中随机获取图像名称。但是,如果不在数组中手动键入每个图像的文件名,如何获取图像名称数组?首先,感谢您的回复。我已经按照你的建议做了,当我添加图像的“普通”文件夹时,xcode会自动将文件夹中的图像添加到“复制包资源”本身(而不是实际的文件夹本身),然后运行代码,我仍然得到“tempArray was nil”和“没有文件”。您还有其他提示吗?将文件夹添加到Xcode时,请确保将其添加为“文件夹引用”,而不是作为组。然后,文件夹应在项目导航器中显示为蓝色。它还应显示为“复制捆绑资源”步骤中的文件夹。