Day4 oc point syntax

Use of oc dot syntax

(From http://www.cnblogs.com/wendingding/p/3705658.html)

 

#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        // insert code here...
        Person *person = [[Person alloc] init];
        
        //[person setAge:10];
        person.age = 10;//dot syntax, equivalent to [person setAge:10];
//This is not to assign a value to the property of person, but to call the setAge method of person
        
        //int age = [person age];
        int age = person.age; // etc. Effective int age = [person age]
       NSLog(@"age is %i", age);
        [person release];
        
    }
    return 0;
}

Another example is Person.h in the dog walking

#import "Dog.h"
@interface Person:NSObject
{
     Dog *_dog;
}
//原-(void)setDog:(Dog *)aDog;
//-(Dog *) dog;
@property(retain) Dog *dog;
@end

 Person.m

#import "Person.h"
@implementation Person
@synthesize dog=_dog;
/*
-(void)setDog:(Dog *)aDog
{
   if(aDog!=_dog){
     [_dog release];
       _dog =[aDog retain];
     }
}
-(Dog *)dog
{
    return _dog;
}
*/
-(void)dealloc
{
   //[_dog release];
   //_dog=nil;
   self.dog = nil;
   //[self setDog:nil];
   [super dealloc];
}
@end

 

The essence of oc dot syntax

 

The essence of dot syntax is method invocation, rather than accessing member variables. When dot syntax is used, the compiler will automatically expand into the corresponding method. It is not the same as the point of using object variables to directly access variables of public type in Java.

There are two conditions for using dot syntax in Objective-C:

1. Must be an attribute

2. There are corresponding get/set methods

Stu.age=10; expand to: [stu setAge:10];

int a=stu.age; expands to: [stu age];

The get method in OC is not the same as in Java. As long as a method in OC has a return value, it can be considered as a get method.

#import <Foundation/Foundation.h>

@interface Person : NSObject{
    //The dot syntax can only be applied to the set/get method. If there is no corresponding set/get method, it cannot be used. Syntax
    //The get method here is different from other languages, as long as there is a return value, it is the get method
    NSString *_name;
    NSInteger _age;
}

- (void)setName:(NSString *)name;
- (void)setAge:(NSInteger)age;

- (NSString *)name;
- (NSInteger) age;

//It should be noted here that showInfo is also a get method
- (NSString *)showInfo;

//Only defined, not implemented, so when calling [person test], no error will be reported when compiling
//But it will report an error when running, because when compiling, it only judges whether the method is defined, and when running, it needs to judge whether the method is implemented or not.
- (void)test;

@end

 

#import "Person.h"

@implementation Person

- (void)setName:(NSString *)name{
    _name = name;
    NSLog(@"setName is Execute");
}
- (void)setAge:(NSInteger)age{
    //Note here, if the global variable we define is age instead of _age
    //You can't use age = age for assignment here
    //But you can't use self.age = age, because self.age is equivalent to [self setAge:age], so there will be an infinite loop
    //So we usually add an underscore when defining global variables to distinguish
    _age = age;
    NSLog(@"setAge is Execute");
}
- (NSString *)name{
    NSLog(@"getName is Execute");
    return _name;
}
- (NSInteger) age{
    NSLog(@"getAge is Execute");
    return _age;
}
- (NSString *)showInfo{
    NSLog(@"showInfo is Execute");
    return _name;
}

@end

 

oc dot syntax attribute

(Excerpted from http://www.cnblogs.com/X-Code/archive/2013/01/17/2865165.html)

 

In oc, the set and get methods of variables are automatically generated by pairing @property and @synthesize

 

@interface Person:NSObject
{
      int _age;
      int _height;
}
@property int age;
//1. Equivalent to the following two sentences, you can write multiple variables such as @property int age, height
//2, can only be written in @interface
//- (void)setAge:(int)age;
//- (int)age;
@end
 

 

 

@implementation
{
@synthesize age=_age;
//1. Equivalent to the following, you can write multiple variables such as @synthesize age=_age,height=_height;
//2. The above int _age can not be written. When the access _age does not exist, the _age variable of type @private will be automatically generated
//3. After xcode4.4, @synthesize can also be omitted, just write @property. At this time, member variables are also private. If you want member variables to be protected, add @protect int _age;
//4, if written as @synthesize age; will access age by default
//5, can only be written in @implementation
//6. If the setter method is manually implemented, the compiler will only generate the getter method...
//-(void)setAge:(int) age
//{
//_age= age;
//}
//-(int)age
//{
//return _age;
//}
}
@end
 

 

  • @property has the following properties

readwrite (default), readonly read-only (only get method, disable set method);

atomic: Enable multi-threaded variable protection, which will consume a certain amount of resources (non-atomic, to ensure multi-threaded safety), nonatomic: disable multi-threaded variable protection to improve performance;

assign (default): direct assignment, suitable for basic data types (non-object types), retain: memory optimization when assigning, suitable for object types, copy: copy a copy, suitable for special object types (commonly used in NSstring) (copy can only be used if there is an NSCoping protocol)

 

  • The internal implementation of the setter method of assign retain copy

assign:

@property float price;

Internal implementation:

- (void)setPrice:(float)price
 {
       _price = price;
}
-(float)price
{
       return _price;
}

 

 

retain:

@property (retain, nonatomic) NSString *company;

Internal implementation:

 

- (void)setCompany:(NSString *)company{
      if(_company != company){
        [_company release];
        [company retain];
        _company = company;
     }
}

 

 

copy:

@property (copy, readwrite, nonatomic) NSString *company;

Internal implementation:

 

- (void) setCompany:(NSString *)company{
      if(_company != company){
           [_company release];
           [company copy];
           _company = company;
      }
}

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327001350&siteId=291194637