Protocol, category, time, extension, and attribute in oC

Source: Internet
Author: User

You must accept the marryprotocol protocol, and ID <marryprotocol> can assign values to instance variables only based on the Type limitation.

@ Property (nonatomic, assign) ID <marryprotocol> husband;

Avoid functionality not implemented by optitonal

If ([_ husband respondstoselector: @ selector (driver)]) {

[_ Husb and driver];

}

@ Required // required protocol, default Protocol

-(Void) seegril;

@ Optional // optional Implementation Protocol

-(Void) seeyou;

A class can comply with multiple protocols. It is used after the parent class <>

Define category objectives (category name)

@ Interface nsstring (sayhello)

Extend

Extension is a special category.

Just move the Class Object declaration to the. M file

The main function of extension is to manage the "private" method.

Instance variables can be defined in the extension, but they are private and cannot be inherited by the quilt class.

Attributes can be defined in the extension.

 

Obtains the current time, but the printed date is the Greenwich Mean Time.

Nsdate * Date = [nsdate date];

Used for mutual conversion between date object nsdate and string

Nsdateformatter * formattter = [[nsdateformatter alloc] init];

HH is set to 12, HH is 24, and A is in the morning, G is in the morning, and E is in the week.

Category

A category (category, category) can only be extended to known classes, and instance variables cannot be added.

The method to add a category will become part of the original class, with the same level, it will inherit from the quilt class.

Instance variables cannot be defined in a category

 

 

Attribute is a pair of methods, attribute operation instance variables

[P setage: 24]; // as long as there is a setter, it is equivalent to the following method.

P. Name = @ "James ";

P. Sex = @ "male ";

P. Age = 24;

To the left of the equal sign, call the setter method.

To the right of the equal sign, call the getter method.

If no equal sign is used, call the getter method.

If you need to change the method name (setter = xxx:, Getter = ooo );

Readonly indicates that the read-only feature is new and cannot be modified. Only getter does not have a setter.

By default, readwrite is generated, including setter and getter.

Equivalent to creating

-(Void) setname :( nsstring *) Name;

-(Nsstring *) Name;

@ Property (nonatomic, copy) nsstring * sex;

The default atomicity attribute is atomic, which ensures the security of accessing instance variables in the case of multithreading (essentially adding a thread lock in the setter method)

Non-atomicity, nonatmiic (data security is not guaranteed in the case of multithreading)

Setter semantic features, assgin, retain, copy

The three differences may lead to different internal implementations of the generated setter method.

Assgin directly assigns values, while retain and copy perform memory optimization.

Principle 1: the basic data type must use assgin

Principle 2: retain can be used for all object types

Principle 3: only objects in the <nscoping> protocol can be accepted to write a copy.

Principle 4: assgin by default

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.