Question

I have been coding in objective c for a while now, but I still wonder at times if there is a BEST or FASTEST way to do something.

For example let's say I had a UILabel named myLabel and UIViewController called mainView, what is the FASTEST or BEST way to set it.

Method 1

mainView.myLabel.text = @"String"; //similar to c/c++ style

Method 2

[mainView.myLabel setText:@"String"]; //between c/c++ and obj-c style

Method 3

[[mainView myLabel] setText:@"String"]; //obj-c style

I personally prefer Method 2, because isn't Method 1 just a shorthand method to access it? Thanks for your help.

Était-ce utile?

La solution

All 3 options you listed result in calling the same code (myLabel and setText: methods), so there's absolutely no performance difference.

So the best way will be to choose whatever single option you like the most and use it consistently through whole your project.

Autres conseils

They compile to exactly identical code. There's no efficiency penalty whatsoever. Use whichever your like.

Option 1 involves the least typing. Option 3 best describes what actually happens (you can't mistake property access for struct element access, for example).

I have added another way, just for reference, and all peers said, all these are similar, and no performance issue....

Method 1

mainView.myLabel.text = @"String"; //similar to c/c++ style

Method 2

[mainView.myLabel setText:@"String"]; //between c/c++ and obj-c style

Method 3

[[mainView myLabel] setText:@"String"]; //obj-c style
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top