Outline:
Introduction
Notes compared with the C Language
Objective-c advanced features
Development Tool introduction (cocoa toolkit features, framework, source file organization; XCode usage Introduction)
Introduction:
1. objective-c is an extension set of c language, which is mainly maintained by APPLE and is the main development language in MAC systems.
In my opinion, objective-C is an alternative and non-mainstream language for people who are familiar with commonly used languages such as c and JAVA.
2. For Mac UI programs, the Cocoa framework is used. The components of cocoa include the foundation and application kit frameworks.
[Foundation framework processes features under the user interface, such as data structures and communication mechanisms; application kit framework includes advanced features of cocoa: user interface elements, printing, color, sound management, applescript, etc]
3. I learned this book:
I have mastered the syntax of Objective C, can understand the code written by others, and can write the code myself;
Familiar with the use of XCode in the development environment (including Project Creation, debugging, running, and code management)
And CNotes for Language Comparison:
1. file Introduction: Objective-C also uses header files with the suffix. h, but use. m (message, also known as method in other object-oriented programming languages) is used as the suffix of the source file.
In objective-c, use # import <> instead of # include <>. # import ensures that the header file is only contained once.
2. Consistent with C:
Data Types, expressions, and various operators
Loop: for, while, do while, break, continue
Branch: if, else, switch
3. NSlog () function: similar to printf (), You want to output information on the console. However, it adds some features, such as timestamps.
[Cocoa adds the NS prefix to all functions, constants, and type names .]
4. @ in front of double quotation marks indicates that the strings in double quotation marks should be processed as NSString elements of cocoa.
5. boolean BOOL: Value: YES, NO;
6.% d indicates the output integer
% @ Indicates the output NSString type
% S indicates the output String Array (char *);
7. square brackets in objective-c:
1. Used to notify an object of what to do.
2. The first item in square brackets is the object, and the rest is the operation you need to perform on the object.
3. In objective-c, an object is notified to execute an operation calledSend message. (Call method)
8. identifier id: it is a generic type used to represent objects of any type.
9. Class declaration @ interface:
|
@ Interface Circle: NSObject // defines the interface for the Circle class; NSObject indicates the parent class. {// The following are data members ShapeColor fillColor; ShapeRect bounds; } // The method declaration is as follows: -(Void) setFillColor: (ShapeColor) fillColor; // the short line above indicates that this is a method declaration. // The short-term is followed by the return type of the Method // The method name is followed. // The colon is followed by a parameter, where (ShapeColor) is a parameter type, FillColor is the parameter name. -(Void) setBounds :( ShapeRect) bounds; -(Void) draw; // Minus sign is a common function The plus sign is a static function. @ End // end statement |
When objective-c sees the @ symbol, it is regarded as an extension of the c language. |
The complete declaration of a class is as follows: @ Interface CClassA (Category): CBaseClass <IA, IB...> // Class Name (category name): parent class <protocol> |
10. Class implementation @ implementation
@ Implementation |
@ Implementation Circle -(Void) setFillColor: (ShapeColor) c { FillColor = c; } -(Void) draw { NSLog (@ "drawing a circle at (% d) in % @", bounds. x, bounds. y, bounds. width, bounds. height, colorName (fillName )); } @ End |
The hidden object self corresponds to this in C. Self-> fillcolor to access member variables. |
11. Call the written classes and class functions:
// Create a new object and use the default initialization Function Bank * bankDefault = [[Bank alloc] init]; // Call method: [Bank addAmount: 1]; [Bank print]; // Release the object: [BankDefault free]; |
|
12.
Two Parameter methods: |
-(Void) setTire: (Tire *) tire // Declaration AtIndex: (int) index; // Use [Car setTire: tire atIndex: 2]; |
Objective-cAdvanced features:
1. Inheritance
Objective-c does not support multi-inheritance. |
Super Keyword: Call the parent class of the class; Superclass: another statement of the parent class. |
|
2.
Custom NSLog () output: |
You can add the description method to the class to customize how NSLog () Outputs objects. |
@ Implementation Tire -(NSString *) description { Return (@ "I am a tire ."); } Main () { NSLog (@ "% @", tire [0]); } |
3.
Foundation kit: |
Cocoa consists of two frameworks: foundation kit [including some basic classes] And application kit. [including user interface objects and advanced classes] |
Create a string: NSString * test; Test = [NSString stringWithFormat: @ "I'm % d years old! ", 23]; |
If the plus sign is added before the method when the method is declared, the method is defined as a class method. [This method belongs to a class object, not an Instance Object of the class .] |
NSArray class: it can store any type of objects. 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, and struct. 2. nil cannot be stored (the zero or NULL value of the object). [because nil is added at the end of the list to indicate that the list ends when NSArray is created .] Create NSArray: NSArray * array; Array = [NSArray arrayWithObjects: @ "one", @ "two", nil]; |
NSString, NSMutableString class; [NSString is immutable, that is, once it is created, it cannot be changed by deleting a character or adding a character; NSMutableString is variable. These two classes are similar to the difference between the string and stringBuffer classes in JAVA .] NSArray, NSMutableArray class; NSEnumerator enumeration; 【 NSEnumerator * emun; Emun = [array objectEnumerator]; Id thingie; While (thingie = [enumerator nextObject]) {} ] NSDictionary: A Dictionary (a set of keywords and their definitions .) [Also becomes a hash, associated array], NSMutableDictionary class; NSNumber: Used to encapsulate basic data types, such as int, char, float, and bool. [Package A basic data type into an object called packing .] NSValue: It can wrap any class, And NSNumber is its subclass. NSNull: |
When "CF" is displayed in cocoa, it indicates that it is related to Apple's Core Foundation framework. |
NSAutoreleasePool: Automatically releases the memory pool. |
|
4.
Memory Management |
Each object has a reference count associated with it (also called a retention count) |
When an object is created using the alloc, new, or copy message (a copy of the receiving object is generated), the reference count of the object is set to 1; This value is added when a retain message is sent to an object; This value is reduced when a release message is sent; When the reference count of an object changes to 0, objective-c will automatically send a dealloc message to the object. Destroy the object. You can override this method in your own object, You can use the retainCount message to obtain the reference counter value. -(Id) retain; -(Void) release; -(Unsigned) retainCount; |
Automatic release pool: autorelease pool; Create: NSAID utoreleasepool * pool; Pool = [[NSAID utoreleasepool alloc] init]; Destruction: [Pool release]; Note: xcode automatically generates code. When the pool is destroyed, [pool drain] is used. The drain method only clears the release pool, but does not destroy the pool. therefore, you still need to use release when writing your own code. In addition, drain applies only to mac OS 10.4 and later versions, while release applies to all versions. This object is automatically released only when an autorelease message is sent to an object. For example, [car autorelease]; |
Gold guidelines for memory management: Only objects created using the alloc, new, and copy methods require the programmer to send the release or autorelease message to the object. Objects obtained through other methods are set to Auto Release by default, so no operations are required by the programmer. |
There is a garbage collection mechanism in objective-c 2.0, To use garbage collection for a project: Project information -- build tab -- Query "garb", "objective-c Garbage Collection" appears, set its value to "required [-fobjc-gc-only]" After garbage collection is enabled, the common memory management commands are all empty operation commands and no operation is executed. You cannot use garbage collection to develop iphone software. |
|
5.
Object initialization |
There are two ways to create a new object: [Class Name new] [[Class name alloc] init] The two methods are equivalent, but the cocoa convention is to use the latter. |
Alloc initializes the memory to 0 while allocating space for the object; |
Init method: Initialize instance variables to make the object available. [The return type is id, and the returned value describes the object to be initialized] When using new to create a new object, the system must complete two steps: 1. allocate memory for the object, that is, the object obtains a memory block used to store its instance variables; 2. automatically call the init method to make the object available. |
|
|
6.
New Features of objective-c 2.0 [applicable only to mac OS x10.5 and later] |
@ Property: Declares the attributes of an object. [In this way, you do not need to write the attribute accessors .] (It has attributes such as copy, retain, readwrite, and readonly) |
@ Synthesize: "Create accessors for this attribute" |
Point expression |
|
|
7.
Category |
Category is a way to add a new method to an existing class. |
CATEGORY declaration: @ Interface NSString (NumberConvenience) // Class Name (category name) -(NSNumber) lengthAsNumber; // extension method declaration @ End When using the original class name, you can call methods in all its classes. |
CATEGORY limitations: 1. You cannot add new instance variables to the class; 2. If the method in the category has the same name as the existing method in the class, the method in the class is unavailable and replaced by the new method in the category. |
Category: 1. Distribute the implementation of classes to multiple files or frameworks; 2. Create a forward reference to a private method; [Cocoa does not have a real private method, so the method for implementing a private method is as follows: Declare the method in the category first, and then implement the method in the implementation of the existing class. In this way, other methods in this class can use this method, and other external classes will not know the existence of this method .] 3. Add informal protocols to objects. [Creating an NSObject category is called creating an informal Protocol .] |
The delegate is an object. Objects of another class require 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 to execute the delegate method. |
Selector:@ Selector(): The selector is only a method name, but it is encoded in a special way during the objective-c runtime to quickly execute the 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 delegated object know whether it can process messages sent to it (delegated object? With the selector, the entrusted object first checks the entrusted object and asks whether it can respond to the selector. If yes, send a message to it. |
|
8.
Protocol: |
The formal protocol is a list of named methods. Protocol adoption means that all methods of the Protocol must be implemented. Otherwise, the compiler will issue a warning. Formal agreements are likeJAVAThe same as in. |
Declarative agreement: @ Protocal NSCopying -(Id) copywithzone :( NSZone *) zone; // method list @ End |
Protocol used: @ Interface Car: NSObject <NSCopying, NSCoding> // list of protocols to be implemented in brackets {// Instance variable list} // Method list @ End |
In objective-c 2.0, there are new features: @ optional, @ required |
9.
AppKit: |
Create an APPKIT project: File-new project-mac OS-application-Cocoa application; |
IBOutlet and IBAction Both are provided by APPKIT # defines. IBOutlet does not function and does not compile it. IBAction is defined as void. These two are the tags provided for the Interface Builder and the person reading the code. |
A. xib file is generally called A. nib file. The. nib file is a binary file that contains frozen objects. The. xib file is a nib file in XML format. |