"iOS Development Learning-oc article"

Source: Internet
Author: User

The difference between instancetype and ID keywords

1) Instancetype means that the return type of the method is the same as the object type of the calling method.

2) before Object-c introduces Instancetype, the return type of the initialization method is id,id defined as "pointer to any object (equivalent to void* in C)"

The difference is that instancetype can only be used to represent a method return type, but the ID is also used to represent the types of variables and method parameters.

Second, the method declaration order in the header file

1) This is a convention, in order of precedence: instance variables, class methods (static methods), initialization methods, other methods.

Iii. methods for generating random numbers-arc4random ()

1) How to use

Get an integer between 0 and X-1 by Arc4random (): Int value = arc4random ()% x;

The code to get an integer between 1 and X is as follows: int value = (arc4random ()% x) + 1;

Iv.[Nbritem count]: Unrecognized selector sent to instance 0x100309120 '

This is a common mistake, and in the literal sense it sends an unrecognized selector to the object, so check to see if the Nbritem object has the Count method.

V. Weak references and strong references

1) pointer variable points to an object, then the corresponding object will be one more owner, and will not be released by the program, this pointer attribute is a strong reference.

2) The program can also choose to have pointer variables do not affect the number of objects to which they point, and this pointer property, which does not change the number of object owners, becomes a weak reference. Commonly used to solve the problem of strong reference loops, you can parse out a parent-child relationship, and then leave a strongly-referenced attribute to the child object, and then make a weak reference attribute (__weak), when the object with the weak reference attribute is disposed, the pointer is automatically set to nil.

Vi. Attributes of properties

Any attribute can have a set of attributes that describe the behavior of the corresponding access method, such as:

@property (nonatomic, ReadWrite, strong) NSString *itemname

Any attribute has three attributes

1) Multithreading features

Nonautomic and Atomic, in most cases, we set the property to Nonautomar because the default is atomic, so we're going to specify Nonautomar.

2) read/write characteristics

ReadWrite and ReadOnly, you can literally know that the compiler generates set and get methods for readwrite properties, but the ReadOnly property has only the Get method.

3) Memory management features

Strong, weak, copy, unsafe_unretained. These types determine how the corresponding instance variable will reference the object.

    • For unknown attributes (int, etc.) that do not require memory management, it is defined as unsafe_unretained, which makes the default value of non-object properties, so it can be used without writing, which was represented before the introduction of arc using assign.
    • For properties that point to OC objects, the four types are possible. The default is the strong type.
    • For a pointer to a property that points to another object, and the class of the object has a modifiable subclass (for example: Nstring/nsmutablestring, Nsarray/nsmutablearray), the property should be set to copy. For the difference between mutablecopy and copy, see the article "Shallow copy and deep copy"

Not to be continued ...

"iOS Development Learning-oc 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.