我是Swift的新手,我正在尝试将这行代码从Objective-C转换为Swift。这是objective-c代码:
首先,条款和地点的目标C代码是:
NSString *term = [[NSUserDefaults standardUserDefaults] valueForKey:@"term"] ?: defaultTerm;
NSString *location = [[NSUserDefaults standardUserDefaults] valueForKey:@"location"] ?: defaultLocation;
我把它翻译成了这个swift(我不知道如何解释?:defaultTerm)
var term: AnyObject? = NSUserDefaults.standardUserDefaults().valueForKey("term");
var location: AnyObject? = NSUserDefaults.standardUserDefaults().valueForKey("location");
var ApiSample:YPAPISample = YPAPISample();
这是Objective-C中的查询: [APISample queryTopBusinessInfoForTerm:术语位置:位置completionHandler:^(NSDictionary * topBusinessJSON,NSError *错误){
当我输入我的swift代码时,它会像这样出现
APISample.queryTopBusinessInfoForTerm(term, location: location) { (topBusinessJSON, error) -> Void in
code
}
我尝试删除 - >并且无效,但它似乎并不喜欢它。
答案 0 :(得分:0)
这有用吗?
// cast the results that come back from UserDefaults
var term: String? = NSUserDefaults.standardUserDefaults().valueForKey("term") as? String
var location: String? = NSUserDefaults.standardUserDefaults().valueForKey("location") as? String
APISample.queryTopBusinessInfoForTerm(term, location: location, completionHandler: { (topBusinessJSON: [NSObject: AnyObject]!, error: NSError!) -> Void in
//code
})