@property properties in Ui-ios development weak nonatomic strong readonly and other introduction

Source: Internet
Author: User

@property and @synthesize are paired, and you can automatically generate access methods for a class member variable. In Xcode4.5 and later versions, @synthesize can be omitted.

1.atomic and Nonatomic
Atomic: The default is the property, this property is to ensure that the program in multi-threaded situations, the compiler will automatically generate some mutex lock code, to avoid the variable read and write unsynchronized problems.
nonatomic: If the object does not need to consider multithreading, add this property, which will allow the compiler to generate a few mutually exclusive lock code, can improve efficiency.

2.readwrite and ReadOnly
ReadWrite: This property is the default and will automatically generate accessors for you.
readonly: Only getter Generation does not have setter methods.
The real value of the two properties of ReadWrite,readonly , is not to provide a member variable access interface, but to control access to member variables.

3.strong and weak
Strong: Strong references, which we usually refer to, determine the survival of the object directly. If there is no reference to an object and the object no longer appears in the list, the object is freed from memory.
Weak: Weak references do not determine the survival of an object. Even if an object is held with countless weak references, it is cleared if no strong references point to it.
Strong and retain function similar,weak and assign similar, only when the object disappears after the weak will automatically turn the pointer to nil;

4.assign, copy, retain
Assign: The default type, the setter method is assigned directly, does not make any retain operation, does not change the reference count. Typically used to handle basic data types.
retain: Releases the old object (release), assigns the value of the old object to the new object, and then counts the new object reference to 1. I understand that as a copy of the pointer, copy the original pointer, release the contents of the object pointed to by the original pointer, and then point the pointer to the new object content.
copy: As with the retain process, release the old value first, and then copy the new object, Retaincount to 1. The mechanism introduced to reduce dependency on the context. I understand a copy of the content, request a piece of space into memory, assign the original object content to it, and make its reference count 1. Pay special attention to the Copy property: the object defined with the Copy property must conform to the Nscopying protocol and must implement the-(ID) Copywithzone: (Nszone *) Zone method.
can also be used directly:
Use assign: for underlying data types (nsinteger,cgfloat) and C data types (int, float, double, char, and so on)
Using copy: on NSString
Use retain: For other nsobject and its subclasses

5.getter Setter
Getter: Is the method name used to specify the Get method
Setter: Is the method name used to specify the set adapting
In the @property attribute, if this property is a bool value, usually we can use getter to define a name that we like, for example:
@property (nonatomic, assign, Getter=isvalue) boolean value; @property (nonatomic, assign, Setter=setisvalue) Boolean value; One, retain, copy, assign difference

1.
Suppose you allocate a piece of memory with malloc and assign its address to pointer a, and then you want pointer b to share the memory, so you assign a to (assign) B. At this point a
and b point to the same piece of memory, if a no longer need this memory, can I directly release it? The answer is no, because a does not know whether B is still using this memory, if a is released, then B is using this block
Memory will cause the program to crash out.

2.
Understand the assign problem in 1, then how to solve? The simplest method is to use a reference count (reference
counting), or the above example, we set a reference count for that memory, and when the memory is assigned and assigned to a, the reference count is 1. When assigning A to B, the reference count is increased to
2. If a no longer uses this memory, it only needs to subtract the reference count by 1, indicating that it no longer owns the memory. b The reference count is also reduced by 1 when the memory is no longer used. When the reference count becomes 0,
means that the memory is no longer referenced by any pointers, and the system can release it directly.

3. Above two points is actually the difference between assign and retain, assign is directly assigned, which may cause 1 problems, when the data is int, float and other native types, you can use assign. Retain as described in 2, using a reference count, retain causes a reference count plus 1, release causes a reference count minus 1, when the reference count is 0 o'clock, the Dealloc function is called and memory is recycled.

4. Copy is used when you do not want a and B to share a piece of memory. A and B each have their own memory.

5. Atomic and nonatomic are used to determine whether the getter and setter generated by the compiler are atomic operations. In a multithreaded environment, atomic operations are necessary, otherwise they may cause incorrect results. Adding the Atomic,setter function will change to the following:


if (Property! = newvalue) {
[Property release];
property = [NewValue retain];
}

Second, in-depth understanding (including Autorelease) 1. Retain after Count plus one. Alloc after the count is 1,release will call Dealloc destroy this object.
If retain, you need to release two times. It is usually necessary to assign a parameter to a member variable in method retain.
For example:
ClassA has setname this method:
-(void) SetName: (ClassName *) inputname
{
name = InputName;
[Name retain]; Here Retian, equivalent to [InputName retain],count equals 2
}
When called:
ClassName *myname = [[ClassName alloc] init];
[ClassA Setname:myname]; Retain count = = 2
[MyName release]; Retain Count==1, release name in ClassA's dealloc to really free memory.

2. Autorelease is more tricky and easily confused by its name. I want to emphasize here: Autorelease is not garbage collection, completely different from the GC in Java or. Net.
Autorelease and scopes do not have any relationship!
Autorelease principle:
A. Create a autorelease pool first
B. The object is generated from within this autorelease pool.
C. After the object is generated, call the Autorelease function, which simply marks the Autorelease pool and lets the pool remember to release the object in the future.
D. At the end of the program, the pool itself needs to be rerlease, and the pool will release each object marked as Autorelease once. If an object retain count is greater than 1 at this point, the object is still not destroyed.
The above example should be written like this:
ClassName *myname = [[[ClassName alloc] init] autorelease];//marked as Autorelease
[ClassA Setname:myname]; Retain count = = 2[myname release]; Retain Count==1, note that in ClassA Dealloc cannot release name, otherwise the release pool will release this retain count 0 object, this is not right. Remember one point: If this object is your alloc or new, you need to call release.  If you use Autorelease, release only once the retain has occurred (let retain count is always 1). 3 new markup in Xcode strong weak strong is used to decorate strongly referenced properties, corresponding to previous retainweak used to decorate weak references, corresponding to previous assign

@property properties in Ui-ios development weak nonatomic strong readonly and other introduction

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.