2016-11-18 82 views
0

我只是轉換一個項目從斯威夫特2至3斯威夫特後我得到一個錯誤,但我不明白爲什麼代碼是一個問題:.MAP生產[T]斯威夫特3從斯威夫特轉換2

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 

    } 

的錯誤是:「‘地圖’產生‘[T]’,而不是預期的情境結果類型‘的NSArray?’」

我該如何解決這個問題?我熟悉.map,但我不完全瞭解錯誤或代碼錯誤(現在)

謝謝!

+1

別使用'NSArray',使用一個實際的類型快速數組 – dan

回答

3

隱含橋接到基金會類型已removed from Swift 3。你最好使用原生斯威夫特類型的變量:

var imagesInProject : [URL]? 

或者,如果你不能/不想這樣做,無論出於何種原因,添加一個顯式類型轉換:

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

除非'!'拋出一個錯誤,已從lastPathComponent中移除 - 但我無法編輯它,因爲它在六個字符以下 – user3246092