.Map 从 Swift 2 转换后在 Swift 3 中生成 [T] [英] .Map producing [T] in Swift 3 after Converting from Swift 2

查看:20
本文介绍了.Map 从 Swift 2 转换后在 Swift 3 中生成 [T]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚将一个项目从 Swift 2 转换为 Swift 3 并且出现错误,但我不明白为什么代码有问题:

I just converted a project from Swift 2 to Swift 3 and am getting an error, but I do not understand why the code is a problem:

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?'"

The error is: "'map' produces '[T]', not the expected contextual result type 'NSArray?'"

我该如何解决这个问题?我熟悉 .map 但我不完全理解错误或代码是如何错误的(现在)

How do I fix this? I'm familiar with .map but I don't totally understand the error or how the code is wrong (now)

谢谢!

推荐答案

隐式桥接到 Foundation 类型已经从 Swift 3 中删除.您最好为变量使用原生 Swift 类型:

Implicit bridging to Foundation types has been removed from Swift 3. You are better off using native Swift types for your variables:

var imagesInProject : [URL]?

或者如果你不能/不想这样做,无论出于何种原因,添加一个明确的演员:

Or if you can't/don't want to do that for whatever reason, add an explicit cast:

imagesInProject = urls
                    .filter {$0.lastPathComponent.hasPrefix("AppIcon") == false}
                    .map {$0.lastPathComponent} as NSArray

这篇关于.Map 从 Swift 2 转换后在 Swift 3 中生成 [T]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆