XCode 7 print keys and values of Dictionary

纵然是瞬间 提交于 2019-12-21 17:51:21

问题


Print description in xcode 7 is giving memory addresses similar to below. Tried all the options, but getting output like this. Please let me know how to print the exact value of a dictionary.

▿ 3 elements
  ▿ [0] : 2 elements
    - .0 : Output
    ▿ .1 : 3 elements
      ▿ [0] : 2 elements
        - .0 : type
        - .1 : Output2 { ... }
      ▿ [1] : 2 elements
        - .0 : version
        - .1 : 1.0
      ▿ [2] : 2 elements
        - .0 : content
        ▿ .1 : 2 elements

Not sure why Apple is 100% concentrating on console window.. there is change from XCode 5 to 6, 6 to 6.2, 6.2 to 6.4 and now 6.4 to 7.. I think Apple should focus on improving the quality instead of console)


回答1:


I created this test dictionary:

let dict:Dictionary = ["key1": "value1", "key2": 42, "keyForColor": UIColor.redColor()]

Then I used po dict and get the result you described:

po dict
▿ 3 elements
  ▿ [0] : 2 elements
    - .0 : "key1"
    - .1 : value1
  ▿ [1] : 2 elements
    - .0 : "keyForColor"
  ▿ [2] : 2 elements
    - .0 : "key2"

When you use po dict.description, you get this:

po dict.description
"[\"key1\": value1, \"keyForColor\": UIDeviceRGBColorSpace 1 0 0 1, \"key2\": 42]"



回答2:


Try with CFShow:

Prints a description of a Core Foundation object to stderr.

 po CFShow(dict)



回答3:


<command-name>'.
(lldb) repl dump(dict)
▿ 3 key/value pairs
  ▿ [0]: (2 elements)
    - .0: key1
    ▿ .1: Swift._NSContiguousString #0
      ▿ super: Swift._SwiftNativeNSString
        ▿ super: value1
          ▿ NSString: value1
            - NSObject: value1
      ▿ _core: Swift._StringCore
        ▿ _baseAddress: Swift.COpaquePointer
          - _rawValue: (Opaque Value)
        - _countAndFlags: 6
        - _owner: nil
  ▿ [1]: (2 elements)
    - .0: keyForColor
    ▿ .1: UIDeviceRGBColorSpace 1 0 0 1 #1
      ▿ UIDeviceRGBColor: UIDeviceRGBColorSpace 1 0 0 1
        ▿ UIColor: UIDeviceRGBColorSpace 1 0 0 1
          - NSObject: UIDeviceRGBColorSpace 1 0 0 1
  ▿ [2]: (2 elements)
    - .0: key2
    ▿ .1: 42 #2
      ▿ NSNumber: 42
        ▿ NSValue: 42
          - NSObject: 42
([String : NSObject]) $R1 = 3 key/value pairs {
  [0] = (key = "key1", value = "value1")
  [1] = (key = "keyForColor", value = 0x00007f9c42d3aab0)
  [2] = (key = "key2", value = Int64(42))
}



回答4:


As a follow up on @user3441734, you can simply get the last part by using p instead of po:

(lldb) p dict
([String : NSObject]) $R1 = 3 key/value pairs {
  [0] = (key = "key1", value = "value1")
  [1] = (key = "keyForColor", value = 0x00007f9c42d3aab0)
  [2] = (key = "key2", value = Int64(42))
}

If this doesn't suit your needs, you could always write an extension of Dictionary and build a custom description, something in the line of:

extension Dictionary {

  var fancyDescription: String {
      var description = ""
      for (key, value) in self {
          description += "\(key) : \(value), "
      }  
      return "{ \(description) }"
  }
}

and use it with po dict.fancyDescription



来源:https://stackoverflow.com/questions/32936686/xcode-7-print-keys-and-values-of-dictionary

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!