objective-c – 无法将表达式的类型’Void’转换为’String!’类型

我试图用
swift调用一个
objective-c方法,并得到这个奇怪的错误:

无法将表达式的类型’Void’转换为’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 })

我收到以下错误:无法将表达式的类型’Void’转换为’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构造函数而不是向下投射这些参数. (我猜想有一个聪明的东西在幕后为3000文字看起来应该是一个Int,否则那个人可能不会工作,或者……)

我还提出了一个与Apple无关的错误消息的错误.我在使用错误的参数类型调用Objective C时看到了一些.

点赞