2011-09-21 6 views
0

내 프로젝트에서 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 mainbundle"); 
    appDPath = [mainBundle pathForResource:@"MyAppSettings" ofType:@"plist"]; 
    NSLog(@"appDataPath: %@", appDPath); 
} 

return appDPath; 
} 

경우 (mainBundle! = nil) 있지만 appDPath null입니다. 동일한 기능이 다른 프로젝트에서 작동합니다. 왜 여기서 일하지 않는거야? 프로젝트에서 plist 파일의 경로를 가져 오는 다른 방법이 있습니까? 미리 감사드립니다.

+0

실제로 프로젝트의 리소스에'MyAppSettings.plist' 파일이 있습니까? – DarkDust

+0

예. 내 프로젝트의 리소스에는 MyAppSettings.plist가 있습니다. – Piscean

답변

1

는 시도이

+ (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  
} 
+0

그림이 없습니다. 뭐가 문제 야? 나는 리소스에 plist MyAppSettings.plist 있습니다. 대소 문자 구분도 확인했습니다. – Piscean

1

이 코드를 사용하여 프로젝트에서 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]; 
    } 
} 
+0

경로를 초기화하지 않았습니다. – Piscean

+0

그리고 소스 경로가 nil 인 예외가 있습니다 – Piscean

+0

경로에 데이터 유형 Nsstring이 있으므로 .h 파일에서 선언하십시오. 초기화 할 필요가 없습니다. – Sonu

2

케이스를 확인하십시오. iOS 파일 시스템은 대소 문자를 구분합니다.

파일이 디렉토리에 있지 않은지 확인하십시오. 이 경우 pathForResource:ofType:inDirectory으로 전화해야합니다.

작성중인 대상에 대해 파일을 사용할 수 있는지 확인하십시오. 그렇지 않으면 번들로 복사되지 않습니다.

2

당신이 하위 폴더에있는 plist가있는 어떤 기회에 의해, 당신은뿐만 아니라 디렉토리를 지정하여

[[NSBundle mainBundle] pathForResource: ofType: inDirectory:] 

사용을 고려해야합니다.

관련 문제