我设置了一个默认的字符串值?运营商。但我得到一个可选的字符串?
request(.GET, CUSTOMER_URL, parameters: params, encoding: .URL).responseJSON { (request, response, result) -> Void in
var message = JSON(result.value ?? "")["message"].string ?? "Default value to make it nonOptional"
switch(result) {
case .Success(let json):
if let customer = JSON(json)["customer"].dictionaryObject {
GlobalCache.sharedInstance.setCustomer(customer)
}
completion(succeed: response?.statusCode == 200, message: message)
case .Failure(_,_):
completion(succeed: false, message: message)
}
}
预期行为
意外行为
答案 0 :(得分:1)
答案 1 :(得分:0)
这类似于Providing a default value for an Optional in Swift?
您可以使用类型注释来强制使用非可选值:
var message: String = JSON(result.value ?? "")["message"].string ?? "Default value"