Objective-c Getting Started Tutorial 03: Properties (@property and @synthesize)

Source: Internet
Author: User



In Java, in particular a standard Pojo class, we define some properties and then generate the appropriate getter and setter for each property. For example:


Package com.demo;

/**
  * Mobile phone
  * @author liuzc
  */
Public class Phone {

Private String color; //color
Private String os; //system
Private String brand; //brand

/******* Getter & Setter *******/
Public String getColor() {
Return color;
}
Public void setColor(String color) {
This.color = color;
}
Public String getOs() {
Return os;
}
Public void setOs(String os) {
This.os = os;
}
Public String getBrand() {
Return brand;
}
Public void setBrand(String brand) {
This.brand = brand;
} 


In Objective-c, the property of a class is protected by default, that is, only the class and its subclasses can access it, and if you want to use it externally, you need to add a setter/getter to it. Using objective-c, we can rewrite the above code to read:


 
 
@implementation Phone{
    NSString *color;
    NSString *brand;
    NSString *os;
}
-(NSString*) color{
    return color;
}
-(NSString*) brand{
    return brand;
}
-(NSString*) os{
    return os;
}
-(void) setColor:(NSString*) _color{
    color=_color;
}
-(void) setBrand:(NSString*) _brand{
    brand=_brand;

}
-(void) setOs:(NSString*) _os{
    os=_os;
}

@end


It is important to note that in Objective C, get has a special meaning, so the Getter method uses the property name directly instead of using get and then adding the property name. The above is just three properties, if there are 10, 20 attributes, do you need to write a lot of code?



Objective C 2.0 provides us with @property and @synthesize. It greatly simplifies the process of creating read and write functions for data members, and more crucially it provides a more concise and understandable way to access data members.



The above code can be simplified to:



Phone.h


 
@interface Phone : NSObject

@property NSString *color;
@property NSString *brand;
@property NSString *os;

@end


Phone.m


 
#import "Phone.h"

@implementation Phone

@synthesize color;
@synthesize brand;
@synthesize os;

@end
Syntax for @property:





Where attribute has the following several values, the meaning of each attribute related to objective-c memory management knowledge, the following will be explained in detail, so here is just a brief introduction. Only objective-c memory management with a more comprehensive understanding , in order to understand the meaning of the various attribute here very well.



attribute is divided into three main categories:


    • Read-Write properties: (readwrite/readonly) Determines whether a set accessor is generated
    • The semantics of the setter Semantics (assign/retain/copy) set accessor, which determines how the data member is given a new value.
    • Atomicity: (atomic/nonatomic)


readwrite: Generate Setter\getter Method (default)



ReadOnly: Only getter methods are generated.



This tag indicates that the property is read-only, and if you specify ReadOnly, only one getter is required in @implementation. Or if you use the @synthesize keyword, only the Getter method will be generated. If you try to assign a value to a property using the dot operator, you will get a compilation error. The ReadOnly keyword means that the setter will not be generated, so it cannot be used in combination with copy/retain/assign.



Assign: simple assignment, not changing index count



This flag indicates that the setting is assigned directly, which is also the default value. In an application that uses garbage collection, if you want a property to use assign, and this class conforms to the nscopying protocol, you should explicitly indicate this tag instead of simply using the default value, otherwise you will get a compile warning. This again explains to the compiler that you really need to assign a value, even if it is a copy.



retain: Frees the old object, assigns the value of the old object to the input object, and increments the input object's index count to 1



Specifies that retain will wake the retain message of the incoming value when the value is assigned. This property can be used only for Objective-c object types, not for core Foundation objects. (for obvious reasons, retain increases the object's reference count, and neither the base data type nor the core Foundation object has a reference count).



copy: Create an object with an index count of 1 and then release the old object



It indicates that a copy of the passed-in value is used when the value is assigned. Copy work is performed by the copy method, and this property is valid only for those object types that implement the Nscopying protocol. For a more in-depth discussion, refer to the "Copying" section.



Atomic/nonatomic:



Indicates that the accessor is not an atomic operation, atomic indicates that the attribute is atomic, supports multi-threaded concurrent access, and by default nonatomic, the accessor is an atomic operation. This means that, in a multithreaded environment, the parsed accessor provides a secure access to the property, the return value obtained from the picker, or the value set by the setting can be done at once, even if another thread is accessing it. If you do not specify nonatomic, the parsed accessor retains and automatically releases the returned value in the environment in which it manages the memory, and if nonatomic is specified, the accessor simply returns the value. There are no special multithreading requirements to recommend using Nonatomic to help improve performance.



After iOS5 introduced automatic reference calculation (ARC), the object variable properties add strong and Weak,strong similar to retain, which can be said to replace retain.


Access modifiers


OBJECTIVE-C provides 3 instructions to control access to an instance variable of an object:



@protected: An instance variable decorated with this directive can be accessed directly by the method determined by that class and any subclass, which is the default.
@private: Instance variables decorated with this directive can be defined directly in the methods of the class, but cannot be accessed directly by the methods defined in the class.
@public: Instance variables decorated with this directive can be accessed directly by methods in the class, or by methods defined by other classes.






The @package keyword was newly added to the Mac 10.5 Objective-c runtime to support the 64-bit system.



@packageis a new instance variable protection class, like@publicand@protected.@packageinstance variables behave as follows:


    • @publicIn 32-bit;
    • @publicIn 64-bit, inside the framework of that defined the class;
    • @privateIn 64-bit, outside the framework of that defined the class.


In 64-bit, the instance variable symbol for a@packageIvar is not exported, so any attempt to use the Ivar from outside the Framework that defined the class would fail with a link error.






Objective-c Getting Started Tutorial 03: Properties (@property and @synthesize)


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.