Swift 3 RKObjectRequestOperation 返回错误无法转换类型为 RKObjectRequestOperation
Posted
技术标签:
【中文标题】Swift 3 RKObjectRequestOperation 返回错误无法转换类型为 RKObjectRequestOperation【英文标题】:Swift 3 RKObjectRequestOperation returns error unable to convert of type RKObjectRequestOperation 【发布时间】:2016-11-29 19:44:26 【问题描述】:我正在尝试将代码从现有应用程序转换为 swift 3 (Xcode 8.1),但我卡在下面给出错误的代码上:
cannot convert value of type (RKObjectRequestOperation!, RKMappingResult!) -> Void to expected argument type (RKObjectRequestOperation?, RKMappingResult?) -> Void!)
这是我的代码:
class func getShoppingItemsForPage(_ page: Int,pageSize: Int,
completionHandler:@escaping ([ShopItemModel]?,NSError?) -> ())
RKObjectManager.sharedManager().getObjectsAtPath("search",
parameters: ["page" : page, "pagesize" : pageSize], success: (request: RKObjectRequestOperation!, result: RKMappingResult!) -> Void in
let resultArray = result.array() as? [ShopItemModel]
completionHandler(resultArray,nil)
) (request: RKObjectRequestOperation!, error: NSError!) -> Void in
completionHandler(nil,error)
我怀疑这可能与我看到的 FBSDKLoginManager 中的类似错误有关
Cannot convert value of type (FBSDKLoginManagerLoginResult!, NSError!) -> Void to expected argument type "FBSDKLoginManagerRequestTokenHandler!"
这是产生错误的 Facebook 代码。
loginManager.logIn(withReadPermissions: facebookReadPermissions, from: self, handler: (result:FBSDKLoginManagerLoginResult!,error:NSError!) -> Void in
if error != nil
// Process Error
else if result.isCancelled
// Cancelled by User
else
let delegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
delegate.showMainStoryboard()
)
【问题讨论】:
这可能有助于github.com/ekreative/testbuild-rocks-ios/blob/master/AppsV2/… 【参考方案1】:class func getShoppingItemsForPage(_ page: Int,pageSize: Int,
completionHandler:@escaping ([ShopItemModel]?,NSError?) -> ())
RKObjectManager.sharedManager().getObjectsAtPath("search",
parameters: ["page" : page, "pagesize" : pageSize],
success: (operation: RKObjectRequestOperation!, mappingResult: RKMappingResult!) -> Void in
let resultArray = mappingResult.array() as? [ShopItemModel]
completionHandler(resultArray,nil)
, failure: (operation: RKObjectRequestOperation!, error: NSError!) -> Void in
completionHandler(nil,error)
)
【讨论】:
这不起作用。有 2 个错误,如果我更改为上述错误,第 4 行仍然存在相同的错误。如果我将第 4 行更改为带有问号,则您识别的代码有错误。【参考方案2】:解决方案是将错误类型从 NSError 更改为 Error,并使调用和结果参数可选。
【讨论】:
以上是关于Swift 3 RKObjectRequestOperation 返回错误无法转换类型为 RKObjectRequestOperation的主要内容,如果未能解决你的问题,请参考以下文章
swift takasek / CodePiece.swiftのSwift 3.1版