Iphone 为什么我不能在我的项目中获取plist文件的路径?
我正在使用此函数获取项目中plist文件的路径:Iphone 为什么我不能在我的项目中获取plist文件的路径?,iphone,objective-c,xcode,ios4,plist,Iphone,Objective C,Xcode,Ios4,Plist,我正在使用此函数获取项目中plist文件的路径: + (NSString *) appDataPath{ NSLog(@"in appDataPath"); NSString *appDPath = nil; // Get the main bundle for the app. NSBundle* mainBundle = [NSBundle mainBundle]; if (mainBundle != nil) { NSLog(@"in appDataPath mainbundl
+ (NSString *) appDataPath{
NSLog(@"in appDataPath");
NSString *appDPath = nil;
// Get the main bundle for the app.
NSBundle* mainBundle = [NSBundle mainBundle];
if (mainBundle != nil) {
NSLog(@"in appDataPath mainbundle");
appDPath = [mainBundle pathForResource:@"MyAppSettings" ofType:@"plist"];
NSLog(@"appDataPath: %@", appDPath);
}
return appDPath;
}
如果(mainBundle!=nil)但appDPath为null,则会进入。同样的功能也在另一个项目中工作。为什么它不在这里工作?是否有其他方法可以获取项目中plist文件的路径。提前感谢。我正在使用此代码在我的项目中查找plist路径
-(void)CreatePlistPath
{
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
path = [[documentsDirectory stringByAppendingPathComponent:@"data.plist"] retain];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path])
{
NSString *bundle =[[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
[fileManager copyItemAtPath:bundle toPath: path error:&error];
}
}
我使用这段代码在我的项目中查找plist路径
-(void)CreatePlistPath
{
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
path = [[documentsDirectory stringByAppendingPathComponent:@"data.plist"] retain];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path])
{
NSString *bundle =[[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
[fileManager copyItemAtPath:bundle toPath: path error:&error];
}
}
检查一下箱子。iOS的文件系统区分大小写 检查文件是否不在目录中。在这种情况下,您需要调用
pathForResource:ofType:inDirectory
检查是否为正在生成的目标启用了该文件。如果不是,它将不会被复制到捆绑包中。检查案例。iOS的文件系统区分大小写 检查文件是否不在目录中。在这种情况下,您需要调用
pathForResource:ofType:inDirectory
检查是否为正在生成的目标启用了该文件。如果不是,它将不会被复制到捆绑包中。试试这个
+ (NSString *) appDataPath{
NSString *plistPath;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory,
NSUserDomainMask,
YES) objectAtIndex:0];
plistPath = [rootPath stringByAppendingPathComponent:@"MyAppSettings.plist"];
if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
plistPath = [[NSBundle mainBundle] pathForResource:@"MyAppSettings"
ofType:@"plist"];
return plistPath;
}
else
// No plist found
}
试试这个
+ (NSString *) appDataPath{
NSString *plistPath;
NSString *rootPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory,
NSUserDomainMask,
YES) objectAtIndex:0];
plistPath = [rootPath stringByAppendingPathComponent:@"MyAppSettings.plist"];
if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
plistPath = [[NSBundle mainBundle] pathForResource:@"MyAppSettings"
ofType:@"plist"];
return plistPath;
}
else
// No plist found
}
如果你在子文件夹中有你的PLIST,那么你应该考虑使用
[[NSBundle mainBundle] pathForResource: ofType: inDirectory:]
通过指定目录, 如果你在子文件夹中有你的PLIST,那么你应该考虑使用
[[NSBundle mainBundle] pathForResource: ofType: inDirectory:]
通过同时指定目录。在项目的资源中是否确实存在文件
MyAppSettings.plist
。在我的项目的资源中有MyAppSettings.plist。在你的项目的资源中确实有一个文件MyAppSettings.plist
?是的。在我的项目的资源中有MyAppSettings.plist,但有一个例外,源路径为nilpath,数据类型为Nsstring,所以只需在.h文件中声明它即可。。不需要初始化。并且存在一个例外,即源路径为nilpath具有数据类型Nsstring,所以只需在.h文件中声明它。。不需要初始化。未找到plist。有什么问题吗?我在参考资料中有plist MyAppSettings.plist。我还检查了病例敏感性,没有发现plist。有什么问题吗?我在参考资料中有plist MyAppSettings.plist。我还检查了病例敏感性。