Required for beginners: Objective-C learning documents

Source: Internet
Author: User

Objective-CThis document describes how to manage data types and memory,Object, Protocol and other content. For details, refer to the detailed description in this article.

I. Data Types

1. nsange

 
 
  1. typedef struct _NSRange {  
  2. NSUInteger location;  
  3. NSUInteger length;  
  4. } NSRange; 

2. NSPoint, NSSize, NSRect

3. NSString

 
 
  1. Method 1: Class Method) stringWithFormat [NSString stringWithFormat: @ ""]
  2. Method 2: length
  3. Method 3: isEqualToString [Instance name 1 isEqualToString: Instance name 2]
  4. Method 4: compare can contain parameters
  5. Method 5: hasPrefix hasSuffix

4. NSMutableString

 
 
  1. Method 1: Class Method) stringWithCapacity [NSMutableString stringWithCapacity: Number of the size]
  2. Method 2: appendString appendFormat
  3. Method 3: deleteCharactersInRange rangeOfString

5. NSArray

 
 
  1. Method 1: Class Method) arrayWithObject is separated by commas (,) and ends with nil.
  2. Method 2: count objectAtIndex

6. NSMutableArray

 
 
  1. Method 1: Class Method) arrayWithCapacity
  2. Method 2: addObject removeObjectAtIndex

7. NSEnumerator

 
 
  1. Method 1: objectEnumerator reverseObjectEnumerator
  2. Method 2: nextObject

8. NSDictionary

 
 
  1. Method 1: Class Method) series of storage for dictionaryWithObjectsAndKeys receiving objects and keywords, ended with nil
  2. Method 2: objectForkey

9. NSMutableDictionary

 
 
  1. Method 1: Class Method) dictionaryWithCapacity
  2. Method 2: setObject: forKey: removeObjectForKey

10. NSNumber

Used to package basic data types into objects

11. NSValue

Any value can be packaged

12. NSNull

 
 
  1. [NSNull null] indicates null

Ii. Memory Management

1. Cocoa memory management rules

1) when you create an object using the new, alloc, or copy method, the reserved counter value of this object is 1. When this object is no longer used, you are responsible for sending a release or autorelease message to this object, or releasing it in the dealloc method ). In this way, the object will be destroyed at the end of its service life.

2) When you obtain an object through any other method, assume that the reserved counter value of this object is 1 and has been set to Auto Release, therefore, you do not need to perform any operations to ensure that the object is cleared.

Auto Release pool:

 
 
  1. NSAutoreleasePool *pool;  
  2. pool = [[NSAutoreleasePool alloc] init];  
  3. [pool release]; 

If you want to use the retain method for the object within a period of time, you need to keep it and ensure that it is released after the operation is completed or released in the dealloc method ).

3) if you keep an object, you need to eventually release it or automatically release it. Make sure that the retain method and the release method are used equally.

Note: If you use the alloc, new, or copy method to obtain an object, you must release the object.

Iii. object initialization

1. Basic Knowledge

1) There are two ways to create an object in Objective-C:

[Class Name new]: directly new a new object

[[Class name alloc] init]: first allocates memory space for the object and then initializes it.

2) in Cocoa, object allocation and initialization are two separate operations: the class method alloce from NSObject allocates a piece of memory for the object and clears it 0; the instance method init is used to obtain an object and run it.

2. Convenient initialization functions

Multiple initialization functions in a class, that is, multiple methods starting with init.

3. Specify the initialization Function

An Initialization Method in the class is assigned as the specified initialization function. All the initialization methods of the class use the specified initialization function to perform the initialization operation. The subclass uses the specified initialization function of its superclass to initialize the superclass.

Iv. Feature property)

1. Basic Knowledge

The pre-compiled command of the feature is @ property, which is used to automatically declare the setter and getter methods of the attribute. Therefore, you no longer need to generate lengthy code for the accesser method written for each attribute.

The pre-compiled command @ property is used to notify the compiler to automatically generate the method declaration for the setter and getter attributes of the object.

At the same time, the @ synthesis pre-compilation command is used to notify the compiler to generate the corresponding accesser method.

2. Format

Declare in the. h file: @ property type variable name;

Implement in the. M file: @ synthesis variable name;

3. Related point syntax

The setter and getter methods of the access attribute can be used in either of the following ways:

1) [object attribute method]: The regular [] method unique to Objective-C

2) object. Attribute method: similar to other languages. Method

4. Special Line Extension

You can specify the attribute requirements in the attribute declaration, such as copy, retain, nonatomic, and readwrite by default) and readonly.

 
 
  1. @ Property retain, nonatomic) type variable;

V. Category

1. Basic Knowledge

A category is a way to add a new method to an existing class.

2. Format

1) Statement:

 
 
  1. @ Interface Class Name class name)
  2. Method list
  3. @ End

2) Implementation:

 
 
  1. @ Implementation class name)
  2. Method implementation
  3. @ End

3. Category limitations

1) only new methods can be added for category, and new instance variables cannot be added

2) If the class name conflicts with the method in the original class, the class will overwrite the original method because the class has a higher priority.

4. Role of category

Classes can be implemented in different files.

Vi. Agreement

1. Basic Knowledge

The Objective-C protocol is similar to the interface concept in Java.

2. Format

1) Statement:

 
 
  1. @ Protocol name
  2. Method declaration list
  3. @ End

2) protocol used for objects: Add <protocol name> after @ interface class declaration

 
 
  1. @ Interface class name <protocol name>

3) object implementation Protocol: The method for implementing the Protocol in @ implementation

 
 
  1. @ Implementation Class Name
  2. How to implement the Protocol
  3. @ End

3,Objective-C2.0 new features of the Protocol

1) @ optional pre-compiled command: indicates the implementation method that can be selected.

2) @ required: indicates the method that must be implemented forcibly.

Summary: essential for beginners:Objective-CI hope this article will help you understand the content of this document!

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.