"Turn" objective-c syntax property detailed

Source: Internet
Author: User

1, Introduction:

The property is a objective-c keyword, used in pairing with @synthesize , to allow the compiler to automatically generate a method declaration with the same name as the data member. @synthesize is the implementation that is used to generate the corresponding declaration method.

Syntax format for 1.1 property:

@property (parameter 1, Parameter 2) type name;

Here are the main parameters, the following three kinds:

Setter/getter Method (assign/retain/copy)

Read-write properties (readwrite/readonly)

Atomicity (nonatomic)

1.2 Three ways to use

The assign/retain/copy represents the way in which the value is assigned.

the ReadOnly keyword means that the setter will not be generated, so it cannot be used in combination with Copy/retain/assign .

The default value for Atomicity is Atomic, and the Read function is atomic.

1.2.1 Copy/reain/assign in which to select a setter to determine how the property handles this property. The NSObject object is used in this way.

1.2.2 Some special objects, such as nssstring, use copy.

The 1.2.3 assign keyword represents the setter's direct assignment, rather than copying or preserving it. For basic data types, such as Nsinteger and cgfloat, or types that you do not directly own, such as delegates.

2, how to use property1.1 no property and a comparison of property

The header file defines obj. Use in. m files

[CPP]View Plaincopy
    1. #import <UIKit/UIKit.h>
    2. @interface Viewcontroller:uiviewcontroller
    3. {
    4. NSObject *obj;
    5. }
    6. @end
[CPP]View Plaincopy
    1. -(void) Viewdidload
    2. {
    3. [Super Viewdidload];
    4. Self.obj = nil;
    5. }

The prompt is not available.

Plus property

[CPP]View Plaincopy
    1. #import <UIKit/UIKit.h>
    2. @interface Viewcontroller:uiviewcontroller
    3. {
    4. NSObject *obj;
    5. }
    6. @property (Nonatomic,retain) NSObject *obj;
    7. @end

Compile can pass, run, crash, prompt error reason: '-[viewcontroller setobj:]: Unrecognized selector sent to instance 0x6b6c480

That's all we have. Implements setter methods.

Use the @synthesize keyword to implement getter and setter.

In the. m file

[CPP]View Plaincopy
    1. @implementation Viewcontroller
    2. @synthesize obj;
    3. -(void) Viewdidload
    4. {
    5. [Super Viewdidload];
    6. Self.obj = nil;
    7. }

Run, the program runs normally. The setter worked.

3. Code generated by @property and @synthesize keywords

What code did the @property and @synthesize keywords generate? We can also replace these keywords by implementing getter and setter on our own.

Comment out the code that corresponds to the two keywords

. h

[CPP]View Plaincopy
    1. #import <UIKit/UIKit.h>
    2. @interface Viewcontroller:uiviewcontroller
    3. {
    4. NSObject *obj;
    5. }
    6. @property (Nonatomic,retain) NSObject *obj;
    7. -(nsobject*) obj;
    8. -(void) Setobj: (nsobject*) NEWOBJ;
    9. @end

. m

[CPP]View Plaincopy
  1. @implementation Viewcontroller
  2. @synthesize obj;
  3. -(void) Viewdidload
  4. {
  5. [Super Viewdidload];
  6. Self.obj = nil;
  7. }
  8. -(nsobject*) obj{
  9. return obj;
  10. }
  11. -(void) Setobj: (nsobject*) newobj{
  12. if (obj! = newObj) {
  13. [obj release];
  14. obj = [newObj retain];
  15. }
  16. }

Run again, it can start normally. A description of the getter and setter he wrote replaced the property.

4, using three kinds of parameters comparison

@property (nonatomic, retain)nsobject *obj;

@property (nonatomic, retain, ReadWrite) nsobject *obj;

ReadWrite is the default behavior, so these two lines of code are equivalent

@property (retain) nsobject *obj;

@property (Atomic, retain) NSObject *obj;

Atomic is the default behavior, so these two lines of code are equivalent.

@property(Atomic, assign) int number ;

@property (atomic) int number;

@property int number ;

for int, atomic assign is the default behavior, so these three rows are equivalent.

@property nsobject *obj, is that OK? No, it's a warning .


Only the underlying data types such as int can be written like this. The object must be assigned the type of the value.

@property (retain) nsobject *obj; that's fine. When to use assign, when to use retain, and later to speak.

5, retainand copy experiments.

Use copy.

. h file

[CPP]View Plaincopy
    1. #import <UIKit/UIKit.h>
    2. @interface Viewcontroller:uiviewcontroller
    3. {
    4. NSString *string;
    5. }
    6. @property (nonatomic, copy) NSString *string;
    7. @end

. m file

[CPP]View Plaincopy
  1. @synthesize string;
  2. -(void) Viewdidload
  3. {
  4. [Super Viewdidload];
  5. NSString *str = [[NSString alloc] initwithformat:@"ABCD"];
  6. NSLog (@"str_point:%p%@ retaincount:%d", str, str, [str retaincount]);
  7. self.string = str;
  8. NSLog (@"string_point:%p%@ retaincount:%d", String, String, [string Retaincount]);
  9. }

Print results

2012-07-19 20:41:44.853 testproject1[1213:f803] str_point:0x6a8e0b0 ABCD retaincount:1

2012-07-19 20:41:44.854 testproject1[1213:f803] string_point:0x6a8e0b0 ABCD Retaincount:2

The memory address is the same, not like other text written in the same way, copied a copy of the memory, here with copy is also a shallow copy. Retain also +1

Using retain

[CPP]View Plaincopy
    1. #import <UIKit/UIKit.h>
    2. @interface Viewcontroller:uiviewcontroller
    3. {
    4. NSString *string;
    5. }
    6. @property (nonatomic, retain) NSString *string;
    7. @end

The printing results are:

2012-07-19 20:42:08.113 testproject1[1230:f803] str_point:0x6d3b8f0 ABCD retaincount:1

2012-07-19 20:42:08.114 testproject1[1230:f803] string_point:0x6d3b8f0 abcd Retaincount:2,

The result is the same as the copy above.

Note: After IOS5, added automatic Reference counting (ARC), iOS5 in the New keyword has strong, weak, unsafe_unretained.

Copyright statement: This article by http://blog.csdn.net/totogo2010/Original, welcome reprint share. Please respect the author Labor, reproduced when the statement and the author of the blog link, thank you!

"Turn" objective-c syntax property detailed

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.