2015-10-14 67 views
1

有人可以幫我修復這些錯誤嗎?斯威夫特改變,我不知道如何改變這些,使其與新版本的工作:語法錯誤的Swift錯誤

這一個提供了以下錯誤:

Cannot invoke createDirectoryAtPath with an argument list of type (SwiftCoreDataHelper.Type, withintermediateDirectories: Bool, atrributes: NilLiteralConvertible, error:inout NSError?)

NSFileManager.defaultManager().createDirectoryAtPath(SwiftCoreDataHelper, withIntermediateDirectories: true, attributes: nil, error: &error) 

接下來的幾個剛給我說'錯誤「是一個額外的參數:

if storeCoordicator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil, error: &error){ 
     if (error != nil){ 
      print(error!.localizedDescription) 
      abort() 
     } 
    } 

let items: NSArray = managedObjectContext.executeFetchRequest(fetchRequest, error: nil) 

回答

2

在Swift 2中,您需要用do-catch塊捕獲錯誤; 使用addPersistentStoreWithType與CoreData的時候,你需要做到以下幾點:

do 
{ 
    try storeCoordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: nil) 
} 
catch(let error as NSError) 
{ 
    NSLog(error.localizedDescription) //error has occurred. 
    abort() //abort 
} 

這也同樣適用於executeFetchRequest

do 
{ 
    let items: NSArray = try managedObjectContext.executeFetchRequest(fetchRequest) 
} 
catch(let error as NSError) 
{ 
    NSLog(error.localizedDescription) 
} 

createDirectoryAtPath

do 
{ 
    try NSFileManager.defaultManager().createDirectoryAtPath(SwiftCoreDataHelper, withIntermediateDirectories: true, attributes: nil) 
} 
catch(let error as NSError) 
{ 
    NSLog(error.localizedDescription) 
} 
+0

真棒感謝你,修復這兩個,但我怎麼去修復NSFileManager.defaultManager()。createDirectoryAtPath(SwiftCoreDataHelper,withIntermediateDir ectories:true,屬性:nil,錯誤:錯誤) – Karpisdiem

+0

謝謝你的工作 – Karpisdiem