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

{

...

}

相关文章
|
6月前
|
安全 编译器 Swift
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
IOS开发基础知识: 对比 Swift 和 Objective-C 的优缺点。
390 2
|
2月前
|
设计模式 前端开发 Swift
探索iOS开发:Swift与Objective-C的较量
在这篇文章中,我们将深入探讨iOS开发的两大编程语言——Swift与Objective-C。我们将分析这两种语言的特性、优势和局限性,并讨论它们在现代iOS开发中的应用。无论你是初学者还是有经验的开发者,这篇文章都将为你提供有价值的见解和建议。
57 3
|
3月前
|
开发工具 iOS开发 容器
【Azure Blob】关闭Blob 匿名访问,iOS Objective-C SDK连接Storage Account报错
【Azure Blob】关闭Blob 匿名访问,iOS Objective-C SDK连接Storage Account报错
|
4月前
|
开发工具 iOS开发 容器
【Azure Blob】关闭Blob 匿名访问,iOS Objective-C SDK连接Storage Account报错
iOS Objective-C 应用连接Azure Storage时,若不关闭账号的匿名访问,程序能正常运行。但关闭匿名访问后,上传到容器时会出现错误:“Public access is not permitted”。解决方法是将创建容器时的公共访问类型从`AZSContainerPublicAccessTypeContainer`改为`AZSContainerPublicAccessTypeOff`,以确保通过授权请求访问。
【Azure Blob】关闭Blob 匿名访问,iOS Objective-C SDK连接Storage Account报错
|
6月前
|
缓存 开发工具 iOS开发
优化iOS中Objective-C代码调起支付流程的速度
优化iOS中Objective-C代码调起支付流程的速度
103 2
|
6月前
|
算法 编译器 Swift
【Swift开发专栏】Swift与Objective-C的对比
【4月更文挑战第30天】Swift与Objective-C对比:Swift语法简洁,支持元组、泛型和闭包,提高可读性;性能优化,使用LLVM编译器,与Objective-C兼容,便于迁移项目;生态系统活跃,苹果官方支持,丰富资源库。Objective-C虽历史悠久,但逐渐边缘化。对于新项目和开发者,Swift是更佳选择,驱动iOS开发创新。
442 0
|
6月前
|
iOS开发
iOS自动混淆测试处理笔记
iOS自动混淆测试处理笔记
27 0
|
6月前
|
iOS开发
  iOS 自动混淆测试处理笔记
  iOS 自动混淆测试处理笔记
|
6月前
|
安全 JavaScript 前端开发
IOS开发基础知识:介绍一下 Swift 和 Objective-C,它们之间有什么区别?
IOS开发基础知识:介绍一下 Swift 和 Objective-C,它们之间有什么区别?
264 0
|
机器学习/深度学习 API iOS开发
【IOS 开发】Objective-C Foundation 框架 -- 字符串 | 日期 | 对象复制 | NSArray | NSSet | NSDictionary | 谓词(一)
【IOS 开发】Objective-C Foundation 框架 -- 字符串 | 日期 | 对象复制 | NSArray | NSSet | NSDictionary | 谓词(一)
178 0