NSSearchPathForDirectoriesInDomains函數詳解

NSSearchPathForDirectoriesInDomains函數詳解

 
 

 

 

#import "NSString+FilePath.h"

 

@implementation NSString (FilePath)

//檢索指定路徑

//第一個參數指定了搜索的路徑名稱,NSDocumentDirectory表示是在Documents中尋找.NSCacheDirectory的話就是在cache文件中尋找.第二個參數限定了文件的檢索範圍只在沙箱內部.其意義爲用戶電腦主目錄.也能夠修改成網絡主機等.最後一個參數決定了是否展開波浪線符號.展開後纔是完整路徑,這個布爾值一直爲YES.

//該方法返回值爲一個數組,在iphone中因爲只有一個惟一路徑(相對OC而言),因此直接取數組第一個元素便可.

+ (NSString *)documentsPath

{

    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES)
objectAtIndex:0];

}

 

//在上一步得到的路徑後面加上文件的名字(用字符串的拼接方法).這樣使用的返回路徑就能夠找到指定文件,文件的讀取和寫入便可以順利完成了.

+ (NSString *)filePathAtDocumentWithFileName:(NSString *)fileName

{

   
NSString *filePath = [[self
documentsPath]stringByAppendingPathComponent:fileName];

   
return filePath;

}

@end
相關文章
相關標籤/搜索