【发布时间】:2016-11-18 21:40:27
【问题描述】:
我刚刚将一个项目从 Swift 2 转换为 Swift 3 并收到错误,但我不明白为什么代码有问题:
var imagesInProject : NSArray?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
print(paths[0])
if let urls = Bundle.main.urls(forResourcesWithExtension: "png", subdirectory: nil) {
imagesInProject = urls.filter {$0.lastPathComponent.hasPrefix("AppIcon") == false} .map {$0.lastPathComponent!}
}
return true
}
错误是:“'map' 产生 '[T]',而不是预期的上下文结果类型 'NSArray?'”
我该如何解决这个问题?我对 .map 很熟悉,但我不完全理解错误或代码是如何错误的(现在)
谢谢!
【问题讨论】:
-
不要使用
NSArray,使用实际类型化的swift数组
标签: ios iphone swift xcode swift3