2011-08-05 13 views
11

मैं एक सरणी में एक plist लोड करने के लिए कोड का एक टुकड़ा मैंने पहले सफलता के साथ उपयोग किया है का उपयोग कर रहा:पहुँच देता है अशक्त

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"species_names" ofType:@"plist"]; 
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath]; 

initWithContentsOf फ़ाइल विफल हो रहा है (सरणी रिक्त है , यह सुझाव देता है कि यह ऐप बंडल में प्लिस्ट नहीं ढूंढ सकता है)।

आईफोन सिम्युलेटर के तहत मैंने जांच की है कि पहली पंक्ति द्वारा बनाई गई पथ ऐप फ़ाइल (यह करता है) को इंगित करती है, और फाइंडर संदर्भ मेनू "पैकेज संकुल दिखाएं" का उपयोग करता है ताकि यह साबित हो सके कि "species_name.plist "वास्तव में बंडल में है - एक लंबाई के साथ जिसमें यह सुझाव देता है कि इसमें सामान शामिल है, इसलिए कार्य (और मेरे द्वारा लिखे गए अन्य आईओएस ऐप्स में) होना चाहिए। सुझाव सबसे स्वागत है ...

[एनवी एक्सकोड 4.2 बीटा, आईओएस 4.3.2]।

उत्तर

27

आपको उदाहरण की initWithContentsOfFile: विधि का उपयोग करना चाहिए।

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"species_names" ofType:@"plist"]; 
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:filePath]; 
NSArray *array = [NSArray arrayWithArray:[dict objectForKey:@"Root"]]; 

या इस:

NSString *errorDesc = nil; 
NSPropertyListFormat format; 
NSString *path = [[NSBundle mainBundle] pathForResource:@"startups" ofType:@"plist"]; 
NSData  *plistXML = [[NSFileManager defaultManager] contentsAtPath:path]; 
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization      
              propertyListFromData:plistXML 
mutabilityOption:NSPropertyListMutableContainersAndLeaves 
              format:&format 
              errorDescription:&errorDesc]; 
NSArray *array = [NSArray arrayWithArray:[temp objectForKey:@"Root"]]; 


संपादित

आप NSArray की writeToFile:atomically: विधि के साथ बनाई गई फ़ाइल के साथ NSArray की initWithContentsOfFile: विधि का उपयोग कर सकते हैं यहाँ नमूना है। writeToFile:atomically: के साथ बनाया plist इस संरचना है:

<plist version="1.0"> 
<array> 
    ...Content... 
</array> 
</plist> 

और plist XCode में बनाए गए इस संरचना है:

<plist version="1.0"> 
<dict> 
    ...Content... 
</dict> 
</plist> 

यही कारण है कि NSArray dosnt काम की initWithContentsOfFile: विधि।

+0

जो मुझे ट्रैक पर मिला, धन्यवाद। – RonC

2

इस प्रकार के plist:

enter image description here

या (मूल रूप से दोनों एक ही हैं)

enter image description here

इस तरह पढ़ा जा सकता है:

NSString *file = [[NSBundle mainBundle] pathForResource:@"appFeatures" ofType:@"plist"]; 
NSArray *_arrFeats = [NSArray arrayWithContentsOfFile:file]; 
NSLog(@"%d", _arrFeats.count); 

कहाँ appFeaturesplist का नाम है। (आईओएस 6,1)

0

इस सरल metod का प्रयास करें:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self plistData]; 
} 


- (void)plistData 
{ 
    NSString *file = [[NSBundle mainBundle] pathForResource:@"appFeatures" ofType:@"plist"]; 
    NSArray *messages = [NSArray arrayWithContentsOfFile:file]; 
    NSLog(@"%d", messages.count); 

    for (int i=0; i<messages.count; i++) 
    { 
     NSString *data = [messages objectAtIndex:i]; 
     NSLog(@"\n Data = %@",data); 
    } 
} 
संबंधित मुद्दे