【问题标题】:looping, want to print out information about an array of dictionary objects at the end of the loop循环,想在循环结束时打印出字典对象数组的信息
【发布时间】:2012-10-09 20:14:37
【问题描述】:

这可能是几个问题捆绑在一起,但情况就是这样。我在目标 c 中工作,并且有一个 for 循环正在遍历字典对象数组......就像这样。

  for (NSDictionary *dictionary in array){
         log(@"the dictionary object looks like this \n%@\n",dictionary);
         log(@"value of someProp in the current dict object is %@",[dictionary valueForKey:@"someProp"]);
  }

这一切都很好,但我想稍微整理一下它是如何打印到控制台的。我是 Objective c 的新手,但在其他语言方面经验丰富。我想在目标 c 中做这样的事情(以 actionscript 为例)

  var outputString:String;

  for (items in dictionary){
     outputString += dictionary.toString();
     outputString += "value of item in current dict object is " + dictionary [item];// etc

  }

  // now just print out the contents of outputString;
  trace(outputString);

我在这里遇到的挑战是整理不是字符串或 NSString 类型的对象..

我知道类似的东西

  NSString *outputString = @"";

  for (//same loop as above in obj-c){
      outputString = [outputString stringByAppendingString:**someString**];

  }

但也许我只是不熟悉适当的字符串方法,因为简单地替换 dictionary 来打印出它读取 someString 的整个字典对象会产生错误,因为当然字典对象不是字符串。

所以我想

- 将我想要 NSLog 的内容累积到一系列字符串中

-这包括打印出字典对象(或者也可能是数组对象)的内容,将其转换为字符串,并将其添加到 outputString。

有人对如何进行此操作有任何建议吗?

【问题讨论】:

    标签: iphone objective-c logging nsarray nsdictionary


    【解决方案1】:

    您可以通过...编写 NSDictionary 的序列化版本

    NSLog(@"%@", dictionaryItem);
    

    假设您的 NSDictionary 实例名为 dictionaryItem

    它将输出类似于...的字典对象

    {
        key1 = test;
        key2 = 1;
        key3 =     (
            blah
        );
    }
    

    【讨论】:

      【解决方案2】:

      尝试使用 [NSString stringWithFormat:@"Something about value: %@", dictionaryItem]

      【讨论】:

      • 我认为这是正确的答案,我只是这样使用它... myString = [NSString stringWithFormat:@"%@%@%@",a,b,c];效果很好。
      猜你喜欢
      • 2012-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-08
      • 1970-01-01
      相关资源
      最近更新 更多