OC _ Properties

Source: Internet
Author: User

Main.m

Oc04_ Property

//

Created by Dllo on 15/7/17.

Copyright (c) 2015 CML. All rights reserved.

//

#import <Foundation/Foundation.h>

#import "Person.h"

#import "Student.h"

#import "Animal.h"

int main (int argc,constchar * argv[]) {

Person *per =[[person alloc] init];

object to be repaired by the content of the member variable through the

[per setname:@ "Hello"];

object is evaluated by accessors

NSLog (@ "%@", [per name]);

Properties have done three things altogether

1. The set, setter, and accessor getter are declared

2. Implements the setup and accessor

3. A member variable is declared, and the name of the member variable is preceded by an underscore in the property name

Specific data storage, or member variables to complete, properties just help the programmer to do some tedious work, simplify the code

Person *per =[[person alloc] init];

[Per hobby];

[Per Buhao];

Student *stu=[[student alloc] init];

[Stu setstuname:@ "Eagle King"];

NSLog (@ "%@", [Stu Stuname]);

For object properties, we can use point syntax to get the contents of an object, or to set

[Email protected] "Cui MoU";

NSLog (@ "%@", stu.stuname);

With the dot syntax, you can manipulate the attributes, saving a lot of code

Near the = number is setter method, the rest is getter method

KVC

Key-value-coding

Consider the attribute name as a key in the KVC, the value to be modified as value, and then through the KVC method, assign the value to the specified key

Student *stu=[[student alloc] init];

[Stu setvalue:@ "Cui MoU" forkey:@ "Stuname"];

NSLog (@ "%@", stu.stuname);

NSLog (@ "%@", [Stu valueforkey:@ "Stuname"]);

//

[Stu setvalue:@ "5756" forkey:@ "Name"];

//

NSLog (@ "Name =%@", Stu. Name);

NSLog (@ "name =%@", stu.name);

//

Alloc Init

Animal *animalone =[[animalalloc]initwithcolor:@ "Red" size:20age:20 type:@ "dog"];

Modify and set the value of an object by point syntax

[Email protected] "yellow";

ID animaltest = [animalanimalwithcolor:@ "Grey" Size:3age:5 type:@ "DG"];

NSLog (@ "idtest =%@", [Animaltestcolor]);

Convenience Builder

Animal *animaltwo=[animalanimalwithcolor:@ "Orange" Size:10age:10 type:@ "cat"];

[Email protected] "bird";

NSLog (@ "%@", Animaltwo.color);

KVC

[animalonesetvalue:@ "white" forkey:@ "color"];

NSLog (@ "%@", [animalonevalueforkey:@ "color"]);

Return0;

}

Person.h

Oc04_ Property

//

Created by Dllo on 15/7/17.

Copyright (c) 2015 CML. All rights reserved.

//

#import <Foundation/Foundation.h>

@interface Person:nsobject

Property

@property NSString *name;

@property NSString *sex;

@property Nsinteger age;

Properties of the property

1. Read/write Control: Readonly,readwrite

After the ReadOnly is set, the property has no setter and defaults to ReadWrite

The role of setter and Getter is to re-name the setup and accessor methods, note: Do not forget to add when setting the settings:

@property () NSString *hobby;

The second part of the atomic control

Through the atomic atomic to monitor the physical in the whole process is not completed, but generally speaking we are the simple assignment of data, generally we use this part nanatomic

@property (atomic) NSString *sex;

Part Three semantic settings

Copy, assign, retain

Retain are generally used by object types, such as the classes we write ourselves, and Nsarray will use

Assign generally nsinteger,cgfloat will be used because they are in the stack area and do not require memory management, so use asign

Copy generally only strings are used by copy

@property (nonatomic, copy) NSString *color;

@property (nonatomic, assign) Nsinteger age;

@property (nonatomic, assign) CGFloat score;

@property (nonatomic, retain) Nsarray *arr;

-(void) sayhi;

@end

Person.m

Oc04_ Property

//

Created by Dllo on 15/7/17.

Copyright (c) 2015 CML. All rights reserved.

//

#import "Person.h"

@implementation person

Implementation of attributes

Keyword @sythesize, preceded by the attribute name, followed by the corresponding member variable

zaiXCode4.5 after the beginning of writing, so before the old program, there are a lot of @synthesize

@synthesize name = _name;

-(void) sayhi{

NSLog (@ "%@", _name);

}

@end

Animal.h

Oc04_ Property

//

Created by Dllo on 15/7/17.

Copyright (c) 2015 CML. All rights reserved.

//

#import <Foundation/Foundation.h>

@interface Animal:nsobject

Four properties

@property (nonatomic, copy) NSString *color;

@property (nonatomic, assign) cgfloat size;

@property (nonatomic, assign) Nsinteger age;

@property (nonatomic,copy) NSString *type;

Custom initialization

-(ID) Initwithcolor: (NSString *) color

Size: (cgfloat) size

Age: (Nsinteger) Age

Type: (NSString *) type;

Convenience Builder

+ (ID) Animalwithcolor: (NSString *) color

Size: (cgfloat) size

Age: (Nsinteger) Age

Type: (NSString *) type;

@end

animal.m

Oc04_ Property

//

Created by Dllo on 15/7/17.

Copyright (c) 2015 CML. All rights reserved.

//

#import "Animal.h"

@implementation Animal

-(ID) Initwithcolor: (NSString *) color

Size: (cgfloat) size

Age: (Nsinteger) Age

Type: (NSString *) type{

Self=[superinit];

if (self) {

_color=color;

_size=size;

_age=age;

_type=type;

}

return self;

}

+ (ID) Animalwithcolor: (NSString *) color

Size: (cgfloat) size

Age: (Nsinteger) Age

Type: (NSString *) type

{

Animal *animal =[[animalalloc]initwithcolor:colorsize:sizeage:agetype:type];

return animal;

}

@end

Student.h

Oc04_ Property

//

Created by Dllo on 15/7/17.

Copyright (c) 2015 CML. All rights reserved.

//

#import <Foundation/Foundation.h>

#import "Person.h"

@interface Student:nsobject

@property (nonatomic,copy) NSString *name;

@property (nonatomic,copy) NSString *name;

1. Student's name

@property (nonatomic, copy) NSString *stuname;

2. Age of Student

@property (nonatomic, assign) Nsinteger stuage;

3. Student's achievements

@property (nonatomic, assign) CGFloat Stuscore;

4. Students have a person's attributes

@property (nonatomic, retain) person *per;

5. Students have an array of properties

@property (nonatomic, retain) Nsarray *arr;

6.

@property (readonly,getter=isselected,nonatomic,assign) BOOL selected;

@end

Student.m

Oc04_ Property

//

Created by Dllo on 15/7/17.

Copyright (c) 2015 CML. All rights reserved.

//

#import "Student.h"

@implementation Student

@end

OC _ Properties

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.