2014
Mar
04

初学 IOS 很多基本的功能都不会使用,我习惯把一些好行的程式,包成一个简单使用的 function。

第一个功能,读取档案内容。

readFile
  1. NSError *error;
  2. NSString* content = [NSString stringWithContentsOfFile:filename
  3. encoding:NSUTF8StringEncoding
  4. error:&error];
  5. if(error) {
  6. NSLog(@"error = %@", error);
  7. }

取得当前目录

NSString *curDir = [[NSFileManager defaultManager] currentDirectoryPath];

取得 app 执行目录

NSString *myPath = [[NSBundle mainBundle] bundlePath];

储存资料至 IOS

Example
  1. //存资料
  2. [[NSUserDefaults standardUserDefaults]
  3. setObject:[NSString stringWithFormat:@"test"] forKey:@"score"];
  4.  
  5. //同步
  6. [[NSUserDefaults standardUserDefaults] synchronize];
  7. //取资料
  8. NSString *score = [[NSUserDefaults standardUserDefaults]
  9. stringForKey:@"score"];
  10. NSLog(@"score = %@",score);

移除小键盘

Example
  1. [[[UIApplication sharedApplication] keyWindow] endEditing:YES];

Timer

Example
  1. [NSTimer scheduledTimerWithTimeInterval:1.0
  2. target:self
  3. selector:@selector(setTime)
  4. userInfo:nil
  5. repeats:YES];

自动念单字 (机器发音)

Example
  1. NSString *name = @"Michael Jordan";
  2. NSLog(@"%@", name);
  3. AVSpeechUtterance *utterance = [AVSpeechUtterance
  4. speechUtteranceWithString:name];
  5. AVSpeechSynthesizer *synth = [[AVSpeechSynthesizer alloc] init];
  6. [synth speakUtterance:utterance];

弹出一个视窗

UIAlertView

回應 (Leave a comment)