Dark Horse Programmer--objective-c Features

Source: Internet
Author: User

1. Inheritance

OBJECTIVE-C does not support multiple inheritance. Super Keyword: Call the parent class of the class; superclass: Another way of saying the parent class.

2. Custom NSLog () output:
Adding the description method to a class allows you to customize how NSLog () outputs the object.

#import <Foundation/Foundation.h>

@implementation Tire-(NSString *) description

{
Return (@ "I am a tire.");

}

int Main ()

{
NSLog (@ "%@", tire[0]);

return 0;

}

3.Foundation Kit:
Cocoa is made up of two parts: the foundation Kit [includes some basic classes] and application kit. "Includes user interface objects and advanced classes"

To create a string:

NSString *test;
test=[nsstring stringwithformat:@ "I ' m%d years old!", 23];
If you add a plus sign before a method when declaring a method, it means that the method is defined as a class method, which belongs to the class object, not to the instance object of the class. 】
Nsarray class: You can store objects of any type. It has two restrictions:
1. It can only store objective-c objects, but cannot store basic data types in C, such as int, float, enum, struct, etc.

2. You cannot store nil (0 value or null value for an object); "Because when you create Nsarray, you add nil to the end of the list. 】

Create Nsarray:

Nsarray *array;
Array=[nsarray arraywithobjects:@ "One", @ "one", nil];

NSString, Nsmutablestring class;
The nsstring is immutable, that is, once created, it cannot be changed by deleting characters or adding characters, whereas nsmutablestring is mutable.
These two classes are like the difference between a string in Java and a StringBuffer class. "Nsarray, Nsmutablearray class; Nsenumerator enumeration;

nsenumerator *emun; 
emun=[array objectenumerator]; id thingie; 
while ( Thingie=[enumerator nextobject]) {}  "&NBSP;
Nsdictionary: A dictionary (a collection of keywords and their definitions.) "Also become a hash list, associative array", nsmutabledictionary class;  
NSNumber: Used to wrap basic data types, such as int ,char, float, bool; " Wrapping a basic type of data into an object is called boxing. "&NBSP;
Nsvalue: It can wrap any class, NSNumber is its subclass.  nsnull: 
  When the word "CF" is seen in cocoa, it means that it is the content of Apple's core foundation  framework.   nsautoreleasepool: Automatically frees the memory pool.    
         
4. Memory management  
Each object has a reference count (also called a retention count) associated with it  
When an object is created by using the alloc, new  method or by  copy message (Generating a copy of the receiving object), the object's reference-count value is set to 1;  
  When sending an retain message to an object, increase the value;   send release message, reduce the value;  
When the reference count value of an object becomes 0 o'clock, Objective-c automatically sends an DEALLOC message to the object. Destroys the object.  

You can override this method in your own object,
Using the Retaincount message, you can get the value of the reference counter. -(ID) retain; -(void) release;
-(unsigned) retaincount;

Auto-Release pool: autorelease pools; Create:
NSAutoreleasePool *pool;
Pool=[[nsautoreleasepool alloc] init];

Destroyed:
[Pool release];
Note: Xcode auto-generated code, when destroying the pool, is using the [pool Drain],drain method to simply empty the release pool, but not destroy it. So you use release when you write your own code.
Furthermore, drain is only available for Mac OS 10.4 and above, and release is available for all versions.
The object will be automatically freed only if it is added to NSAutoreleasePool when the autorelease message is sent to an object.
such as: [Car autorelease];

Gold Guidelines for memory management:
Only objects created through the Alloc, new, and copy methods require the programmer to send release or autorelease messages to the object.
Objects obtained by other methods are set to Auto-release by default, so no programmer is required to do any of the work. There is a garbage collection mechanism in OBJECTIVE-C 2.0, if you want to use garbage collection for an item:
Project information query "garb", "objective-c garbage Collection" appears, set its value to "Required[-fobjc-gc-only"
When garbage collection is enabled, the usual memory management commands become empty operations directives and do nothing. You can't use garbage collection to develop iphone software.

5. Object initialization
Two ways to create new objects: [Class name New], [[Class name alloc] init]
The two methods are equivalent, but the Cocoa Convention is to use the latter. Alloc the memory is initialized to 0 while allocating space for the object;

Nit Method: Initializes an instance variable so that the object is in a usable state. [Return type is ID, the returned value describes the object being initialized]

When creating a new object using new, the system will complete two steps:
1. Allocate memory for the object, that is, the object obtains a block of memory to hold its instance variables;

2. Automatically call the Init method to make the object available.

new features of 6.OBJECTIVE-C 2.0 "only for Mac OS x10.5 and above"
@property: Represents a property that declares an object. "So you don't have to write the accessor of the property again." "(He has copy, retain, ReadWrite, ReadOnly and other attributes)

@synthesize: Represents the create accessor for this property point expression

7. Category (category)
Category is a way to add a new method to an existing class.

Declaration of classification:
@interface NSString (numberconvenience)//class name (category name)

-(NSNumber) Lengthasnumber; //extension method declaration

@end
Using the original class name, you can call methods in all of his categories.

Limitations of classification:
1. You cannot add a new instance variable to a class;
2. If the method in the category has the same name as an existing method in the class, the method in the class is not available and is replaced by a new method in the category.

Role of classification:
1. Spread the implementation of the class into multiple files or frameworks;

2. Create a forward reference to the private method;
"There is no real private method in cocoa, the method of implementing a private method similar functionality is to first declare the method in the taxonomy, and then implement the method in the implementation of the existing class."
This method can be used by other methods in this class, and other external classes will not be aware of the existence of the method. 】

3. Add an informal agreement to the object.
"Creating a nsobject category is called creating an informal protocol. 】

A delegate delegate is an object, and an object of another class requires the delegate object to perform some of its operations. When a delegate object is triggered at a certain time (an event), the delegate object is automatically notified of the execution of the delegate method.

Selector: @selector (): The selector is just a method name, but it is encoded in a special way used by the OBJECTIVE-C runtime to quickly execute a query. The content in parentheses is the method name.
So the setengine of the car class: The selector of the method is: @selector (setengine:
How does a delegate object know that its delegate object can handle the message it (the delegate object) sends to it (the delegate object)? Through the selector, the delegate object checks the delegate object first to see if it responds to the selector. If it does, it sends a message to it.
8.
Agreement:
A formal agreement is a list of named methods.
Adopting a protocol means that all the methods of the Protocol must be implemented. Otherwise, the compiler will issue a warning.

Declaration Agreement:
@protocal nscopying
-(ID) Copywithzone: (Nszone *) zone;

Method List

@end

Adoption Protocol:
@interface car:nsobject <NSCopying,NSCoding>//brackets are the list of protocols to implement

{

Instance variable list

}

Method List

@end
In OBJECTIVE-C 2.0, there are new features: @optional, @required

9.AppKit:
To create a AppKit project:
File-new Project-mac Os-application-cocoa Application; Iboutlet and Ibaction
Both of these are the #defines provided by AppKit. Iboutlet does not have any effect, it is not compiled. Ibaction is defined as void.
These are the tags provided for Interface builder and the person reading the code.

. xib files are generally referred to as. nib files.
The. nib file is a binary file that contains frozen objects. The. xib file is a nib file in XML format.

http://www.itheima.com/

Dark Horse Programmer--objective-c Features

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.