Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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).</p> <p>Then join the components of the filtered array into a string.</p> <p>Solution 1:</p> <pre><code>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]; </code></pre> <p>Solution 2:</p> <pre><code>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:@", "]; </code></pre> <hr> <p>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 <code>BOOL selected[7]</code> and nothing more.</p> <p>Then to have the name of the weekdays you should instead use the methods of <code>NSCalendar</code>/<code>NSDateFormatter</code>/<code>NSDateComponents</code> to get the standard names of the weekdays (automatically in the right language/locale of the user): <em>create an <code>NSDate</code> using a <code>NSDateComponent</code> for which you simply define the weekday component, then use an <code>NSDateFormatter</code> to convert this to a string, choosing a string format that only display the weekday name.</em></p> <hr> <pre><code>-(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 } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload