2 回答
TA贡献1829条经验 获得超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函数直接返回代表temp文件夹的全路径的字符串对象
NSString *tempDirectory = NSTemporaryDirectory();
NSString *file = [tempDirectory stringByAppendingPathComponent:@"file.txt"];
实现例子如下:
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条经验 获得超6个赞
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"fileName" ofType:@"plist"];
NSString *fullpath = [NSString stringWithFormat:@"%@%@%@",plistPath,@"/",@"fileName.plist"];
你想获取的应该就是模拟器的路径。模拟器运行时工作区域就是模拟器的工作区域,所以获取的路径就是运行在模拟器中的文件的路径,同样道理在设备上获取的就是设备上的文件的路径。你要想在模拟设备上获取xcode工程中的文件,这不符合道理 逻辑上有问题。
- 2 回答
- 0 关注
- 1049 浏览
添加回答
举报