【问题标题】:Can you see the values of NSUserDefaults anywhere in the xcode debugger?你能在 xcode 调试器的任何地方看到 NSUserDefaults 的值吗?
【发布时间】:2010-06-14 18:22:55
【问题描述】:

你能在 xcode 调试器的任何地方看到 NSUserDefaults 的值吗?

只是想知道这是否可能?

谢谢,

尼克

【问题讨论】:

  • 你接受一个答案怎么样?
  • 嘿,对不起@David,这样做了。

标签: iphone xcode debugging nsuserdefaults


【解决方案1】:

我没有在调试器中查看它们的解决方案,但我可以提供:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSLog(@"%@", [defaults dictionaryRepresentation]);

对于一些穴居人调试:)

编辑:正如 David 在评论中建议的那样,我们现在可以在调试控制台中执行此操作:

po [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]

斯威夫特 3.0

po UserDefaults.standard.dictionaryRepresentation()

【讨论】:

  • 这是最好的答案。只需在调试器中执行此操作:po [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]
【解决方案2】:

我还没有这样做,但您应该能够在用户默认值上执行 po(打印对象)命令,如下所示:

po [[NSUserDefaults standardUserDefaults] valueForKey:@"someKeyName"] 

我更喜欢将我的默认值包装在一个自定义类中,并创建一个转储默认值的描述方法。

您可以使用“默认值”命令行实用程序来准确检查默认值。阅读手册页了解详细信息。

【讨论】:

    【解决方案3】:

    不知道任何显示 NSUserDefaults 的 GUI,但我在我的应用程序委托中使用它来查看启动时的设置:

    - (void)applicationDidFinishLaunching:(UIApplication *)application
    {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSLog(@"%@ DEFAULTS = %@", [self class], [defaults persistentDomainForName:[[NSBundle mainBundle] bundleIdentifier]]);
    } 
    

    【讨论】:

      【解决方案4】:

      您可以在调试器中记录或使用 PO 命令 钥匙:

      NSLog(@"%@", [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys]);
      

      或者对于键和值:

      NSLog(@"%@", [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]);
      

      & 调试器使用: 获取所有密钥:

       po [[[NSUserDefaults standardUserDefaults] dictionaryRepresentation] allKeys]
      

      键和值:

      po [[NSUserDefaults standardUserDefaults] dictionaryRepresentation]
      

      【讨论】:

        【解决方案5】:

        为 Swift 5 更新

        将所有 UserDefault 键值对打印到控制台:

        print(UserDefaults.standard.dictionaryRepresentation())
        

        将 UserDefault keys 打印到控制台:

        print(UserDefaults.standard.dictionaryRepresentation().keys)
        

        将 UserDefault 打印到控制台:

        print(UserDefaults.standard.dictionaryRepresentation().values)
        

        【讨论】:

          【解决方案6】:

          阅读:

          po UserDefaults.standard.value(forKey: "key")
          

          写:

          po UserDefaults.standard.set(true, forKey: "key")
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2010-10-19
            • 2011-03-28
            • 2019-03-05
            • 1970-01-01
            • 2018-06-21
            • 1970-01-01
            • 2013-03-30
            相关资源
            最近更新 更多