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

简介: 最近的iOS开发总算告一段落,了解和学习了不少的内容,抽了一点时间把开发中参考的一些资料和内容汇总一下。 iOS 2007年iPhone发布 2008年iPhone 3G release, 2009年iPhone 3GS.

最近的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

{

...

}

相关文章
|
26天前
|
API 数据安全/隐私保护 iOS开发
利用uni-app 开发的iOS app 发布到App Store全流程
利用uni-app 开发的iOS app 发布到App Store全流程
82 3
|
3月前
|
存储 iOS开发
iOS 开发,如何进行应用的本地化(Localization)?
iOS 开发,如何进行应用的本地化(Localization)?
122 2
|
3月前
|
存储 数据建模 数据库
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
IOS开发数据存储:什么是 UserDefaults?有哪些替代方案?
38 0
|
3月前
|
API 定位技术 iOS开发
IOS开发基础知识:什么是 Cocoa Touch?它在 iOS 开发中的作用是什么?
IOS开发基础知识:什么是 Cocoa Touch?它在 iOS 开发中的作用是什么?
42 2
|
3月前
|
安全 编译器 Swift
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
89 2
|
24天前
|
数据安全/隐私保护 开发者 iOS开发
iOS-打包上架构建版本一直不出现/正在处理/自动消失
iOS-打包上架构建版本一直不出现/正在处理/自动消失
23 0
|
26天前
|
iOS开发
iOS自动混淆测试处理笔记
iOS自动混淆测试处理笔记
11 0
|
2月前
|
监控 API Swift
用Swift开发iOS平台上的上网行为管理监控软件
在当今数字化时代,随着智能手机的普及,人们对于网络的依赖日益增加。然而,对于一些特定场景,如家庭、学校或者企业,对于iOS设备上的网络行为进行管理和监控显得尤为重要。为了满足这一需求,我们可以利用Swift语言开发一款iOS平台上的上网行为管理监控软件。
181 2
|
2月前
|
iOS开发
  iOS 自动混淆测试处理笔记
  iOS 自动混淆测试处理笔记
|
2月前
|
移动开发 开发工具 数据安全/隐私保护
iOS APP 版本更新升级教程:如何打包上架新的 APP 版本?
iOS APP 版本更新升级教程:如何打包上架新的 APP 版本?
iOS APP 版本更新升级教程:如何打包上架新的 APP 版本?