【问题标题】:Enumerating array which contains a dictionary produces unexpected output枚举包含字典的数组会产生意外的输出
【发布时间】:2009-07-02 00:04:47
【问题描述】:

我的问题是为什么它输出日志中的最后 4 行(见下文)...这些对象是日志中先前打印的字典的一部分,并且不应位于数组的末尾?我在这里遗漏了一些基本的东西......谢谢

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSURL URLWithString: @"www.stanford.edu"], 
    @"Stanford University", 
    [NSURL URLWithString: @"www.apple.com"], 
    @"Apple shop", 
    [NSURL URLWithString: @"cs193p.stanford.edu"], 
    @"CS193P course", 
    [NSURL URLWithString: @"itunes.stanford.edu"], 
    @"Stanford on iTunes U", 
    [NSURL URLWithString: @"stanfordshop.com"], 
    @"Stanford Mall", 
    nil];

NSMutableArray *myArray = [NSMutableArray arrayWithObjects:
    [NSString init],
    [NSURL URLWithString: @"www.stanford.edu"],
    [NSProcessInfo processInfo],
    dictionary,
    [@"Mutable string example" mutableCopy],
    [@"another mutable string" mutableCopy]];

NSEnumerator *enumerator = [myArray objectEnumerator];
id object;

while ((object = [enumerator nextObject])) {
    NSLog([object description]);
}

2009-07-02 09:35:12.756 WhatATool[6407:10b] NSString
2009-07-02 09:35:12.756 WhatATool[6407:10b] www.stanford.edu
2009-07-02 09:35:12.757 WhatATool[6407:10b] <NSProcessInfo: 0x107e20>
2009-07-02 09:35:12.758 WhatATool[6407:10b] {
“苹果商店”= www.apple.com;
"CS193P 课程" = cs193p.stanford.edu;
“斯坦福购物中心”= stanfordshop.com;
“斯坦福大学”= www.stanford.edu;
“iTunes U 上的斯坦福” = itunes.stanford.edu;
}
2009-07-02 09:35:12.758 WhatATool[6407:10b] 可变​​字符串示例
2009-07-02 09:35:12.759 WhatATool[6407:10b] 另一个可变字符串
2009-07-02 09:35:12.760 WhatATool[6407:10b] itunes.stanford.edu
2009-07-02 09:35:12.760 WhatATool[6407:10b] iTunes U 上的斯坦福
2009-07-02 09:35:12.761 WhatATool[6407:10b] stanfordshop.com
2009-07-02 09:35:12.762 WhatATool[6407:10b] 斯坦福购物中心

【问题讨论】:

    标签: objective-c nsarray nsdictionary enumeration


    【解决方案1】:

    当您使用便捷方法创建 NSMutableArray 时,我认为您缺少必要的 nil 作为最后一个参数。有没有

    NSMutableArray *myArray = [NSMutableArray arrayWithObjects:
        [NSString init],
        [NSURL URLWithString: @"www.stanford.edu"],
        [NSProcessInfo processInfo],
        dictionary,
        [@"Mutable string example" mutableCopy],
        [@"another mutable string" mutableCopy],
        nil];
    

    工作?

    【讨论】:

      【解决方案2】:

      打开警告(其他警告标志“-Wall”),你会得到:

      警告:函数调用中缺少标记

      在你的 NSMutableArray arrayWithObjects 方法的末尾告诉你丢失的 nil。

      【讨论】:

      • 我喜欢 -Wall 拯救世界。虽然它经常会产生大量被忽略的垃圾,但有时它确实会发光......
      • “大量的垃圾”?我使用 -Wall 运行 permanelty 并“将警告视为错误”(这样可以避免在您进行构建时出现警告,然后再看不到它们)。我的代码编译干净,没有任何警告。我想如果你使用蹩脚的第三方代码可能是一个问题,但除此之外,只需修复警告!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多