我最近更新到了 Xcode 9.3 和 Swift 4.1。
自从更新以来,我现在在类属性和字符串属性中看到文本“some”(请参见下面的示例):
示例 json 字符串: some({"email":"test@test.com","password":"password","deviceID":"00000000000:0000000000000000000000000", “用户ID”:“00000000-0000-0000-0000-000000000000”,“用户名”:“测试”})
注意到开头的一些了吗?
另一个例子:
我有一个简单的 User 对象:
代码:
class UserBasic: Codable{
let deviceID: String!
let email: String!
let password: String!
let userid: String!
let username: String!
init(deviceID: String, email: String, password: String, userid: String, username: String) {
self.deviceID = deviceID
self.email = email
self.password = password
self.userid = userid
self.username = username
}
}
当我实例化并迭代属性时,我现在得到:
Attr 0: deviceID = some("00000000000:0000000000000000000000000")
Attr 1: email = some("test@test.com")
Attr 2: password = some("password")
Attr 3: userid = some("00000000-0000-0000-0000-000000000000")
Attr 4: username = some("test")
Again note the text some at the start of the values?!?
自从使用 Xcode 9.3 更新到 Swift 4.1 以来就开始了
它搞砸了 api 调用等,因为现在我有一些与任何例如捆绑在一起的东西。 json编码..
(更新) 这是我的用户 obj inky 和迭代代码:
let userObj = Utility.createUserObj(email: email!, password: password!)
let mirrored_object = Mirror(reflecting: userObj)
for (index, attr) in mirrored_object.children.enumerated() {
if let property_name = attr.label as String! {
print("Attr \(index): \(property_name) = \(attr.value)")
}
}
更新2:
这是上面生成 json 示例的代码:
let userObj = Utility.createUserObj(email: email!, password: password!)
let jsonEncoder = JSONEncoder()
let jsonData = try! jsonEncoder.encode(userObj)
let json: NSString! = NSString(data: jsonData, encoding: String.Encoding.utf8.rawValue)
最佳答案
要解决您的问题,请以这种方式创建 JSON 字符串,隐式解包可选是不合适的。
do {
let jsonData = try jsonEncoder.encode(userObj)
let json = String(data: jsonData, encoding: .utf8)!
} catch { print(error) }
如果JSON编码成功,json
不能为nil
。
关于ios - Swift 4.1 类属性和子字符串前缀为 'some' (Xcode 9.3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49615433/