在plist文件中存储值时出错

时间:2013-04-01 09:44:57

标签: ios objective-c iphone cocoa-touch plist

我有一个步进器,在更改其值时,我需要将值存储在plist中。

所以根据saveAppPlist方法,我需要传递带有更改文件的NSMutableDictionary - > dictData

NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:dictData format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
if(plistData)
{
    [plistData writeToFile:plistPath atomically:YES];
}

这里是步进器的方法:

-(void)changeItemQuantityAtRow:(int)row
                       toValue:(double)value
{
    NSMutableDictionary *item = [[items objectAtIndex:row] mutableCopy];
    [item setObject:[NSNumber numberWithDouble:value] forKey:@"Quantity"];
    [items setObject:item atIndexedSubscript:row];
    NSLog(@"%@", items);
    NSMutableDictionary *plistDict = [NSDictionary dictionaryWithObjects:items forKeys:[NSArray arrayWithObjects:@"Name", @"Price", @"Quantity", nil]];
    NSLog(@"%@", plistDict);
    [self saveAppPlist:plistDict];
}

并且plist看起来像这样(这只是4个项目中的一个):

<dict>
    <key>Items</key>
    <array>
        <dict>
            <key>Name</key>
            <string>The zero item</string>
            <key>Price</key>
            <integer>133</integer>
            <key>Quantity</key>
            <integer>17</integer>
        </dict>

步进器正在改变物品的数量。但是当我尝试更改数量值时,我有这样的错误:

'NSInvalidArgumentException', reason: '*** -[NSDictionary initWithObjects:forKeys:]: count of objects (4) differs from count of keys (3)'

我理解错误的来源,但不知道如何解决此问题。谁能帮我?还有一个 - 在每次点击步进器后存储值可能不太好吗?

3 个答案:

答案 0 :(得分:2)

忘记要存储数组,将对象视为id实例。由于您希望使用密钥id items存储@"Items",您要做的是使用

//[NSDictionary dictionaryWithObject:<#(id)#> forKey:<#(id<NSCopying>)#>]
NSMutableDictionary *plistDict = [NSDictionary dictionaryWithObject:items forKey:@"Items"];

答案 1 :(得分:1)

这是一种简单的方法如果我想在pList文件中写一些数据

Plist文件名是“ Contacts.plist ” 存储

的值
  1. 名称
  2. 地址
  3. 电话号码
  4. 您可以在下面的链接中找到有关如何使用plist文件的编译教程

      

    How to use PList files in iPhone Tutorial

    以下是代码示例

    - (void)saveDataInPlist {
    
        //get the plist document path
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *plistFilePath = [documentsDirectory stringByAppendingPathComponent:@"Contacts.plist"];    
    
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSMutableDictionary *data = [[NSMutableDictionary alloc]init];
        NSMutableArray *contentArray= [[NSMutableArray alloc]init];
    
        if (![fileManager fileExistsAtPath: plistFilePath])
        {
            NSLog(@"File does not exist");
    
            // If the file doesn’t exist, create an empty plist file        
            plistFilePath = [documentsDirectory stringByAppendingPathComponent:@"Contacts.plist"];
            //NSLog(@"path is %@",plistFilePath);
    
        }
        else{
            NSLog(@"File exists, Get data if anything stored");
    
            contentArray = [[NSMutableArray alloc] initWithContentsOfFile:plistFilePath];       
        }       
    
    
        NSString *name = nameTextField.text;
        NSString *address = addressTextField.text;
        NSString *phone = phoneTextField.text;
    
    
        //add values to dictionary
        [data setValue:name forKey:@"Name"];
        [data setValue:address forKey:@"Address"];
        [data setValue:phone forKey:@"PhoneNo"];
    
        //add dictionary to array
        [contentArray addObject:data];
    
        //write array to plist file
        if([contentArray writeToFile:plistFilePath atomically:YES]){
    
           NSLog(@"saved");  
         }
         else {
           NSLog(@"Couldn't saved");
        }
        }
    
    }
    

    希望这会对你有所帮助。

答案 2 :(得分:1)

从我所看到的,问题与词典结构有关。 您提供items作为值(每个项可能包含NamePriceQuantity键),并提供item键作为键。 您可能需要将构造更改为: [NSDictionary dictionaryWithObjects:@[items] forKeys:@[@"Items"]]