0

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

    }

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

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)

Thanks!

user3246092
  • 880
  • 2
  • 13
  • 28

1 Answers1

3

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
Cœur
  • 37,241
  • 25
  • 195
  • 267
Code Different
  • 90,614
  • 16
  • 144
  • 163
  • 1
    Throws up an error unless '!' is removed from lastPathComponent -- I can't edit it as it's under six characters however – user3246092 Nov 18 '16 at 22:24