您的位置:首页 > 移动开发 > Objective-C

Objective-C学习备忘单

2014-04-28 15:28 465 查看


终极版本的Objective-C教程备忘单帮助你进行iOS开发。



想开始创建你的第一个iOS应用程序么?那么看一下这篇很棒的教程吧:Create your first iOS 7 Hello World Application



注:这篇文章我写了三天,可能在一些必要的地方使用了编辑和说明,所以如果有任何疑问和修改建议请在下方评论。



这不是一个初学者指南,也不是关于Objective-C的详细讨论,这是关于常见的和高水平的论题的快速索引。



如果这里有些问题没有涉及到,你也可以查阅以下文章:

Objective-C: A Comprehensive Guide for Beginners

The Objective-C Programming Language

Coding Guidelines for Cocoa

iOS App Programming Guide



内容目录

Commenting

Data Types

Constants

Operators

Declaring Classes

Preprocessor Directives

Compiler Directives

Literals

Methods

Properties and Variables

Naming Conventions

Blocks

Control Statements

Enumeration

Extending Classes

Error Handling

Passing Information

User Defaults

Common Patterns



注释

Objective-C中注释用来组织代码,并为将来的代码重构或者那些可能阅读你代码的iOS开发者们提供重要的额外信息。注释通常会被编译器忽视,因此它们不会增加编译器程序的大小。



有两种方式添加注释:

// This is an inline comment(内嵌注释) /* This is a block comment and it can span multiple lines. */(块注释,可用于多行注释) // You can also use it to comment out code(也可以用来注释掉代码) /* - (SomeOtherClass *)doWork { // Implement this } */


使用pragma mark来组织代码:

#pragma mark - Use pragma mark to logically organize your code(使用#pragma mark可以对代码进行逻辑组织) // Declare some methods or variables here(在此处声明方法和变量) #pragma mark - They also show up nicely in the properties/methods list in Xcode(在Xcode中#pragma mark还可以用来很好地显示属性及方法列表) // Declare some more methods or variables here(在此处声明方法和变量)



数据类型

数据类型的大小

无论是在32位还是64位的系统环境中,允许的数据类型大小是由为具体的类型分配了多少内存字节决定的。在32位的系统环境中,长整型(long)被分配4字节,相当于2^(4*8)(每个字节有8位)或者4294967295的内存范围。在64为的系统环境中,长整型(long)被分配8字节,相当于2^(8*8)或者1.84467440737096e19的范围。



64位系统环境的变化的完全指南,请参考:transition document



C语言基础

注:Objective-C继承了所有的C语言基本类型,然后又新增了一些其他的类型。



Void(空类型)

Void是C语言的空数据类型。通常用于指定无返回值的函数的返回值类型(即,函数类型为无返回值类型)。



整数

整数既可以是signed(有符号)的也可以是unsigned(无符号)的。signed(有符号)代表是正整数或者负整数,unsigned(无符号)代表只能是正整数。



整数类型以及它们的字节大小:

// Char (1 byte for both 32-bit and 64-bit) unsigned char anUnsignedChar = 255; NSLog(@"char size: %zu", sizeof(char)); // Short (2 bytes for both 32-bit and 64-bit) short aShort = -32768; unsigned short anUnsignedShort = 65535; NSLog(@"short size: %zu", sizeof(short)); // Integer (4 bytes for both 32-bit and 64-bit) int anInt = -2147483648; unsigned int anUnsignedInt = 4294967295; NSLog(@"int size: %zu", sizeof(int)); // Long (4 bytes for 32-bit, 8 bytes for 64-bit) long aLong = -9223372036854775808; // 32-bit unsigned long anUnsignedLong = 18446744073709551615; // 32-bit NSLog(@"long size: %zu", sizeof(long)); // Long Long (8 bytes for both 32-bit and 64-bit) long long aLongLong = -9223372036854775808; unsigned long long anUnsignedLongLong = 18446744073709551615; NSLog(@"long long size: %zu", sizeof(long long));


固定宽度的整数类型和字节大小来作为变量名:

// Exact integer types int8_t aOneByteInt = 127; uint8_t aOneByteUnsignedInt = 255; int16_t aTwoByteInt = 32767; uint16_t aTwoByteUnsignedInt = 65535; int32_t aFourByteInt = 2147483647; uint32_t aFourByteUnsignedInt = 4294967295; int64_t anEightByteInt = 9223372036854775807; uint64_t anEightByteUnsignedInt = 18446744073709551615; // Minimum integer types int_least8_t aTinyInt = 127; uint_least8_t aTinyUnsignedInt = 255; int_least16_t aMediumInt = 32767; uint_least16_t aMediumUnsignedInt = 65535; int_least32_t aNormalInt = 2147483647; uint_least32_t aNormalUnsignedInt = 4294967295; int_least64_t aBigInt = 9223372036854775807; uint_least64_t aBigUnsignedInt = 18446744073709551615; // The largest supported integer type intmax_t theBiggestInt = 9223372036854775807; uintmax_t theBiggestUnsignedInt = 18446744073709551615;


浮点类型

浮点没有signed或者unsigned

// Single precision floating-point (4 bytes for both 32-bit and 64-bit)单精度浮点float aFloat = 72.0345f; NSLog(@"float size: %zu", sizeof(float)); // Double precision floating-point (8 bytes for both 32-bit and 64-bit)双精度浮点 double aDouble = -72.0345f; NSLog(@"double size: %zu", sizeof(double)); // Extended precision floating-point (16 bytes for both 32-bit and 64-bit)扩展精度浮点 long double aLongDouble = 72.0345e7L; NSLog(@"long double size: %zu", sizeof(long double));


Objective-C基础

id:被定义为匿名或者动态对象类型,它可以存储任何类型对象的引用,不需要指定指针符号。

id delegate = self.delegate;


Class:用来表示对象的类,并能用于对象的内省。

Class aClass = [UIView class];


Method:用来表示一个方法,并可用于swizzling方法。

Method aMethod = class_getInstanceMethod(aClass, aSelector);


SEL:用于指定一个selector,它是编译器指定的代码,用于识别方法的名称。

SEL someSelector = @selector(someMethodName);


IMP:用于在方法开始时指向内存地址。你可能不会用到这个。

IMP theImplementation = [self methodForSelector:someSelector];


BOOL:用来指定一个布尔类型,布尔类型中0值被认为是NO(false),任何非零值被认为是YES(true)。任何零值对象都被认为是NO,因此不需要对零值进行同样的验证(例如,只要写if(someObject),不需要写if (someObject !=nil))

// Boolean BOOL isBool = YES; // Or NO


nil:用来指定一个空对象指针。当类第一次被初始化时,类中所有的属性被设置为0,这意味着都指向空指针。



Objective-C中还有很多其他类型,如NSInteger, NSUInteger, CGRect,CGFloat, CGSize, CGPoint等。



Enum(枚举)和Bitmask(位掩码)类型

Objective-C的枚举类型可以用以下多个不同方式定义:

// Specifying a typed enum with a name (recommended way)用一个别名来指定一个带有typedef关键字的枚举类型(推荐方法) typedef NS_ENUM(NSInteger, UITableViewCellStyle) { UITableViewCellStyleDefault, UITableViewCellStyleValue1, UITableViewCellStyleValue2, UITableViewCellStyleSubtitle }; // Specify a bitmask with a name (recommended way)用一个别名来指定一个bitmask(推荐方法) typedef NS_OPTIONS(NSUInteger, RPBitMask) { RPOptionNone = 0, RPOptionRight = 1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: