1. 程式人生 > >IOS 開發學習十二 Bundle使用及檔案引用

IOS 開發學習十二 Bundle使用及檔案引用

1.在xcode外建立一個資料夾,命令為app.bundle(必須為.bundle副檔名)

以後開啟此資料夾,要滑鼠右鍵-顯示包內容,直接雙擊不行。

2.加到xcode專案裡

3.程式碼獲取裡面檔案路徑:

資源路徑:

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

檔案路徑:
  NSString *url=[[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:@"app.bundle/datas.sqlite"];

上一級路徑:
NSString *secondParentPath = [[bundlePath stringByDeletingLastPathComponent] stringByDeletingLastPathComponent]

不通過bundle獲取資源:
UIImage *img=[UIImage imageNamed:@"cellicon.png"];

獲取自定義的檔案路徑:
    NSString *newPath=[NSString stringWithFormat:@"%@%@%@",[[NSBundle mainBundle]resourcePath],@"/",@"test.txt"];
    NSFileHandle *url2=[NSFileHandle fileHandleForReadingAtPath:newPath];

    NSString *newPath=[[NSBundle mainBundle] pathForResource:@"test" ofType:@"txt"];
    NSFileHandle *fileHandle=[NSFileHandle fileHandleForReadingAtPath:newPath];