您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 訊息 >> iOS 相關資訊

iOS 開發雜記(三):實用小技巧彙編

編輯:iOS 相關資訊

1. 隱藏狀態欄

[[UIApplication sharedApplication] setStatusBarHidden:YES];

以下是一些日常開發中常用到的代碼片段,涉及隨機數、音樂播放、定時器、線程、文件路徑、時間格式化與 plist 讀寫等,總有一條是你用得上的。

1、取隨機數:

NSData *datanow = [NSData data]; int i = (int)datanow; srand(i); rand();

2、播放音樂:

-(void) playMusic { @try{ NSString *musicFilePath = [[NSBundle mainBundle] pathForResource:@"startLogo" ofType:@"mp3"]; NSURL *musicURL = [[NSURL alloc] initFileURLWithPath:musicFilePath]; musicPlayer= [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL error:nil]; [musicURL release]; musicPlayer.numberOfLoops= 0; [musicPlayer play]; } @catch(NSException* e) { } }

3、每隔 0.8 秒執行 timeCount 方法:

NSTimer *countTimer; countTimer= [NSTimer scheduledTimerWithTimeInterval: 0.8 target: self selector: @selector(timeCount:) userInfo: nil repeats: YES]; [countTimer fire];

4、延遲 1 秒執行 test 方法:

[self performSelector:@selector(test) withObject:nil afterDelay:0.1];

5、啟動線程:

[NSThread detachNewThreadSelector:@selector(transImage) toTarget:self withObject:nil]; timer=[NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(TimerClock:) userInfo:nil repeats:YES]; [timer fire];

6、獲取文件路徑:

//通過 NSHomeDirectory 獲得文件路徑 NSString *homeDirectory = NSHomeDirectory(); NSString *fileDirectory = [homeDirectory stringByAppendingPathComponent:@"temp/app_data.plist"];

//使用 NSSearchPathForDirectoriesInDomains 檢索指定路徑 NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [path objectAtIndex:0]; NSString *fileDirectory = [documentsDirectory stringByAppendingPathComponent:@"file.txt"];

//使用 Foundation 中的 NSTemporaryDirectory 函數直接返回代表 temp 文件夾的全路徑字符串對象 NSString *tempDirectory = NSTemporaryDirectory(); NSString *file = [tempDirectory stringByAppendingPathComponent:@"file.txt"];

7、文件與文件夾操作:

//如果路徑不存在則創建 if(![[NSFileManager defaultManager] fileExistsAtPath:themePath]) { [[NSFileManager defaultManager] createDirectoryAtPath:themePath attributes:nil]; } //刪除已存在的同名文件夾 if([[NSFileManager defaultManager] fileExistsAtPath:savePath]) { [[NSFileManager defaultManager] removeItemAtPath:savePath error:NULL]; }

8、子線程拋給主線程:

[self performSelectorOnMainThread:@selector(shiftView) withObject:nil waitUntilDone:YES];

9、獲取當前時間:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"]; NSString *locationString=[formatter stringFromDate: [NSDate date]];

注意:大寫的 H 日期格式預設為 24 小時制,小寫的 h 則預設為 12 小時制。日期格式符說明:y 表示年、M 表示月、d 表示日、E 表示星期、H/k/K/h 表示小時(不同制式)、m 表示分、s 表示秒、S 表示毫秒、z/Z 表示時區。

讀取和寫入 plist 文件

plist 文件是標準的 xml 文件,在 cocoa 中可以很簡單地使用,以下代碼在 Mac 和 iPhone 中均適用。

寫入 plist 文件:

NSMutableDictionary* dict = [ [ NSMutableDictionary alloc ] initWithContentsOfFile:@"/Sample.plist"]; [ dict setObject:@"Yes" forKey:@"RestartSpringBoard"]; [ dict writeToFile:@"/Sample.plist" atomically:YES];

讀取 plist 文件:

NSMutableDictionary* dict = [ [ NSMutableDictionary alloc ] initWithContentsOfFile:@"/Sample.plist"]; NSString* object = [ dict objectForKey:@"RestartSpringBoard" ];

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved