2014
Mar
04
初學 IOS 很多基本的功能都不會使用,我習慣把一些好行的程式,包成一個簡單使用的 function。
第一個功能,讀取檔案內容。
readFile
- NSError *error;
- NSString* content = [NSString stringWithContentsOfFile:filename
- encoding:NSUTF8StringEncoding
- error:&error];
- if(error) {
- NSLog(@"error = %@", error);
- }
取得當前目錄
NSString *curDir = [[NSFileManager defaultManager] currentDirectoryPath];取得 app 執行目錄
NSString *myPath = [[NSBundle mainBundle] bundlePath];儲存資料至 IOS
Example
- //存資料
- [[NSUserDefaults standardUserDefaults]
- setObject:[NSString stringWithFormat:@"test"] forKey:@"score"];
- //同步
- [[NSUserDefaults standardUserDefaults] synchronize];
- //取資料
- NSString *score = [[NSUserDefaults standardUserDefaults]
- stringForKey:@"score"];
- NSLog(@"score = %@",score);
移除小鍵盤
Example
- [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
Timer
Example
- [NSTimer scheduledTimerWithTimeInterval:1.0
- target:self
- selector:@selector(setTime)
- userInfo:nil
- repeats:YES];
自動念單字 (機器發音)
Example
- NSString *name = @"Michael Jordan";
- NSLog(@"%@", name);
- AVSpeechUtterance *utterance = [AVSpeechUtterance
- speechUtteranceWithString:name];
- AVSpeechSynthesizer *synth = [[AVSpeechSynthesizer alloc] init];
- [synth speakUtterance:utterance];