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 6620097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:03:21+00:00 2026-05-25T21:03:21+00:00

I have a Mutable Dictionary which allows people to choose a selection of days

  • 0

I have a Mutable Dictionary which allows people to choose a selection of days of the week. Once a day has been selected the state is updated using numberWithBool.

When I NSLog the output it looks something like this:

    {
    day = Monday;
    isSelected = 1;
},
    {
    day = Tuesday;
    isSelected = 0;
},
    {
    day = Wednesday;
    isSelected = 0;
},
    {
    day = Thursday;
    isSelected = 0;
},
    {
    day = Friday;
    isSelected = 0;
},
    {
    day = Saturday;
    isSelected = 0;
},
    {
    day = Sunday;
    isSelected = 1;
}

I would like to be able to extract the chosen days and produce the output in the form of a string. So in this example the output would be: Monday, Sunday

How can I do this?

My code for creating the dictionary is below:

NSMutableArray * tempSource = [[NSMutableArray alloc] init];
NSArray *daysOfWeek = [NSArray arrayWithObjects:@"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday", @"Sunday",nil];
for (int i = 0; i < 7; i++)
{
    NSString *dayOfWeek = [daysOfWeek objectAtIndex:i];
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:dayOfWeek, @"day",   [NSNumber numberWithBool:NO], @"isSelected",nil];
    [tempSource addObject:dict];
}

[self setSourceArray:tempSource];
[tempSource release];
  • 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-05-25T21:03:21+00:00Added an answer on May 25, 2026 at 9:03 pm

    You can loop thru all of your items in the array and build a side-array only containing names of the day (1), or you can use a predicate and then KVC to extract the days directly (2).

    Then join the components of the filtered array into a string.

    Solution 1:

    NSMutableArray selectedDays = [[NSMutableArray alloc] init];
    for(NSDictionary* entry in sourceArray)
    {
        if (([entry objectForKey:@"isSelected"] boolValue]) {
            [selectedDays addObject:[entry objectForKey:@"day"]];
        }
    }
    NSString days = [selectedDays componentsJoinedByString:@", "];
    [selectedDays release];
    

    Solution 2:

    NSPredicate* filter = [NSPredicate predicateWithFormat:@"SELF.isSelected == 1"]; // not sure about the exact format (no mac here to test right now so you may adapt a bit if it does not work directly)
    // get the array of dictionaries but only the ones that have isSelected==1
    NSArray selectedEntries = [sourceArray filteredArrayUsingPredicate:filter]; 
    NSArray selectedDays = [selectedEntries valueForKey:@"day"]; // extract the "days" keys of the dictionaries. We have a NSArray of strings then.
    NSString days = [selectedDays componentsJoinedByString:@", "];
    

    As a side note, your way of doing this is quite strange. Why having an NSArray of NSDictionaries for this? As this is simple and a static-size array containing only BOOL, you may instead for this particular case simply use C array BOOL selected[7] and nothing more.

    Then to have the name of the weekdays you should instead use the methods of NSCalendar/NSDateFormatter/NSDateComponents to get the standard names of the weekdays (automatically in the right language/locale of the user): create an NSDate using a NSDateComponent for which you simply define the weekday component, then use an NSDateFormatter to convert this to a string, choosing a string format that only display the weekday name.


    -(void)tableView:(UITableView*)tv didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
      // selectedDays is an instance variable in .h declared as
      // BOOL selectedDays[7];
      selectedDays[indexPath.row] = ! selectedDays[indexPath.row];
      [tv reloadData];
    }
    
    -(UITableViewCell*)tableView:(UITableView*)tv cellForRowAtIndexPath:(NSIndexPath*)indexPath {
      static NSString* kCellIdentifier = @"DayCell";
      UITableViewCell* cell = [tv dequeueReusableCellWithIdentifier:kCellIdentifier];
      if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:... identifier:kCellIdentifier]; autorelease];
        // configure here every property that is common to all for your cells (text color, etc)
      }
    
      // configure here things that will change from cell to cell
      cell.accessoryType = selectedDays[indexPath.row] ? UITableViewCellAccessoryTypeCheckmarck : UITableViewCellAccessoryTypeNone;
      cell.textLabel.text = weekdayName(indexPath.row);
    
      return cell;
    }
    
    // Simple C function to return the name of a given weekday
    NSString* weekdayName(int weekday)
    {
    #if WAY_1
      /**** Solution 1 ****/
      // Optimization note: You may compute this once for all instead of recomputing it each time
      NSDateComponents* comp = [[[NSDateComponents alloc] init] autorelease];
      [comp setWeekday:weekday+1]; // weekdays start at 1 for NSDateComponents
      NSDate* d = [[NSCalendar currentCalendar] dateFromComponents:comp];
    
      NSDateFormatter* df = [[[NSDateFormatter alloc] init] autorelease];
      [df setDateFormat:@"EEEE"]; // format for the weekday
      return [[df stringFromDate:d] capitalizedString];
    #else
      /**** Solution 2 ****/
      NSDateFormatter* df = [[[NSDateFormatter alloc] init] autorelease];
      NSArray* weekdayNames = [df weekdaySymbols]; // or maybe one of its sibling methods? check what is returned here to be sure
      return [weekdayNames objectAtIndex:weekday];
    #endif
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a mutable array that has been retained. This array contain dictionaries with
I have a Mutable Dictionary that I removed an element from with removeObjectForKey. That's
I have a dictionary/list balance_sign_dict from which I need to retrieve 2 values based
I have a mutable array which stores sprites but the problem is that the
In my project I have two text fields which are bound to a mutable
I have a mutable HashMap and would like to use it like a default-dictionary.
If I have an NSArray (or mutable array) with several dictionary objects (each dictionary
I have mutable string array named arrayout. It is having 3 element .Now I
I have a mutable class that I'm using as a key to a generic
I have a mutable array that is retained and storing several objects. At some

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.