2 回答

TA貢獻1829條經(jīng)驗 獲得超6個贊
方法:通過NSHomeDirectory獲得文件路徑
代碼如下:
NSString *homeDirectory = NSHomeDirectory();
NSString *fileDirectory = [homeDirectory stringByAppendingPathComponent:@"temp/app_data.plist"];
1.//使用NSSearchPathForDirectoriesInDomains檢索指定路徑
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];z
NSString *fileDirectory = [documentsDirectory stringByAppendingPathComponent:@"file.txt"];
2.//使用Foundation中的NSTemporaryDirectory函數(shù)直接返回代表temp文件夾的全路徑的字符串對象
NSString *tempDirectory = NSTemporaryDirectory();
NSString *file = [tempDirectory stringByAppendingPathComponent:@"file.txt"];
實現(xiàn)例子如下:
NSArray *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *docDir = [path objectAtIndex:0];
NSLog(@"filepath:%@",docDir);
NSString *str = @"hello.jpg";
NSString *filepath = [docDir stringByAppendingPathComponent:str];
//NSString *filepath = [docDir stringByAppendingPathComponent:[NSString stringWithUTF8String:"///mest.txt"]];
NSLog(@"filepath:%@",filepath);
BOOL success = [[NSFileManager defaultManager]createFileAtPath: filepath contents:nil attributes:nil];
NSLog(@"result",success);
printf("Create File:%s %s.",[filepath UTF8String], success ? "Success" : "Error");
NSString* reValue= [NSString stringWithString:@"\\"success\\""];

TA貢獻1876條經(jīng)驗 獲得超6個贊
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"fileName" ofType:@"plist"];
NSString *fullpath = [NSString stringWithFormat:@"%@%@%@",plistPath,@"/",@"fileName.plist"];
你想獲取的應(yīng)該就是模擬器的路徑。模擬器運行時工作區(qū)域就是模擬器的工作區(qū)域,所以獲取的路徑就是運行在模擬器中的文件的路徑,同樣道理在設(shè)備上獲取的就是設(shè)備上的文件的路徑。你要想在模擬設(shè)備上獲取xcode工程中的文件,這不符合道理 邏輯上有問題。
- 2 回答
- 0 關(guān)注
- 1154 瀏覽
添加回答
舉報