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];
回應 (Leave a comment)