什么是 ??= Swifter 项目中的运算符
What is ??= operator in Swifter project
我在 Swifter library in GitHub but no where talking about this operator and what it can do... this operator located under source files in SwifterFollowers.swift Line 89.
的源代码中看到了一个运算符 ??=
此文件的代码片段:
func getUserFollowersIDs(for userTag: UserTag,
cursor: String? = nil,
count: Int? = nil,
success: CursorSuccessHandler? = nil,
failure: FailureHandler? = nil) {
let path = "followers/ids.json"
var parameters = [String: Any]()
parameters[userTag.key] = userTag.value
parameters["cursor"] ??= cursor /* --- Here --- */
parameters["stringify_ids"] = true
parameters["count"] ??= count
self.getJSON(path: path, baseURL: .api, parameters: parameters, success: { json, _ in
success?(json["ids"], json["previous_cursor_str"].string, json["next_cursor_str"].string)
}, failure: failure)
}
我发现 ??=
是由开发者在 Operator++ 文件中创建的。
那么这个运算符只在Swifter
项目中有效,逻辑如下:
/// If `rhs` is not `nil`, assign it to `lhs`.
infix operator ??= : AssignmentPrecedence // { associativity right precedence 90 assignment } // matches other assignment operators
/// If `rhs` is not `nil`, assign it to `lhs`.
func ??=<T>(lhs: inout T?, rhs: T?) {
guard let rhs = rhs else { return }
lhs = rhs
}
我在 Swifter library in GitHub but no where talking about this operator and what it can do... this operator located under source files in SwifterFollowers.swift Line 89.
的源代码中看到了一个运算符??=
此文件的代码片段:
func getUserFollowersIDs(for userTag: UserTag,
cursor: String? = nil,
count: Int? = nil,
success: CursorSuccessHandler? = nil,
failure: FailureHandler? = nil) {
let path = "followers/ids.json"
var parameters = [String: Any]()
parameters[userTag.key] = userTag.value
parameters["cursor"] ??= cursor /* --- Here --- */
parameters["stringify_ids"] = true
parameters["count"] ??= count
self.getJSON(path: path, baseURL: .api, parameters: parameters, success: { json, _ in
success?(json["ids"], json["previous_cursor_str"].string, json["next_cursor_str"].string)
}, failure: failure)
}
我发现 ??=
是由开发者在 Operator++ 文件中创建的。
那么这个运算符只在Swifter
项目中有效,逻辑如下:
/// If `rhs` is not `nil`, assign it to `lhs`.
infix operator ??= : AssignmentPrecedence // { associativity right precedence 90 assignment } // matches other assignment operators
/// If `rhs` is not `nil`, assign it to `lhs`.
func ??=<T>(lhs: inout T?, rhs: T?) {
guard let rhs = rhs else { return }
lhs = rhs
}