[object method];
[object methodWithInput:input];
output = [object methodWithInput];
output = [object methodWithInput:input];
NSString
对象, 其中id
为运行时类型id myObject = [NSString string];
// 也可以写为
NSString* myString = [NSString string];
[NSString stringWithFormat:[prefs format]];
-(BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
// 调用
BOOL result = [myData writeToFile:@"/tmp/log.txt" atomically:NO];
setter
[photo setCaption:@"Day Day up"];
output = [photo caption];
photo.caption = @"Day Day up";
output = photo.caption;
// 自动回收...
NSString* myString = [NSString string];
// 自己申请内存, 需要调释放
NSString* myString = [[NSString alloc] init];
// 释放内存
[myString release];
#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
#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
init
- (id)init
{
if(self = [super init])
{
[self setCaption:@"default"];
[self setPhotographer:@"default"];
}
return self;
}
Dealloc
, 对象被清理时, 清理属性- (void)Dealloc
{
[caption release];
[photographer release];
[super dealloc];
}
alloc
-> retain
-> release
-> release
, 调用了retain
, 需要多调用一次release
释放对象#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
getter
, setter
#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
@implementation Photo
+ (void) hello {
NSLog(@"hello..");
}
@end
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];
UIViewController#presenterModalViewController
进行跳转, #dismissViewControllerAnimated