如何打印/转储NSObject?

时间:2019-02-28 15:51:49

标签: swift console nsobject

我有一个

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

我可以访问项目中的SwiftyJSON Pod,所以我已经尝试过

print(JSON(loginAccount))

我知道了

unknown

我该怎么做才能使我的字典更易读?


SessionAccount

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
    }

}

2 个答案:

答案 0 :(得分:4)

SessionAccount是否真的必须是继承自class的{​​{1}}?如果您使用结构NSObject,则将打印所有成员。

print中,您必须覆盖class并返回要显示的字符串,例如

description

答案 1 :(得分:3)

您可以使用转储(自己)

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