我试图用swift调用一个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'
答案 0 :(得分:2)
您的问题是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时看到了一些。