Question

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!)

Was it helpful?

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!

OTHER TIPS

The former is probably slightly faster, however if you are optimising things like this, you are making a mistake. Showing an alert is something you do infrequently and immediately afterwards, you'll be hanging around waiting for the user to respond. There's no benefit in shaving a couple of microseconds off that, it will make absolutely no perceptible difference to how the application runs. Use whichever is clearer for you to read as a developer.

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

Uses the init method which is similar to constructor, which internally calls .title method.

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

First statement creates an object alertBox with title as nil. In the second line you supply yours own title.

For performance :

2nd one will create another stack to perform the second statement, so two extra cpu processing push and pop. There for first one will be faster but with multicore processors and the time of calling is really negotiable.

That first line is just less code for you to write out, both do the same thing, and are constructors for a UIAlertView. It's easier to do the constructor from the first line in my opinion because it's all there in one line instead of going through and adding each property in later.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top