Open In App

Properties in Objective-C

Last Updated : 12 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The object-oriented programming language Objective-C is largely used to create applications for Apple’s macOS and iOS platforms. It incorporates all of the characteristics of C while also providing more features for object-oriented programming, making it a superset of the C programming language. The use of properties is one of Objective-key C’s features. By encapsulating data and activity within an object, properties give other objects a regulated way to access and interact with that data. In other programming languages, instance variables are comparable to properties, but properties offer further features like automatic memory management and the capacity to specify unique getter and setter methods.

Syntax of Properties in Objective-C

Properties are used in Objective-C to declare instance variables and give users a means to access and change their values. The following is the syntax for declaring properties in Objective-C:

ObjectiveC




@interface MyClass : NSObject
  
@property (nonatomic, strong) NSString *myString;
@property (nonatomic, assign) int myInt;
  
@end


In this case, a property is declared using @property, and details about the property are provided in the parenthesis.

Attributes of Properties in Objective-C

Properties in Objective-C can be specified with a variety of attributes to change how they behave or manage memory. The following are the most typical attributes:

  1. Nonatomic: This characteristic indicates that the property should be accessed non-atomically, allowing for simultaneous access from many threads. Although it is not thread-safe, this can boost performance.
  2. Atomic: This characteristic indicates that the property should only be accessed once by a single thread, or in other words, atomically. Although it guarantees thread safety and is the default property, it may have an impact on performance.
  3. Strong: This attribute indicates that the property should keep the object and is used for objects. As long as the property is present, the object won’t be deallocated.
  4. Weak: This attribute indicates that the property should not keep the object and is also used for objects. This indicates that even if the property is still present, the object can be deallocated.
  5. Assign: This primitive type attribute instructs the property to merely assign the value to the instance variable. This indicates that memory management is not necessary.
  6. Readonly: This attribute instructs the system not to build a setter method for the property. This indicates that the property can only be set once, during initialization, and cannot be modified afterward.
  7. Read-write: The getter and setter methods for the property should be produced, according to the read-write attribute. The default attribute is this one.
  8. Copy: This attribute, which is used for objects, instructs the property to make a copy of the object instead of just keeping it. This makes sure that outside code cannot change the value of the attribute.
  9. Getter: This attribute is used to define a unique name for the property’s getter method.
  10. Setter: This attribute is used to give the property’s setter method a unique name.

Example:

ObjectiveC




@property (nonatomic, strong) NSString *name;


In this example, the strong attribute is used to maintain the NSString object while also declaring the name property with the nonatomic attribute to increase efficiency. 

Example of how to use properties in Objective-C

ObjectiveC




@interface Person : NSObject
  
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger age;
  
- (void)introduce;
  
@end
  
@implementation Person
  
- (void)introduce {
    NSLog(@"Hi, my name is %@ and I'm %ld years old.", self.name, (long)self.age);
}
  
@end
  
int main(int argc, char *argv[]) {
    @autoreleasepool {
        Person *person = [[Person alloc] init];
        person.name = @"John";
        person.age = 30;
        [person introduce];
    }
    return 0;
}


In this example, a “Person” class with the fields “name” and “age” is declared. Age is a “assign” property of type NSInteger, and the name is a “strong” property of type NSString. Also, we declare the instance function “introduction,” which just prints the person’s name and age to the console.

We construct a new “Person” object and set its “name” and “age” attributes in the “main” method. The message is then recorded to the console by calling the “introduction” method on the “Person” object. As you can see, we call the getter and setter methods of the properties using a shortcut called the dot syntax. So, “person.name = @”John”” is the same as “[person setName:@”John”]”.

Output:

output

 

Example 2:

ObjectiveC




@interface Car : NSObject
  
@property (nonatomic, strong) NSString *make;
@property (nonatomic, strong) NSString *model;
@property (nonatomic, assign) NSInteger year;
  
- (instancetype)initWithMake:(NSString *)make model:(NSString *)model year:(NSInteger)year;
  
@end
  
@implementation Car
  
- (instancetype)initWithMake:(NSString *)make model:(NSString *)model year:(NSInteger)year {
    self = [super init];
    if (self) {
        self.make = make;
        self.model = model;
        self.year = year;
    }
    return self;
}
  
- (void)drive {
    NSLog(@"I'm driving my %@ %@ from %ld.", self.make, self.model, (long)self.year);
}
  
@end
  
int main(int argc, char *argv[]) {
    @autoreleasepool {
        Car *car = [[Car alloc] initWithMake:@"Honda" model:@"Civic" year:2020];
        [car drive];
    }
    return 0;
}


The make, model, and year attributes of the ‘Car’ class are the three that are declared in this example. Make, Model and Year are “strong” properties of type “NSString” and “assign” properties of type “NSInteger,” respectively. We also create a special initializer called “initWithMake:model: year,” which initializes properties by accepting values for make, model, and year.

Also, we declare the instance function “drive,” which logs the make, model, and year of the vehicle to the console as a message. We use the ‘initWithMake:model: year’ method to create a new Vehicle object in the main function and pass values for the properties make, model, and year‘. To log the message to the console, we next use the ‘drive’ function on the ‘Car’ object.

Output:

output

 

Conclusion

Properties are a strong component of Objective-C, offering a practical and effective way to access the information and behavior of an object. You can build more reliable and effective Objective-C code by being aware of its syntax, properties, and usage.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads