0

我有 40 个项目的数组,其中 dict.Dict 包含 2 个字符串项目,当用户单击 uibutton 时,我想将字符串项目写入 0 到 1

<array>
    <dict>
        <key>quote</key>
        <string>Some Quotes</string>
        <key>favorite</key>
        <string>0</string>
    </dict>

</array>

我正在应用以下代码,但它对我不起作用...

-(void) addFavorite
{
    NSLog(@"Hello");

    [someButton setImage:[UIImage imageNamed:@"add_favoritegold.png"] forState:UIControlStateHighlighted];
    NSURL *documents = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
    NSURL *plistUrl = [documents URLByAppendingPathComponent:@"quotes.plist"];
    NSArray *plistContents = [NSArray arrayWithContentsOfURL:plistUrl];
    NSMutableDictionary *editFavorite = [plistContents objectAtIndex:1];
}

它不会将最喜欢的 0 更改为 1,并且我的 plist 在支持文件中,我是否必须在属性列表中进行任何更改。

4

4 回答 4

1
-(void)btnClicked
{
    [arr_Data addObject:str1];
    [arr_Data addObject:str2];

    // get paths from root direcory
    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);

   // get documents path
   NSString *documentsDirectory = [paths objectAtIndex:0];

   // get the path to our Data/plist file
   NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Data.plist"];

   // This writes the array to a plist file. If this file does not already exist, it creates a new one.
   [arr_Data writeToFile:path atomically: TRUE];
}
于 2013-04-02T13:00:46.797 回答
1

请参阅下面的代码以了解您的概念

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
      NSString  *plistdocumentsDirectory = [paths objectAtIndex:0]; 
    NsString * plistFilePath = [plistdocumentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"filename.plist"]];
  NSMutableArray  *savedStock = [[NSMutableArray alloc] initWithContentsOfFile:plistFilePath];
     [[savedStock objectAtIndex:indexForWheretoStoreData] setObject:@"your value" forKey:@"your key"];
  [savedStock writeToFile:filePath  atomically:YES];
于 2013-04-03T08:54:14.277 回答
0

这是一个简单的 4 步过程:

1)选择词典(您要更新“收藏夹”标签)。

NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *yourPath = [documentsDirectory stringByAppendingPathComponent:@"yourPlist.plist"];

2)设置新值。

[yourDict setValue:@"1" forKey:@"favorite"];

3)将其插入阵列。

NSMutableArray *yourArray = [[NSMutableArray alloc] init];
[yourArray addObject:yourDict];

4)将该数组写入yourPath

[yourArray writeToURL:yourPath atomically:YES];
于 2013-04-02T13:00:54.997 回答
0

用于[yourArray writeToURL:yourPath atomically:YES];存储数据

于 2013-04-03T07:07:04.203 回答