Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c Swift中的返回信号方法_Objective C_Swift_Racsignal - Fatal编程技术网

Objective c Swift中的返回信号方法

Objective c Swift中的返回信号方法,objective-c,swift,racsignal,Objective C,Swift,Racsignal,我在Obj-C中有以下方法: - (RACSignal *)fetchCurrentConditionsForLocation:(CLLocationCoordinate2D)coordinate { NSString *urlString = [NSString stringWithFormat:@"http://api.openweathermap.org/data/2.5/weather?lat=%f&lon=%f&units=metric", coordinate

我在Obj-C中有以下方法:

- (RACSignal *)fetchCurrentConditionsForLocation:(CLLocationCoordinate2D)coordinate {
    NSString *urlString = [NSString stringWithFormat:@"http://api.openweathermap.org/data/2.5/weather?lat=%f&lon=%f&units=metric", coordinate.latitude, coordinate.longitude];
    NSURL *url = [NSURL URLWithString:urlString];

    return [[self fetchJSONFromURL:url] map:^(NSDictionary *json) {
        return [MTLJSONAdapter modelOfClass:[WXCondition class] fromJSONDictionary:json error:nil];
    }];
}
我转换成Swift:

func fetchJSONFromURL(url: NSURL) -> RACSignal {

}

func fetchCurrentConditionsForLocation(coordinate: CLLocationCoordinate2D) -> RACSignal {
    let urlString = NSString(format: "http://api.openweathermap.org/data/2.5/weather?lat=%f&lon=%f&units=metric", coordinate.latitude, coordinate.longitude)
    let url = NSURL.URLWithString(urlString)

    // Convert to Swift?        
    return [[self fetchJSONFromURL:url] map:^(NSDictionary *json) {
        return [MTLJSONAdapter modelOfClass:[WXCondition class] fromJSONDictionary:json error:nil];
    }];
}
在Swift中使用此地图时遇到问题:

return [[self fetchJSONFromURL:url] map:^(NSDictionary *json) {
    return [MTLJSONAdapter modelOfClass:[WXCondition class] fromJSONDictionary:json error:nil];
}];

一切都在正确编译,但有没有更好的方法呢?

我还没有在项目中尝试过,但也许这样可以

return fetchJSONFromURL(url).map { (json: NSDictionary) in
    return MTLJSONAdapter.modelOfClass(WXCondition.self, fromJSONDictionary: json, error: nil)
} as RACSignal

您的Swift代码顶部有
import CoreLocation
吗?@dpassage:是的,我只是为了了解
CLLocationCoordinate2D
错误而加入了它。我已经更新了我的问题。这不起作用,因为
map
不是
NSDictionary
的成员。我的代码正在工作,我只是不确定这是否是正确的方法。那么Objective-C代码是如何工作的?谁将
map
添加到
NSDictionary
?我认为这是一个特殊的
RACSignal
返回映射。在Objective-C中
fetchJSONFromURL
是否返回
NSDictionary
?不,它返回
RACSignal