IOS Learning Note--0004 (@property detailed)

Source: Internet
Author: User

Here is an example of a person class: @interface person:nsobject{...} @property (nonatomic,copy) nsstring* name; @end @property did three things 1. In curly braces (such as curly braces), declare variables, add _ before variables, here is _ Name2. Generate the corresponding Get method 3. The corresponding set method can be generated in the implementation file, overriding the Get,set method here: If the Get,set method is written by itself, then the variable declaration in the curly braces also needs to write the I'll have to mention it when I write here.. Syntax. SyntaxThe premise is that a set of get methods is required, although the Circle class does not have the length property, but it can be used in an instantiated object..To access the eg:
#import<Foundation/Foundation.h>#definePI 3.14159@interfaceCircle:nsobject
@property (nonatomic)floatR;-(float) length;-(void) SetLength: (float) length;
@end
#import "Circle.h"@implementationCircle-(float) length{returnSELF.R *2*PI;}-(void) SetLength: (float) length{SELF.R= Length/(2*PI);}@end

PS: In the constructor, try not to use attributes, you should go directly to manipulate variables such as:
-(instancetype) init{    if (self  = [Super init])    {        @ "AAA ";         10000;     // This underlined variable is just a variable of this class, the parent variable is inaccessible, to access the parent class of the variable, to the same line     }    return self ;}
the difference between several keywords:
1. Readability: readonly, ReadWrite (default)@property (ReadWrite,....) valueType value; This property is the default property of a variable, and by adding the ReadWrite property your variable will have a Get method, and a set method. @property (readonly,...) ValueType value; This property variable means that the variable is only a readable method, that is, you can only use its Get method. 2.assign (default)Setter methods are directly assigned, do not perform any retain operations, in order to solve the original type and circular reference problem application: to the underlying data type (for example, nsinteger,cgfloat) and C data type (int, float, double, char, etc.), Objects that do not have ownership relationships, such as common delegate. 3.retainThe setter method releases the old value of the parameter and then retain the new value to the strong, uses the reference count, retain+1,release-1; When the reference count is 0 o'clock, the memory is freed, as shown in the following code:
-(void) SetName: (nsstring*) _name{       // First determine if it is consistent with the old object, if the assignment is inconsistent.        // because if it is an object, the code in the if will cause an extreme situation: when the name of the retain is 1 o'clock, so that the set operation let the instance name early release, and do not reach the assignment purpose.       if (Name! = _name) {            [name release]            ; = [_name retain];       }  }
4.copySimilar to strong, but the difference is that copy is two pieces of memory, and strong is a pointer to the object, which is a memory application: Nsstring,nsarray, will be decorated with copy (here is an example to illustrate: if the nsmutablestring to NSString, operation of the same memory, nsmutablestring change will affect NSString, is risky) 5.strong and Weakis a new object variable attribute introduced by ARC, ARC introduces a new object for the new life-cycle qualification simple speaking strong equals retain
Weak more than assign a function, when the object disappears automatically turn the pointer to nil, the benefits are self-evident 6.atomic (default) and NonatomicAtomic:atomic means that the operation is atomic, meaning that only one thread accesses the instance variable. The atomic is thread-safe, at least in the current accessor. It is a default feature, but is seldom used because the comparison affects efficiency. Nonatomic:nonatomic and Atomic just the opposite. Represents a non-atomic and can be accessed by multiple threads. Its efficiency is faster than atomic. However, there is no guarantee of security in a multithreaded environment, which is widely used in the case of single-threaded and explicitly only one thread access.

IOS Learning Note--0004 (@property detailed)

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.