Search code examples
objective-calertview

Objective-C: initWithTitle:@"" and alertBox.title = @""


Is there a difference between these two and which one may be better or faster or have any advantage between:

UIAlertView * alertBox = [[UIAlertView alloc] initWithTitle:@"Title"];

And:

UIAlertView * alertBox = [[UIAlertView alloc] init];
alertBox.title = @"Title";

(They both display the same result of course!)


Solution

  • There is not really a difference in performance, one is setting the title on initialization, which really just calls the line of code alertBox.title in the custom initialization method. The reason they have the .title property is so you can change it.

    So this code:

    UIAlertView * alertBox = [[UIAlertView alloc] initWithTitle:@"Title"];
    

    Would be better vs this code:

    UIAlertView * alertBox = [[UIAlertView alloc] init];
    alertBox.title = @"Title";
    

    Just really because of the number of lines, but you can just use this code:

    alertBox.title = @"New Title";
    

    Later on to change it


    If there is a difference in speed, you would need a planck second calculator to measure it :) - Some good hyperbole there, but basically no

    For memory, there isn't a difference because you are intializing an object and setting it's title parameter in both cases, just separate ways of doing it. Think of the first one, as a shorter way for you to write it, but the actual class will basically do the same thing.

    Using the constructer custom initializer method is more efficient to your time and number of lines, but to nothing else -- my verdict


    Which one is more to an advantage? Neither, because you are using an alert view, I hate them!