值有时会以 SwiftyJSON 的可选字符串形式返回 [英] Value sometimes returns as optional string with SwiftyJSON
问题描述
我想知道为什么 SwiftyJSON 有时返回值作为 "myString" 和其他时候作为 Optional("myString")
例如:
func getFBUserData(){if((FBSDKAccessToken.currentAccessToken()) != nil){FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, email"]).startWithCompletionHandler({ (connection, result, error) -> Void in如果(错误 == 零){让 fbJson = JSON(结果)让 fbId = fbJson["id"].string打印(fbId)}})}}
这里 fbId 将返回:Optional("myString")
下一步:
static func loginWithEmail(facebookId: String, email: String, username: String, response: (token: String?) -> ()) {让 urlString = baseURL + ResourcePath.FbLogin.description让参数:[字符串:AnyObject] = ["fb_id": facebookId,1级,电子邮件":电子邮件,用户名":用户名]Alamofire.request(.POST, urlString, parameters: parameters).responseJSON{ (responseData) ->作废//打印(响应数据)让 json = JSON(responseData.result.value!)让令牌 = json["api_token"].string响应(令牌:令牌)}}
这里 token 将返回 "myString"
那么当两个函数都使用 ".string" 而不是 ".string!" 时,一个函数如何返回 optional() ?
SwiftyJSON 有两种用于检索值的getter":可选和非可选.
.string
是值的 String 表示的可选 getter,因此您必须在使用前解开它
if let fbId = fbJson["id"].string {打印(fbId)}
否则字符串本身将包含术语可选".
如果您100% 确定总会有一个值,您可以通过使用非可选的 getter 来使用强制解包"的等效项,并且您不需要 如果让
不再:
let fbId = fbJson["id"].stringValue
但要小心,因为如果值为 nil,这会崩溃,就像你用 fbJson["id"].string!
强制解包一样(你永远不应该这样做).>
请注意,SwiftyJSON 对其他类型的工作方式相同:.int
与 .intValue
、.array
与 .arrayValue
等
I wonder why the SwiftyJSON sometimes returns a value as "myString" and some other time as Optional("myString")
For example:
func getFBUserData(){
if((FBSDKAccessToken.currentAccessToken()) != nil){
FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, email"]).startWithCompletionHandler({ (connection, result, error) -> Void in
if (error == nil){
let fbJson = JSON(result)
let fbId = fbJson["id"].string
print(fbId)
}
})
}
}
Here fbId will return: Optional("myString")
Next:
static func loginWithEmail(facebookId: String, email: String, username: String, response: (token: String?) -> ()) {
let urlString = baseURL + ResourcePath.FbLogin.description
let parameters: [String: AnyObject] = [
"fb_id": facebookId,
"level": 1,
"email": email,
"username": username
]
Alamofire.request(.POST, urlString, parameters: parameters).responseJSON{ (responseData) -> Void in
//print(responseData)
let json = JSON(responseData.result.value!)
let token = json["api_token"].string
response(token: token)
}
}
Here token will return "myString"
So how can one function return optional() when both functions use ".string" and not ".string!"?
SwiftyJSON has two kinds of "getters" for retrieving values: Optional and non-Optional.
.string
is the optional getter for the String representation of a value, so you have to unwrap it before use
if let fbId = fbJson["id"].string {
print(fbId)
}
otherwise the String itself will contain the term "Optional".
If you are 100% sure that there will always be a value, you can use the equivalent of "force unwrap" by using the non-Optional getter and you don't need if let
anymore:
let fbId = fbJson["id"].stringValue
But beware because this will crash if the value is nil, exactly as if you were force-unwrapping with fbJson["id"].string!
(which you should never do).
Note that SwiftyJSON works the same for the other types: .int
vs .intValue
, .array
vs .arrayValue
, etc.
这篇关于值有时会以 SwiftyJSON 的可选字符串形式返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!