问题
I have the following method in 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];
}];
}
My conversion to 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];
}];
}
Having trouble with this map in Swift:
return [[self fetchJSONFromURL:url] map:^(NSDictionary *json) {
return [MTLJSONAdapter modelOfClass:[WXCondition class] fromJSONDictionary:json error:nil];
}];
Everything is compiling properly, but is there a better way of doing this?
回答1:
I haven't tried in a project, but perhaps this wil do
return fetchJSONFromURL(url).map { (json: NSDictionary) in
return MTLJSONAdapter.modelOfClass(WXCondition.self, fromJSONDictionary: json, error: nil)
} as RACSignal
来源:https://stackoverflow.com/questions/24208133/return-racsignal-method-in-swift