iOS 开发学习笔记
Stay hungry. Stay foolish.
HelloWorld
New Project > Mac OS X Application > Command Line Tool (待补)
快捷键
CMD + 0 显示 / 隐藏左栏
CMD + ALT + 0 显示 / 隐藏隐右栏
CMD + R 编译并运行
#import 关键字
避免重复引入,注意与 #include 的区别
数据类型
布尔型
-
BOOL typedef char BOOL; #define YES 1 #define NO 0
-
另外也可用 bool true | false
字符串类型
-
NSString
-
常量定义用 @""
-
输出要配合 NSLog() 使用
NSString *str = @“Hello world”; NSSLog(@"%@", str);
NSString *strl = [[NSString alloc] initWithFormat:@"%d+%d=%d",3,5,8]; NSSLog(@"%@", strl); [strl release];// 需关闭 ARC
内存管理机制
ARC 自动引用计数(默认) MRC 手动引用计数
等价的写法:
//Xcode4.3+
@autoreleasepool {}
//Xcode4.3
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
......
[pool drain];
类
类的声明
#import <Foundation/Foundation.h>
@interface ASStudent : NSObject {
@protected
NSString * name;
@private
NSString * sid;
@public
unsigned int age;
}
-(void) setName:(NSString*)aName;
-(NSString*) name;
-(void) setAge:(int)aAge;
-(int) age;
@end
实例变量默认访问权限是 @protected
NSObject
-
内存分配
-
内存回收
-
对象初始化
-
对象描述
@interface NSObject
{ Class isa; } -(id)init; +(id)new; +(id)alloc; -(void)dealloc; +(NSString*)description … @end
方法的声明
-(void)insertObject:(id)anObject atIndex:(NSUInteger)index
方法的类型 - 返回类型 (void) 方法的签名部分 insertObject, atIndex 参数类型 (id), (NSUInteger) 参数名 anObject, index
方法的类型
-
类方法 (Class Method),可理解为 " 静态方法 " +alloc;
-
实例方法 (Instance Method) -(void) display;
方法的返回值类型可以不写,默认为 id 类型
类的实现
#import "ASStudent.h"
@implementation ASStudent
-(void) setName:(NSString*)aName {
name = aName;
}
-(NSString*) name {
return name;
}
+(void) print {
NSLog(@"Class Method");
}
@end
方法都是 public 的,没有 protected 和 private 方法。 但如果一个方法只出现在类的实现里,那么这个方法可以认为是私有方法。
即 Obj-C 虽然没有方法的访问控制关键字,但仍可通过头文件区分公有与私有方法。
类的实例化
- 在 Obj-C 中,所有对象都在堆区创建,在栈区创建对象是语法错误。
- 通过指针访问堆区对象,一般我们说对象就是指对象的指针
- id 是通用对象指针类型
分配空间 > 初始化
#import <Foundation/Foundation.h>
#import "ASStudent.h"
int main(int argc, const char *argv[])
{
@autoreleasepool {
ASStudent *zhang = [[ASStudent alloc] init];
[zhang setName:@" 张 "];
[zhange setAge:18];
NSLog(@"%@ %u", [zhang name], [zhang age]);
[zhange release];
}
return 0;
}
空对象 nil
nil 只能被应用在可以使用 id 类型的地方
NULL 用于值类型的指针
对 nil 发消息(调用方法),不会抛出异常
id pid = nil; //ok
int *pInt = NULL; //ok
pid = NULL; //Error
存取器 (getter/setters)
一般 getter 方法和实力变量同名,setter 方法命名格式为 setPropertyName 只要写了对应的存取器方法,就可使用点语法。 这里的点语法是一种“语法糖”,实际上编译后还是调用相应的存取器方法。
zhang.name = @" 张 ";
NSLog(@"%d", zhang.age);
// 等价于
[zhang setName:@" 张 "];
NSLog(@"%d", [zhang age]);
属性的声明
如果类中存在大量实例变量,且需要提供大量 setter 和 getter 方法。为了减轻工作量,Objective-C 2.0 引入了属性声明机制。
属性声明 @property(attributes) 数据类型 变量类型 ;
存取器方法实现 @synthesize 实例变量 1, 实例变量 2, … 实例变量 n;
属性的分组 线程相关
- atomic(默认) 注:仅在线程相关时需要,其他场合不推荐用
- nonatomic
读 / 写
- readonly
- readwrite(默认)
类中属性的访问
@property int property; // 声明
@synthesize property=_property; // 相当于别名 ?
self.property = 0;
_property = 0;
继承
#import <Foundation/Foundation.h>
@interface ASGraphic : NSObject {
NSPoint origin;
}
@property(nonatomic) NSPoint origin;
-(id) initWithOrigin:(NSPoint)aPoint;
@end
#import "ASGraphic.h"
@implementation ASGraphics
@synthesize origin;
-(id) initWithOrigin:(NSPoint)aPoint{
if(self = [super init]) {
origin = aPoint;
}
return self;
}
@end
#import <Foundation/Foundation.h>
@interface ASShape : NSObject {
NSPoint origin;
NSString *primaryColor;
}
@property(nonatomic,assign) NSPoint origin;
@property(nonatomic,retain) NSString *primaryColor;
-(id) initWithOrigin:(NSPoint)aPoint color:(NSString*)aColor;
@end
写成继承关系后:
#import <Foundation/Foundation.h>
#import "ASGraphic.h"
@interface ASShape : ASGraphic {
NSString *primaryColor;
}
@property(nonatomic,retain) NSString *primaryColor;
-(id) initWithOrigin:(NSPoint)aPoint color:(NSString*)aColor;
@end
实例化:
#import <Foundation/Foundation.h>
#import "ASShape.h"
int main(int argc, const char *argv[])
{
@autoreleasepool {
ASGraphic *graphic = [[ASGraphic alloc] initWithOrigin:CGPointMake(10, 9)];
ASShape *shape = [[ASShape alloc] initWithOrigin:CGPointMake(90, 34) color:@"red"];
[graphic release];
[shape release];
}
return 0;
}