快速将Objective-C块转换为闭包
我已成功地将以下方法从Obj-C转换为Swift: 在学习如何使用Swift后 Obj-C:快速将Objective-C块转换为闭包,objective-c,closures,objective-c-blocks,swift,reactive-cocoa,Objective C,Closures,Objective C Blocks,Swift,Reactive Cocoa,我已成功地将以下方法从Obj-C转换为Swift: 在学习如何使用Swift后 Obj-C: - (RACSignal *)fetchCurrentConditionsForLocation:(CLLocationCoordinate2D)coordinate { NSString *urlString = [NSString stringWithFormat:@"http://api.openweathermap.org/data/2.5/weather?lat=%f&lon=
- (RACSignal *)fetchCurrentConditionsForLocation:(CLLocationCoordinate2D)coordinate {
NSString *urlString = [NSString stringWithFormat:@"http://api.openweathermap.org/data/2.5/weather?lat=%f&lon=%f&units=imperial", 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];
}];
}
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)
return fetchJSONFromURL(url).map { json in
return MTLJSONAdapter.modelOfClass(WXCondition.self, fromJSONDictionary: json as NSDictionary, error: nil)
}
}
Swift:
- (RACSignal *)fetchCurrentConditionsForLocation:(CLLocationCoordinate2D)coordinate {
NSString *urlString = [NSString stringWithFormat:@"http://api.openweathermap.org/data/2.5/weather?lat=%f&lon=%f&units=imperial", 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];
}];
}
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)
return fetchJSONFromURL(url).map { json in
return MTLJSONAdapter.modelOfClass(WXCondition.self, fromJSONDictionary: json as NSDictionary, error: nil)
}
}
但是,我无法将以下返回图块转换为Swift:
如果有帮助,这就是rac\u序列的含义:
- (RACSequence *)rac_sequence {
return [RACArraySequence sequenceWithArray:self offset:0];
}
RACArraySequence()
:
EDIT:fetch方法返回一个非NSArray
:
func fetchJSONFromURL(url: NSURL) -> RACSignal {
}
看起来您只是忘记了函数声明中的返回类型。声明应该是这样的:
func fetchHourlyForecastForLocation(coordinate: CLLocationCoordinate2D) -> RACSignal { //...rest of function
由于返回类型现在已命名,因此可以在函数末尾的return语句中删除as NSArray
强制转换。希望这有帮助 试试这个:
func fetchHourlyForecastForLocation(coordinate: CLLocationCoordinate2D,completion:(AnyObject) -> Void) {
//after getting the NSArray (maping)
completion(_array)
}
多好的问题啊!一个快速注释是始终使用初始值设定项语法NSURL(字符串:urlString)
而不是NSURL.urlWithString
。您将其用于NSString
,但不用于NSURL
:p@JackWu:谢谢你的建议!谢谢你!为什么使用初始值设定项语法更好?在Swift中,这是进行初始化的唯一方法。没有更多的类初始值设定项和实例初始值设定项。ObjC初始值设定项和工厂方法都会自动转换为这种新语法。这就是未来@JackWu:有道理,谢谢。我已经更新了fetchJSONFromURL()
返回的内容。它实际上不是一个NSArray
它是一个RACSignal
。是的,尽管还有很多代码没有编译。这不是移植该函数所需的唯一更改。有一件事我忘了提到:如果你有一个集合,你必须放上尖括号,并说明它将包含什么(例如,整数列表的Array
)。