在最后一次更新XCode之后,它建议我将我的代码更新为swift 3语言。 XCode帮助我完成更新,在我修复了一些需要手动更新的东西之后,我遇到了一个我无法弄清楚如何做的转换。
这是关于删除Restkit中的孤立对象。以下是XCode生成的代码,用于将swift 2更新为swift 3:
// fetchRequestBlock to delete orphaned objects ------ tmenus --------
manager?.addFetchRequest { (url:URL!) -> NSFetchRequest<AnyObject>! in
let pathMatcher = RKPathMatcher(pattern: config.getDataPath)
let match = pathMatcher.matchesPath(url.relativePath, tokenizeQueryStrings: false, parsedArguments: nil)
guard match else {
return nil
}
let fetchRequest = NSFetchRequest(entityName: "TipoMenuEntity")
return fetchRequest
}
显示的错误:
/Users/mast/work/gColegios/iOSapp/gColegios/AppDelegate.swift:417:34: Cannot convert value of type '(URL!) -> <<error type>>' to expected argument type '((URL?) -> NSFetchRequest<NSFetchRequestResult>?)!'
所以我使用在swift 3中管理请求的新方法更新了代码:
// fetchRequestBlock to delete orphaned objects ------ tmenus --------
manager?.addFetchRequest { (url:URL!) -> NSFetchRequest<TipoMenu>! in
let pathMatcher = RKPathMatcher(pattern: config.getDataPath)
let match = pathMatcher.matchesPath(url.relativePath, tokenizeQueryStrings: false, parsedArguments: nil)
guard match else {
return nil
}
let fetchRequest: NSFetchRequest<TipoMenu> = TipoMenu.fetchRequest()
return fetchRequest
}
现在它显示新错误:
/Users/mast/work/gColegios/iOSapp/gColegios/AppDelegate.swift:418:34: Cannot convert value of type '(URL!) -> NSFetchRequest<TipoMenu>!' to expected argument type '((URL?) -> NSFetchRequest<NSFetchRequestResult>?)!'
我尝试了一切:改变'(URL!) - &gt; NSFetchRequest!”通过'((URL?) - &gt; NSFetchRequest?)!' ,以及之间的各种组合?和!
任何人都可以帮我解决这个问题吗?
提前致谢
答案 0 :(得分:1)
嗯,事实证明正确的代码是
// fetchRequestBlock to delete orphaned objects ------ tmenus --------
manager?.addFetchRequest { (url:URL?) -> NSFetchRequest<NSFetchRequestResult>? in
let pathMatcher = RKPathMatcher(pattern: config.getDataPath)
if let match = pathMatcher?.matchesPath(url?.relativePath, tokenizeQueryStrings: false, parsedArguments: nil)
{
if (match)
{
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "TipoMenuEntity")
return fetchRequest
}
}
return nil
}
让我困惑的是,在其他请求中,有必要为要创建的请求放置NSFetchRequest,因为显然使用swift 3 NSManagedObject类扩展代码以符合NSFetchRequest的委托。
然而,这里只是XCode完成的函数的标题,以使其适应swift 3.所以它只是放置正确的标题并使代码适应此版本的新选项。
感谢New16带领我这个。
答案 1 :(得分:0)
该错误基本上表示期待NSFetchRequestResult
。这是例如NSAsynchronousFetchResult
的结果。您正在使用请求。