views:

3647

answers:

2

My question is why does it output the last 4 lines in the log(see below)...those objects are part of the dictionary printed earlier in the log & should not be located at the end of the array? I am missing something fundamental here... thx

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] {
"Apple shop" = www.apple.com;
"CS193P course" = cs193p.stanford.edu;
"Stanford Mall" = stanfordshop.com;
"Stanford University" = www.stanford.edu;
"Stanford on iTunes U" = itunes.stanford.edu;
}
2009-07-02 09:35:12.758 WhatATool[6407:10b] Mutable string example
2009-07-02 09:35:12.759 WhatATool[6407:10b] another mutable string
2009-07-02 09:35:12.760 WhatATool[6407:10b] itunes.stanford.edu
2009-07-02 09:35:12.760 WhatATool[6407:10b] Stanford on iTunes U
2009-07-02 09:35:12.761 WhatATool[6407:10b] stanfordshop.com
2009-07-02 09:35:12.762 WhatATool[6407:10b] Stanford Mall

+8  A: 

I think you're missing the nil necessary as the last argument when you create your NSMutableArray using the convenience method. Does

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

work?

Jeff Hellman
thanks heaps, save me some frustration - easy to miss
+2  A: 

Turn on warnings (other warning flags "-Wall") and you'll get:

warning: missing sentinel in function call

on the end of your NSMutableArray arrayWithObjects method to tell you about the missing nil.

Peter N Lewis
I love it when -Wall saves the day. Although it often produces tons of spewage that just gets ignored, there are times when it really shines...
Quinn Taylor
"tons of spewage"? I run permanelty with -Wall and "treat warnings as errors" (which avoids the warnings sneaking through when you do a build and then no seeing them again). My code compiles cleanly with no warnings. I guess if you're stuck using crappy third party code that could be a problem, but other than that, just fix the warnings!
Peter N Lewis