Objective C - application crashes while creating array of strings, am I crazy?

If I try to use this simple code:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    // Add the tab bar controller 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 ("it is impossible to read the unknown 0x22 boot command" or just a normal failure) ... What is applicationDidFinishLaunching from my FooAppDelegate and I no longer have code, is this normal?

+3
source share
3 answers

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

NSArray *truc = [NSArray arrayWithObjects:stringMer,stringMer2, nil];
+37
source

Do not use + stringWithFormat: unless you actually have a format string that needs parsing.

+9

NSResponder - sloppy stringWithFormat. Perspx ( ) .

-

( .h)

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

( .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 to dealloc)

[truc release];

It would be nice to become familiar with the tools - run for leaks.

+2
source

Source: https://habr.com/ru/post/1719209/


All Articles