views:

790

answers:

3

If i try to use this simple code:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    // Add the tab bar controller's current view as a subview of the window
    [window addSubview:tabBarController.view];

    NSString *stringMer = [NSString stringWithFormat:@"OK COOL"] ;
    NSString *stringMer2 = [NSString stringWithFormat:@"OK COOL"];

    NSArray *truc = [NSArray arrayWithObjects:stringMer,stringMer2];
}

My application crashes ("unable to read unknown load command 0x22" or just a regular crash)... What the applicationDidFinishLaunching is from my FooAppDelegate and i have no more code, is this normal ?

+36  A: 

The list of arguments passed to the arrayWithObjects: method must be nil-terminated:

NSArray *truc = [NSArray arrayWithObjects:stringMer,stringMer2, nil];
Perspx
Note that GCC will warn you should you forget the `nil` if you compile with `-Wformat` - check out [GCC's `sentinel` function attribute][sentinel] and the header `<Foundation/NSObjCRuntime.h>` for `NS_REQUIRES_NIL_TERMINATION`. You can also make the build fail for warnings by specifying `-Werror`. [sentinel]: http://developer.apple.com/mac/library/documentation/DeveloperTools/gcc-4.0.1/gcc/Function-Attributes.html "Function Attributes"
Jeremy W. Sherman
+7  A: 

Don't use +stringWithFormat: unless you actually have a format string that needs parsing.

NSResponder
+2  A: 

NSResponder is right - don't get sloppy by defaulting to stringWithFormat. Perspx also pointed out a fairly obvious (but easily forgotten) error with the missing nil.

I'd be a bit more explicit -

(in the .h)

NSArray *truc; (assuming it won't be a property)

(in the .m)

//actually, I'd define, "OK COOL" as a string and init with that, but...
    NSString *stringMer = [[NSString alloc] initWithString:@"OK COOL"] ;
    NSString *stringMer2 = [[NSString alloc] initWithString:@"OK COOL"]; 


    truc = [[NSArray alloc] initWithObjects:stringMer,stringMer2, nil];

//appease the memory gods
    [stringMer release];
    [stringMer2 release];

(then, down in dealloc)

[truc release];

It would be good to get really familiar with Instruments - run for Leaks.

inked