4

I have a

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

I have access to SwiftyJSON Pod in my project, so I've tried

print(JSON(loginAccount))

I got

unknown

What should I do to make my dict more humanly readable ?


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
    }

}
Community
  • 1
  • 1
code-8
  • 54,650
  • 106
  • 352
  • 604
  • Only use Swift version specific tags if your question is about a specific change in that version of the language. Your question is clearly not concerned about a specific change, so don't use those tags to show which version you are using. The assumption is always the latest version. – Dávid Pásztor Feb 28 '19 at 16:35
  • What is `SessionAccount` ? – koen Feb 28 '19 at 16:44
  • @Koen : I updated my post, and here also : https://i.imgur.com/YWVU9qU.png – code-8 Feb 28 '19 at 16:54
  • Your question is very confusing. The title is not reflected at all in the body. If you want to "make my dict more humanly readable", you could try this: https://stackoverflow.com/questions/38773979/is-there-a-way-to-pretty-print-swift-dictionaries-to-the-console – koen Feb 28 '19 at 17:14
  • I want to make pretty print NSObject – code-8 Feb 28 '19 at 17:16
  • I update my title – code-8 Feb 28 '19 at 17:17
  • Hi kyo. I wonder if I could ask you to refrain from adding your copy+paste block of Markdown text that you are adding to your questions. There's 220 of them at present, and it's unnecessary. Your written question should be tailored to your specific need, rather than copy-pasted into the footer in each case. In this question it was unrelated to your need - you wanted to dump a dictionary in readable form, and so no debugging _per se_ was required. – halfer Apr 19 '19 at 10:02
  • Moreover, we ask that chatty and conversational material is removed from posts here. Technical writing is preferred, on the basis that posts are a long-term resource for many readers rather than an ephemeral query to help just the author. – halfer Apr 19 '19 at 10:09
  • With this in mind, I often add the following advice to give a better flavour of things that editors will remove: _We gently discourage greetings, hope-you-can-helps, thanks, advance thanks, notes of appreciation, regards, kind regards, signatures, please-can-you-helps, chatty material and abbreviated txtspk, pleading, how long you've been stuck, voting advice, meta commentary, etc. Just explain your problem, and show what you've tried, what you expected, and what actually happened._ – halfer Apr 19 '19 at 10:10

2 Answers2

7

You can use dump(self).

dump() doesn't return a string. It just prints out to the console,

output will be -

<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
schlebe
  • 3,387
  • 5
  • 37
  • 50
Santosh Botre
  • 549
  • 5
  • 21
4

Is SessionAccount really required to be a class which inherits from NSObject? If you use a struct print is going to print all members.

In a class you have to override description and return a string what you want to display for example

override var description : String {
    return "SessionAccount: \(username) - \(accountType)"
}
vadian
  • 274,689
  • 30
  • 353
  • 361