ios - Swift 4.1 类属性和子字符串前缀为 'some' (Xcode 9.3)

标签 ios swift xcode

我最近更新到了 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/

相关文章:

ios - 在 Swift 中为 UITextField 设置光标位置

ios - View 在闭包表达式中没有改变

swift - ARKit图像检测,添加图像

ios - Swift 中的 UIImageView 动画有时间延迟

iphone - 在 iOS 等应用程序中创建您自己的文件夹

c++ - Objective C/C++ 是否有像 __APPLE__ 这样的定义

ios - UIPickerView - 循环数据

ios - 如何将桥接头添加到 Xcode Playground?

ios - 带有自定义 UIToolbar xib 的 Storyboard 中的 UINavigationController 失败

c++ - 如何防止在 std header (在 Xcode 中)内发生编译错误?