Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"getter" keyword in @property declaration in Objective-C?

I noticed some code example in Apple's documentation shows the following style when declaring the property:

@property (nonatomic, getter=isActivated) BOOL activated;

I understand it allows you to specify a certain name for your getter method. I'd like to know what is the reason and advantage to use this style.

Will I be able to use the dot notation to get the value (e.g. BOOL aBool = someObject.isActivated)? Or should I use
[someObject isActivated]; to access the property? Thanks!

like image 459
saurb Avatar asked Aug 26 '11 21:08

saurb


People also ask

Can Objective-C objects be used as C++ template parameters?

C++ "by value" semantics cannot be applied to Objective-C objects, which are only accessible through pointers. An Objective-C declaration cannot be within a C++ template declaration and vice versa. However, Objective-C types (e.g., Classname *) can be used as C++ template parameters.

What are stored and computed properties in JavaScript?

Stored and computed properties are usually associated with instances of a particular type. However, properties can also be associated with the type itself. Such properties are known as type properties. In addition, you can define property observers to monitor changes in a property’s value, which you can respond to with custom actions.

What is the default return type in Objective-C?

The default return type is the generic Objective-C type id . Method arguments begin with a name labeling the argument that is part of the method name, followed by a colon followed by the expected argument type in parentheses and the argument name. The label can be omitted.

How are objects represented in Objective-C?

In Objective-C, all objects are represented as pointers, and static initialization is not allowed. The simplest object is the type that id ( objc_obj *) points to, which only has an isa pointer describing its class. Other types from C, like values and structs, are unchanged because they are not part of the object system.


1 Answers

No, the getter keyword only changes the method name. The idea is that you'll access the property just like a variable:

if (self.activated) { ... }
self.activated = YES;

But when you're sending a message to the object, it's readable code: if ([self isActivated]) { ... }.

like image 191
jtbandes Avatar answered Nov 07 '22 19:11

jtbandes