2011-10-02 15 views
7

मैं इस सवाल का जवाब plistप्लिस्ट में डेटा कैसे लिखें?

How to write data to the plist?

लेकिन अब तक मेरे plist बिल्कुल नहीं बदला करने के लिए डेटा लिखने के लिए पालन किया है।

यहाँ मेरी कोड है: -

- (IBAction)save:(id)sender 
{ 
    NSString *path = [[NSBundle mainBundle] pathForResource:@"drinks" ofType:@"plist"]; 
    NSString *drinkName = self.name.text; 
    NSString *drinkIngredients = self.ingredients.text; 
    NSString *drinkDirection = self.directions.text; 
    NSArray *values = [[NSArray alloc] initWithObjects:drinkDirection, drinkIngredients, drinkName, nil]; 
    NSArray *keys = [[NSArray alloc] initWithObjects:DIRECTIONS_KEY, INGREDIENTS_KEY, NAME_KEY, nil]; 
    NSDictionary *dict = [[NSDictionary alloc] initWithObjects:values forKeys:keys]; 
    [self.drinkArray addObject:dict]; 
    NSLog(@"%@", self.drinkArray); 
    [self.drinkArray writeToFile:path atomically:YES]; 
} 

मैं कुछ अतिरिक्त प्रदर्शन करने की जरूरत है?

मैं आईफोन एसडीके में नया हूं इसलिए किसी भी मदद की सराहना की जाएगी।

उत्तर

35

आप फ़ाइल को अपने एप्लिकेशन बंडल में लिखने की कोशिश कर रहे हैं, जो संभव नहीं है। फ़ाइल को इसके बजाय दस्तावेज़ फ़ोल्डर में सहेजें।

NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
path = [path stringByAppendingPathComponent:@"drinks.plist"]; 

पथफॉर रिसोर्स विधि का उपयोग केवल उन संसाधनों को पढ़ने के लिए किया जा सकता है जिन्हें आपने एक्सकोड में अपनी परियोजना में जोड़ा था।
1. कॉपी पहले प्रक्षेपण पर ऐप के दस्तावेज़ फ़ोल्डर के लिए आपके आवेदन बंडल से drinks.plist (NSFileManager उपयोग करते हुए):

यहाँ आप आम तौर पर जब आप अपने अनुप्रयोग में एक plist संशोधित करना चाहते हैं कि क्या करना है।
2. पढ़ने/लिखने के दौरान केवल दस्तावेज़ फ़ोल्डर में फ़ाइल का उपयोग करें।

NSString *destPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; 
destPath = [destPath stringByAppendingPathComponent:@"drinks.plist"]; 

// If the file doesn't exist in the Documents Folder, copy it. 
NSFileManager *fileManager = [NSFileManager defaultManager]; 

if (![fileManager fileExistsAtPath:destPath]) { 
    NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"drinks" ofType:@"plist"]; 
    [fileManager copyItemAtPath:sourcePath toPath:destPath error:nil]; 
} 

// Load the Property List. 
drinkArray = [[NSArray alloc] initWithContentsOfFile:destPath]; 
+0

धन्यवाद आपके उत्तर के लिए एक बहुत:

अद्यतन

इस तरह आप drinkArray संपत्ति को प्रारंभ होता है। मैं सिर्फ आईफोन एसडीके सीख रहा हूं, तो क्या आप कृपया मुझे बता सकते हैं कि इसे पहली लॉन्च में कैसे कॉपी करें। मेरे ऐप बंडल में मेरे .plist को कॉपी करने के लिए मुझे कोड कहां रखना चाहिए? और मुझे कैसे पता चलेगा कि इसे सफलतापूर्वक कॉपी किया गया है? धन्यवाद। – Varundroid

+3

मैंने अपने उत्तर में एक उदाहरण जोड़ा है। :) – chrisklaussner

+0

धन्यवाद बहुत ईसाईके। :) – Varundroid

संबंधित मुद्दे