1. 委派模式
// A.h
@protocol ViewControllerADelegate
- (void)changeBgColorFromCtrlA:(ViewControllerA*)aView withColor:(UIColor*)color;
@end
@interface ViewControllerA : UIViewController
@property (nonatomic, weak) id delegate;
- (IBAction)changeColorAction:(id)sender;
@end
// A.m
- (IBAction)changeColorAction:(id)sender {
[self.delegate changeBgColorFromCtrlA:self withColor:[UIColor grayColor]];
[self.navigationController popViewControllerAnimated:YES];
}
// B.m 實作協議
- (void)changeBgColorFromCtrlA:(ViewControllerA*)aView withColor:(UIColor*)color {
self.view.backgroundColor = color;
}
2. 通知中心
// A.m 發送
[[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeColorKey"
object:[UIColor greenColor]];
// B.m 註冊與接收
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(changeColor:)
name:@"ChangeColorKey"
object:nil];
}
- (void)changeColor:(NSNotification*)note {
if (note.object) self.view.backgroundColor = note.object;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
3. NSString 常用操作
| 操作 | 方法 | 範例 |
| 建立 | `stringWithFormat:` | `NSString *s = [NSString stringWithFormat:@"%@%@", a, b];` |
| 比較 | `isEqualToString:` | `[a isEqualToString:b]` |
| 前綴/後綴 | `hasPrefix:` / `hasSuffix:` | `[a hasPrefix:@"https://"]` |
| 忽略大小寫 | `caseInsensitiveCompare:` | `[a caseInsensitiveCompare:b] == NSOrderedSame` |
| 分割 | `componentsSeparatedByString:` | `[@"a,b,c" componentsSeparatedByString:@","] → @[@"a",@"b",@"c"]` |
| 合併陣列 | `componentsJoinedByString:` | `[@[@"a",@"b"] componentsJoinedByString:@","] → @"a,b"` |
| 大小寫轉換 | `uppercaseString` / `lowercaseString` | |
| 子字串 | `substringToIndex:` `substringFromIndex:` `substringWithRange:` | `[s substringWithRange:NSMakeRange(0,4)]` |
| 路徑副檔名 | `pathExtension` | `[@"file.txt" pathExtension] → @"txt"` |
| 檔案讀寫 | `initWithContentsOfFile:` `writeToFile:atomically:` | |
4. NSArray / NSMutableArray
// 建立
NSArray *arr = @[@"a", @"b", @"c"]; // 字面值語法
NSMutableArray *mar = [NSMutableArray arrayWithArray:arr];
// 快速枚舉 (推薦)
for (id obj in mar) { ... }
// 列舉器 (反向)
NSEnumerator *e = [mar reverseObjectEnumerator];
while (id obj = [e nextObject]) { ... }
// 複製 (深複製需元素支援 NSCopying)
NSMutableArray *copy = [NSMutableArray arrayWithArray:mar];
// 排序
[mar sortUsingSelector:@selector(compare:)];
// 自訂比較
[mar sortUsingComparator:^NSComparisonResult(id a, id b) { return [a compare:b]; }];
5. NSDictionary / NSMutableDictionary
NSDictionary *dict = @{@"key1": @"val1", @"key2": @"val2"};
NSString *v = dict[@"key1"]; // 現代語法
[mutableDict setObject:@"new" forKey:@"key1"];
[mutableDict removeObjectForKey:@"key2"];
6. 檔案目錄枚舉
NSFileManager *fm = [NSFileManager defaultManager];
NSString *home = @"/Users"; // 或 NSHomeDirectory()
NSDirectoryEnumerator *dirEnum = [fm enumeratorAtPath:home];
NSMutableArray *jpgFiles = [NSMutableArray array];
for (NSString *fname in dirEnum) {
if ([[fname pathExtension] isEqualToString:@"jpg"])
[jpgFiles addObject:fname];
}
// 或舊式 while 迴圈
while (NSString *fname = [dirEnum nextObject]) { ... }
7. NSData 關鍵轉換
- `[NSString dataUsingEncoding:NSUTF8StringEncoding]` → NSData
- `[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]` → NSString
- `[data bytes]` → `const void*`
- `[data length]` → `NSUInteger`