代码之家  ›  专栏  ›  技术社区  ›  code-8

如何将iOS NS对象转换为字典-Swift 4.2?

  •  0
  • code-8  · 技术社区  · 6 年前

    我有一个

    Crashlytics.sharedInstance().setObjectValue(loginAccount, forKey: "loginAccount")
    

    我有权 SwiftyJSON 在我的项目里,所以我试过了

    print(JSON(loginAccount))
    

    我得到了

    unknown
    

    我该怎么做才能让我的口述更人性化?


    会话帐户

    import Foundation
    import SwiftyJSON
    
    
    public class SessionAccount : NSObject {
    
        var id             : Int?
        var username       : String?
        var password       : String?
        var accountType    : String?
        var role           : String?
        var privateVlan    : String?
        var guestVlan      : String?
        var timezoneOffset : Int?
        var loginTime      : Date?
        var cpe            : CPE?
        var cpeName        : String?
        var cpeProfiles    : [Profile] = []
        var deviceCount    : Int?
        var activeCount    : Int?
        var inactiveCount  : Int?
        var offlineCount   : Int?
        var profileDevices : [Device] = []
        var cpeDevices     : [Device] = []
        var lanSetting     : LANSetting?
        var alerts         : [Alert] = []
        var quarantineList : [String] = []
    
        override init() {
            super.init()
        }
    
        convenience init(_ record: JSON) {
            self.init()
            self.id = record["id"].int
            self.accountType = record["accountType"].string
            self.role = record["role"].string
            self.timezoneOffset = record["timezoneOffset"].int
        }
    
    }
    
    0 回复  |  直到 4 年前
        1
  •  4
  •   vadian    6 年前

    SessionAccount 真的需要成为 class 继承自 NSObject print 将打印所有成员。

    在一个 你必须超越 description 并返回一个字符串,例如要显示的内容

    override var description : String {
        return "SessionAccount: \(username) - \(accountType)"
    }
    
        2
  •  3
  •   schlebe    5 年前

    你可以用 .

    dump()不返回字符串。打印到控制台上,

    输出将是-

    <SessionAccount: 0x000055957bfcd400> #0
      - super: Foundation.NSObject
      - id: nil
      - username: nil
      - password: nil
      - accountType: nil
      - role: nil
      - privateVlan: nil
      - guestVlan: nil
      - timezoneOffset: nil
      - loginTime: nil
      - cpeName: nil
      - deviceCount: nil
      - activeCount: nil
      - inactiveCount: nil
      - offlineCount: nil
      - quarantineList: 0 elements