2009-05-07 6 views
0

plist 파일에 저장된 배열 데이터를 검색하는 방법은 무엇입니까?plist 파일에 저장된 배열 데이터 검색

PLIST 파일과 소스 코드를 아래 그림에 표시,

------------------------plist File------------------------- 
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
    <key>Name</key> 
    <string>abcxxx</string> 
    <key>Phones</key> 
    <array/> 
    <key>item1</key> 
    <string>121327777</string> 
    <key>item2</key> 
    <string>333333333</string> 
</dict> 
</plist> 
--------------------------------------------------------- 



    -(id) init 
{ 


    NSString *errorDesc = nil; 
    NSPropertyListFormat format; 
    phoneNumbers =[[NSMutableArray alloc]init]; 
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"testfile" ofType:@"plist"]; 
    NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath]; 

    NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization 
              propertyListFromData:plistXML 
              mutabilityOption:NSPropertyListMutableContainersAndLeaves 
              format:&format errorDescription:&errorDesc]; 

    if(!temp){ 
     NSLog(errorDesc); 
     [errorDesc release]; 
    } 
    self.personName = [temp objectForKey:@"Name"]; 
    self.phoneNumbers = [NSMutableArray arrayWithArray:[temp objectForKey:@"Phones"]]; 
    NSLog(@"Label executed %@",[phoneNumbers objectAtIndex:0]); 

    UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(-180, 100, 100, 20)]; 
    name.text = [phoneNumbers objectAtIndex:1]; 
    [self addChild:name]; 
    NSLog(@"Label executed %@",name); 
+0

당신은 실제 문제가 무엇인지 말을하지 않습니다, 당신의 PLIST 구조를 확인하십시오 PLIST를 읽을 수 없습니다. 증상은 무엇입니까? –

답변

1

귀하의 PLIST의 XML 조금 의심스러운. 가지고있는 <array/> 태그는 독립 실행 형 태그이므로 유용하지는 않습니다.

아마 이런 식으로 PLIST을 변경

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
    <key>Name</key> 
    <string>abcxxx</string> 
    <key>Phones</key> 
    <array> 
     <string>121327777</string> 
     <string>333333333</string> 
    </array> 
</dict> 
</plist> 

나는이 코드 승/도움이 될 것입니다 있는지 확실하지 않다. 이 문서를 읽었습니까? 방법 :

http://developer.apple.com/documentation/Cocoa/Conceptual/PropertyLists/Introduction/Introduction.html

0

당신이해야 할 모든 NSDictionary와의 initWithContentsOfFile를 호출합니다. 예를 들어,

NSDictionary *temp = [[NSDictionary alloc] initWithContentsOfFile:plistPath]; 
self.personName = [temp objectForKey:@"Name"]; 
self.phoneNumbers = [[temp objectForKey:@"Phones"] mutableCopy]; 

그러나 위에서 언급 한 것처럼 plist는 조금 의심스러워 보이지만 Andy White도 제공 한 것으로 변경하십시오.

감사합니다,

카일

+0

그렇다고해서 NSPropertyListSerialization 기술이 모든 컨테이너와 나뭇잎을 변경할 수있는 것은 아닙니다. – dreamlax

0

의 UITextField의 프레임은 CGRectMake(-180, 100, 100, 20)가 오프 스크린 아마 (자기의 프레임에 따라)도 이상해.

0

대부분의 가능성이 단순히

NSString *StringsFromPList = [[NSBundle mainBundle] bundlePath]; 
NSString *itemPositionPlistLocation = [StringsFromPList stringByAppendingPathComponent:@"Words.plist"]; 
_myDictionary= [[NSDictionary alloc] initWithContentsOfFile:itemPositionPlistLocation]; 
NSArray * items = [_myDictionary objectForKey:@"Root"]; 


<plist version="1.0"> 
<dict> 
<key>Root</key> 
    <array> 
     <string>Happy</string> 
     <string>Rain</string> 
    </array> 

는 희망이 도움 :)

관련 문제