Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8369805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:44:51+00:00 2026-06-09T13:44:51+00:00

I’m using a plist with structure like this: plist, array, dict key string key

  • 0

I’m using a plist with structure like this: plist, array, dict key string key string /dict, dict key string key string /dict, /array, /plist.

What I want with the following code it to set the “Done” string’s value in current dictionary to “Yes”.

But now, the code replaces the whole array of dictionaries with the strings of the current dictionary (with the “Done” key valued “Yes” as it should, though.)

What would be the correct code for what I want?

in DetailViewController.m:

-(IBAction)favoriteButtonPressed:(id)sender
{

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


[[detailsDataSource objectAtIndex: detailIndex] setValue:@"Yes" forKey:@"Done"];
[[detailsDataSource objectAtIndex: detailIndex] writeToFile:path atomically:YES];
}

The detailsDataSource and detailIndex are recieved from TableViewController.m segue if that matters.

TableViewController.m segue part:

    detailViewController.detailsDataSource = [[NSArray alloc]initWithArray:objectsArray];
    detailViewController.detailIndex = selectedRowIndex.row;

DetailViewController viewDidLoad

if ([[[detailsDataSource objectAtIndex: detailIndex] valueForKey:@"Favorite"] isEqual:@"Yes"])  {

    [favoriteButton setImage:[UIImage imageNamed:@"favoritedItem.png"] forState:UIControlStateSelected | UIControlStateHighlighted];
 }


districtLabel.text = [[detailsDataSource objectAtIndex: detailIndex] valueForKey:@"District"];

detailsDataSource array is used like this in detailViewController so I cannot change from array to dictionary, must make a mutable copy in that case.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:44:53+00:00Added an answer on June 9, 2026 at 1:44 pm

    setValue:forKey is not for modifying mutable dictionaries. You should use setObject:forKey. I.e., this:

    [[detailsDataSource objectAtIndex: detailIndex] setValue:@"Yes" forKey:@"Done"];
    

    Should be:

    [[detailsDataSource objectAtIndex: detailIndex] setObject:@"Yes" forKey:@"Done"];
    

    If that doesn’t fix the problem, make sure the dictionary and array are both mutable:

    -(IBAction)favoriteButtonPressed:(id)sender {
    
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Object.plist"];
    
        NSMutableDictionary *mutDict = [NSMutableDictionary dictionaryWithDictionary:[detailsDataSource objectAtIndex:detailIndex]];
        [mutDict setObject:@"Yes" forKey:@"Done"];
    
        NSMutableArray *tmpMutArr = [NSMutableArray arrayWithArray:detailsDataSource];
        [tmpMutArr replaceObjectAtIndex:detailIndex withObject:[NSDictionary dictionaryWithDictionary:mutDict]];  // make dict immutable before replacing.  Casting it ( (NSDictionary *)mutDict ) works too.
    
        [detailsDataSource release], detailsDataSource = nil;
        detailsDataSource = [[NSArray alloc] initWithArray:tmpMutArr];
    
        [detailsDataSource writeToFile:path atomically:YES];
    }
    

    Also, instead of @"Yes", you can use [NSNumber numberWithBool:YES]:
    [mutDict setObject:[NSNumber numberWithBool:YES] forKey:@"Done"];
    would generally be better form then using @"Yes".

    And:

    districtLabel.text = [[detailsDataSource objectAtIndex: detailIndex] valueForKey:@"Yes"];
    

    Should be:

    districtLabel.text = [[detailsDataSource objectAtIndex: detailIndex] objectForKey:@"Yes"];
    

    Also noticed that you may have a memory leak with:

    detailViewController.detailsDataSource = [[NSArray alloc] initWithArray:objectsArray];
    

    Make sure to autorelease when using property setters that retain.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I've got a string that has curly quotes in it. I'd like to replace
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.