iOS开发笔记 1、iOS版本和Objective-C

最近的iOS开发总算告一段落,了解和学习了不少的内容,抽了一点时间把开发中参考的一些资料和内容汇总一下。

iOS

2007年iPhone发布

2008年iPhone 3G release,

2009年iPhone 3GS.

2010 年iPhone 4 and iPad

Each is a 4.7- or 4.8-ounce computing device. Each contains a 620 MHz ARM CPU that has been underclocked to improve battery performance and reduce heat. The iPhone and iPhone 3G each include 128 MB of dynamic RAM (DRAM) and from 4 to 16 GB of Flash memory. The 3GS received an upgrade to 256 MB of RAM as well as a graphics chip enabling it to run OpenGL ES 2.0.

开发时使用的语言推荐是Objective-c

开发工具Xcode Interface Builder

框架库Cocoa

Objective-c

A header (.h) file

a source code (.m) : Objective c实现文件

命名

The names of files that contain Objective-C source code have the .m extension. Files that declare class and category interfaces or that declare protocols have the .h extension typical of header files.

Class, category, and protocol names generally begin with an uppercase letter; the names of methods and instance variables typically begin with a lowercase letter. The names of variables that hold instances usually also begin with lowercase letters.

■ A class can declare methods with the same names as methods in other classes.

■ A class can declare instance variables with the same names as variables in other classes.

■ An instance method can have the same name as a class method.

■ A method can have the same name as an instance variable.

■ Method names beginning with “_” , a single underscore character, are reserved for use by Apple.

[From “The Objective-C Programming Language”]

运行时

The Objective-C language defers as many decisions as it can from compile time and link time to runtime. Whenever possible, it dynamically performs operations such as creating objects and determining what method to invoke. This means that the language requires not just a compiler, but also a runtime system to execute the compiled code. The runtime system acts as a kind of operating system for the Objective-C language

有些类似动态语言

id

This is the general type for any kind of object regardless of class, and can be used for both instances of a class and class objects themselves.

nil表示一个空对象,即id=0。

在objc/objc.h头文件可以看到id的定义就是个指针

和C#中的Object类似

 

消息

[receiver message];

[receiver message:argument];

[receiver message:arg1 label2:arg2 label3:arg3];

[[UITextView alloc] initWithFrame:textFieldFrame];

 

All your message calls should follow one of these four patterns when naming its receiver: they can call something by its class name (for a class method), by its instance name (for an instance method), by the self keyword, or by the super keyword.

selector

In Objective-C, “selector” has two meanings. It can be used to refer simply to the name of a method when it’s used in a source-code message to an object. It also, though, refers to the unique identifier that replaces the name when the source code is compiled. Compiled selectors are of type SEL. All methods with the same name have the same selector. You can use a selector to invoke a method on an object—this provides the basis for the implementation of the target-action design pattern in Cocoa.

 

SEL setWidthHeight;

setWidthHeight = @selector(setWidth:height:);

setWidthHeight = NSSelectorFromString(aBuffer);

[friend performSelector:@selector(gossipAbout:) withObject:aNeighbor];

is equivalent to:

[friend gossipAbout:aNeighbor];

 

if ( [anObject respondsToSelector:@selector(setOrigin::)] )

[anObject setOrigin:0.0 :0.0];

else

fprintf(stderr, "%s can’t be placed\n",

[NSStringFromClass([anObject class]) UTF8String]);

/* AppleTree.h */

@interface AppleTree : UrTree

{ NSString *appleType;

}

@property NSString *appleType;

- (id)growFruit:(NSString *)appleColor;

@end

/* AppleTree.m */

#import "AppleTree.h"

#import "Apple.h"

@implementation AppleTree

@synthesize appleType;

- (id)growFruit:(NSString *)appleColor

{

Apple *fruit = [Apple appleWithColor:appleColor];

return fruit;

}

@end

 

Categories are used if you want to add behavior to a class without subclassing. As usual, you do so by creating a new pair of files containing @interface and @implementation code. This time, you no longer need to worry about the superclass name but must include a category name in parentheses, as follows:

 

@interface AppleTree (MyAppleChanges)

@implementation AppleTree (MyAppleChanges)

 

As a result, the categorized methods and variables that you describe for the classes are added to the core class definition in your program.

 

A protocol is effectively an interface that’s not tied to a class. It declares a set of methods, listing their arguments and their returns. Classes can then state that they’re using the protocol in their own @interface statements. For example, if you had a Growing protocol that was used by plants and animals alike, you could define its usage as follows:

@interface AppleTree : UrTree <Growing>

The AppleTree class would thus be promising that it would respond to all the methods defined in the Growing protocol.

 

变量和访问修饰符

@interface Worker : NSObject

{

char *name;

@private

int age;

char *evaluation;

@protected

id job;

float wage;

@public

id boss

}

默认是@ protected

Property

@property(attributes) type name;

@property float value;

等同于

- (float)value;

- (void)setValue:(float)newValue;

指示编译器生成访问方法

@synthesize value;

@synthesize firstName, lastName, age = yearsOld;

方法

+ 类方法

- 实例方法

+ (id)alloc

{

...

}

- (BOOL)isFilled

{

...

}

- (void)setFilled:(BOOL)flag

{

...

}

posted @ 2011-01-14 16:23  2012  阅读(1174)  评论(0编辑  收藏