我试图迅速调用一个objective-c方法,并得到了这个奇怪的错误:
Cannot convert the expression's type 'Void' to type 'String!'
SWIFT代码:
XNGAPIClient.sharedClient().putUpdateGeoLocationForUserID("me",
accuracy: 3000,
latitude: location.coordinate.latitude as CGFloat,
longitude: location.coordinate.longitude as CGFloat,
ttl: 420, success: { (JSON: AnyObject!) in },
failure: { (error: NSError!) in })
Objective-C方法:
- (void)putUpdateGeoLocationForUserID:(NSString*)userID
accuracy:(CGFloat)accuracy
latitude:(CGFloat)latitude
longitude:(CGFloat)longitude
ttl:(NSUInteger)ttl
success:(void (^)(id JSON))success
failure:(void (^)(NSError *error))failure
如果我将所有内容都转换为建议的类型:
XNGAPIClient.sharedClient().putUpdateGeoLocationForUserID("me" as String,
accuracy: 3000 as CGFloat,
latitude: location.coordinate.latitude as CGFloat,
longitude: location.coordinate.longitude as CGFloat,
ttl: 420 as Int,
success: { (JSON: AnyObject!) in },
failure: { (error: NSError!) in })
我收到以下错误: Cannot convert the expression's type 'Void' to type 'StringLiteralConvertible'
您的问题是location.coordinate.latitude
和location.coordinate.longitude
参数。例如,如果我将这些参数设置为Int,我可以重现您的问题。因此,请尝试:
XNGAPIClient.sharedClient().putUpdateGeoLocationForUserID("me" as String,
accuracy: 3000 as CGFloat,
latitude: CGFloat(location.coordinate.latitude),
longitude: CGFloat(location.coordinate.longitude),
ttl: 420 as Int,
success: { (JSON: AnyObject!) in },
failure: { (error: NSError!) in })
...也就是说,使用CGFloat构造函数,而不是as
对这些参数进行向下转换。(我猜想对于3000字面量来说,在幕后发生了一些聪明的事情,看起来它应该是一个Int,否则一个人可能也不起作用...)
对于非常无用的错误消息,我还会向Apple提出错误。我从错误的参数类型对Objective C的调用中看到了其中的一些。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句