iOS Development Review notes (1)-OC Basics

Source: Internet
Author: User
Tags uppercase letter

Learning iOS in the spare time has been three months, because the foundation is a little weak from the basic grammar of OC to learn, have looked at the green grapefruit and red Grapefruit book, now in the programming practice, take advantage of this opportunity to sum up:

1. Naming conventions

Object type and name match to avoid confusion

-(void) SetUrl: (NSString *) URL; // named method of the error // Change to-(void) seturlstring: (NSString *)string; -(void) SetUrl: (Nsurl *) URL;

Static variables (including scopes) begin with S, and the complete variables start with g, and in general, you should avoid using global variables other than constants:

Static Mything *ssharedinstance

Constants start with K in Cocoa and core foundation, not in cocoa, and it is recommended that the (static) constants in the file scope begin with K:

Static Const Nsuinteger kmaximumnumberofrows=3*const mysomethinghappenednotification=@ " somethinghappeded ";

Method parameter name usually add one article (a,an,the) (Landlord Note: Seemingly not very common AH), in this way to name the parameters can be avoided with the method of local variables and instance name confusion

The instance variable begins with an underscore

The class name begins with an uppercase letter, the method name and variable name should start with a lowercase letter, all class names, and the method names are separated by camel case (that is, the first letter of each word), instead of underlining

2. Automatic reference counting

ARC is not garbage collection, it's just a compiler optimization, so it can't handle circular reference issues:

Garbage collection mechanism if the reference link of an external object to object A is interrupted, both object A and object B are destroyed, but the reference count of arc is greater than 1 because of A/b reference, so the management of strong references must be done in iOS development

There are two main types of relationships for attributes: strong and weak, equivalent to retain and assign in non-ARC environments, as long as there is a strong reference object that will persist and will not be destroyed. The weak reference is automatically set to nil when the referenced object is destroyed, so the delegate property should always be declared as weak. Weak.

3. Properties

In the header file declaration of the public property, the. m file declares the private attribute:

// MyClass.h @interface class  IDdelegate; @property (noatomic,readonly, Strong) NSString *  readonlystring;
@end // MYCLASS.M @interface  **privatestring;
@end

The compiler automatically creates several variables _delegate,_readonlystring,_privatestring, but these instance variables can only be called in Init,dealloc

You can also see that the readonlystring variable was re-declared in the. m file, adding a setter to the private method for it

Modifier keywords for the property:

1) atomicity (atomic,nonatomic)

The intent is that the accessor method of the property is thread-safe and does not guarantee that the entire object is thread-safe. For example, using Nsmutablearray to declare a stuff, use

Self.stuff and Self.stuff=otherstuff (access only), while accessing the array using the Objectatindex method is not thread-safe.

However, if the attribute does not require other threads to access it, the use of atomic attributes is a great waste, so the usual case is nonatomic

2) Read-write properties (ReadWrite and ReadOnly)

3) Set method modifier keywords (weak,strong,copy)

Note that the copy decoration is used for immutable classes such as NSString and Nsarray

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.