10.1 基础语法与使用

10.1.1 函数调用

  1. 调用对象的方法
  [object method];
  [object methodWithInput:input];
  1. 方法包含返回值
  output = [object methodWithInput];
  output = [object methodWithInput:input];
  1. 调用类方法, 并返回NSString对象, 其中id为运行时类型
id myObject = [NSString string];
// 也可以写为
NSString* myString = [NSString string];
  1. 嵌套消息调用
[NSString stringWithFormat:[prefs format]];
  1. 多参数定义
-(BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;

// 调用
BOOL result = [myData writeToFile:@"/tmp/log.txt" atomically:NO];
  1. 访问setter
[photo setCaption:@"Day Day up"];
output = [photo caption];
photo.caption = @"Day Day up";
output = photo.caption;
  1. 创建对象
// 自动回收...
NSString* myString = [NSString string];
// 自己申请内存, 需要调释放
NSString* myString = [[NSString alloc] init];
// 释放内存
[myString release];
  1. 定义类接口
#import <Cocoa/Cocoa.h>

@interface Photo : NSObject {
  NSString* caption;
  NSString* photographer;
}

// getter方法定义
- caption;
- photographer;
// 并指定返回类型版本
// - (NSString*) caption;
// - (NSString*) photographer;

// setter方法定义
- (void) setCaption:(NSString*)input;
- (void) setPhotographer:(NSString*)input;
@end
  1. 实现接口
#import <Photo.h>
@implementation photo

- (NSString*) caption {
  return caption;
}
- (NSString*) photographer {
  return photographer;
}

// 没有使用GC, 则需要调用`release`释放老对象, 和使用`retain`保存新对象
- (void) setCaption: (NSString*)input
{
  [caption autorelease];
  caption = [input retain];
}

- (void) setPhotographer: (NSString)input
{
  [photographer autorelease];
  photographer = [input retain];
}

// 使用自动GC
- (void) setCaption: (NSString)input
{
  caption = input;
}
@end
  1. init
- (id)init
{
  if(self = [super init])
  {
    [self setCaption:@"default"];
    [self setPhotographer:@"default"];
  }
  return self;
}
  1. Dealloc, 对象被清理时, 清理属性
- (void)Dealloc
{
  [caption release];
  [photographer release];
  [super dealloc];
}
  1. 内存管理
  2. Properties
#import <Cocoa/Cocoa.h>
@interface Photo : NSObject
{
  NSString* caption;
  NSString* photographer;
}
@Property (retain) NSString* caption;
@Property (retain) NSString* photographer;
@end
// 实现
#import <Photo.h>

@implementation Photo
// @synthesize 自动创建setter和getter
@synthesize caption;
@synthesize photographer;

- (void)dealloc
{
  [caption release];
  [photographer release];
  [super dealloc];
}
@end
  1. Categories, 在原有类基础上增加方法
#import <Cocoa/Cocoa.h>

@interface NSString(Utilities)
- (BOOL) isURL;
@end
#import <NSString-Utilities.h>
@implementation NSString(Utilities)
- (BOOl) isURL
{
  if([self hasPrefix:@"http//"])
    return YES;
  else
    return NO;
}
@end
  1. 类方法
@implementation Photo
+ (void) hello {
  NSLog(@"hello..");
}
@end
  1. Blocks(匿名函数)

double result = multiplyTowValues(2, 4);

- 使用Typeof定义匿名函数

#import<Foundation/Foundation.h>

typeof void(^CompletionBlock)(); @interface SamleClass:NSObject {

@implementation SampleClass

int main() { SampleClass sampleClass = [[SamleClass alloc] init]; [sampleClass performActionWithCompletionBlock:^ { NSLog(@”hello CompletionBlock”); }]; return 0; }

17. protocol(实现多继承) 和 delegate

@protocol ProtocolDelegate

// 必须实现的方法, 不实现会警告但可运行 @required

// 可选实现方法 @option


### 10.2 Foundation Kit
1. NSString
2. NSArray
3. NSDictionary
4. NSNumber
5. NSRange

### 10.3 Cocoa Touch
1. UITableView
  - 风格包含UITableViewStylePlain(不分组)和UITableViewStyleGroup(分组)
2. UIImage
3. UICollectionView
4. UIScrollView
5. UINavigationView
6. UIButtom
7. UILabel
8. WebKit


### 10.4 Animation

### 10.5 调试技巧
1. Instruments
  - Allocations
  - Leaks
  - Activity Monitor
2. GDB, LLDB

### 10.6 TCP/Http
1. NSURL
2. NSHTTP

### 10.7 多线程与锁
1. NSThread
2. @synchronized
3. CFRunLoop
4. Grand Central Dispatch(GCD)
5. pthread_mutex
6. NSOperationQueue

### 10.8 数据库访问
1. Core Data
2. SQLite

### 10.3 ViewController
1. `AppDelete`, 即应用委托, 在`didFinishLaunchingWithOptions()`方法可以配置默认启动`ViewController`, 需要`UIWindow`承载`ViewController`
2. `storyboard`可拖拽`View`, 关联`ViewController`
3. 生命周期
  - loadView(加载View)
  - viewDidLoad(view加载完毕)
  - viewWillAppear(view将要显示)
  - viewWillLayoutSubviews(将布局子控件)
  - viewDidLayoutSubviews(子控件布局完成)
  - viewDidAppear(view完全显示)
  - viewWillDisappear(view即将消失)
  - viewDidDisappear(view完全消失)
  - viewDidUnload(内存不足时)
5. ViewController跳转
  - `UINavigationController`调用`pushViewController`进行跳转, 采用压栈和出栈的方式进行`Controller`的管理, `popViewControllerAnimated`返回

  ```Objective-C
  DemoViewController *viewController = [[DemoViewController alloc]init];
  [self.navigationController pushViewController : viewController animated:YES];
  [viewController release];

10.2 参考