【问题标题】:Swift Errors with syntaxSwift 语法错误
【发布时间】:2015-10-14 02:40:35
【问题描述】:

有人可以帮我解决这些错误吗? Swift 发生了变化,我不知道如何更改这些以使其适用于新版本:

这个给出以下错误:

无法使用类型参数列表调用 createDirectoryAtPath(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)

【问题讨论】:

    标签: ios swift swift2 ios9 xcode7


    【解决方案1】:

    在 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)
    }
    

    【讨论】:

    • 非常感谢您修复了这两个问题,但是我该如何修复 NSFileManager.defaultManager().createDirectoryAtPath(SwiftCoreDataHelper, withIntermediateDirectories: true, attributes: nil, error: &error)
    猜你喜欢
    • 2015-05-09
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 1970-01-01
    • 2021-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多