我把文件放在了工程目录下,跟其他.m .h文件在同一个目录,如何得到他们的路径?NSHomeDirectory()和[NSBundle mainbundle]得到的都不对,感觉得到的是模拟器的什么路径绝对路径当然是可以的,但我想要相对路径
方法:通过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];zNSString *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\\""];
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"fileName" ofType:@"plist"];NSString *fullpath = [NSString stringWithFormat:@"%@%@%@",plistPath,@"/",@"fileName.plist"];你想获取的应该就是模拟器的路径。模拟器运行时工作区域就是模拟器的工作区域,所以获取的路径就是运行在模拟器中的文件的路径,同样道理在设备上获取的就是设备上的文件的路径。你要想在模拟设备上获取xcode工程中的文件,这不符合道理 逻辑上有问题。