Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Is it better to use getter/setter methods directly or properties

I know that properties kind of encapsulate getter and setter methods. So whenever we say myObject.property1 we actually cause to call [myObject property1]. From Apple documentation, Stanford iOS Courses and sample codes I can see that the usage of properties are encouraged. I aggree that using properties make a code look better and more understandable but what about performance? If I write a huge application will using properties have a noticableimpact on performance? Do professionals generally prefer direct setter and getter methods or properties?

like image 272
Mikayil Abdullayev Avatar asked Feb 21 '23 00:02

Mikayil Abdullayev


2 Answers

There is no difference in performance when you use the bracket notation ([myObject property1]) or the . notation (myObject.property1).

This is more of a coding style than any thing else, so use the notation you are comfortable with or the same notation as your team if you don't work alone.

like image 180
sch Avatar answered May 17 '23 07:05

sch


Properties are probably better because they automatically generate the methods for you and when you synthesize them you can do it like this:

@synthesize property = _property

To avoid any confusion

Also you can choose different functions/methods like:

           (nonatomic, retain) // or (readonly) etc.

It also handles the memory better

like image 27
MCKapur Avatar answered May 17 '23 08:05

MCKapur