1

How to add object to NSArray with key from database?

i am using FMDatabase class to open database, its work ok, my problem is how to add object with key to NSArray, see this code

@implementation ViewController  {

    NSArray *allDevices;
    NSArray *searchResult;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    FMDatabase *db = [FMDatabase databaseWithPath:[[[NSBundle mainBundle]
                                    resourcePath] stringByAppendingPathComponent:@"db.sqlite"]];
    [db open];

    FMResultSet *results = [db executeQueryWithFormat:@"select * from allDevices"];

    NSString *name; NSString *company;

    while ([results next]) {

        name = [results stringForColumn:@"name"];
        company = [results stringForColumn:@"company"];

        allDevices = @[
                       @{@"name": name, @"company": company}
                       ];
    }


    [db close];



}

tableView..

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        return [searchResult count];

    } else {
        return [allDevices count];

    }

   }

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }



    NSDictionary *device;

    if ([self.searchDisplayController isActive]) {
        device = searchResult[indexPath.row];

    } else {
        device = allDevices[indexPath.row];
    }

    NSString *name = device[@"name"];
    NSString *company = device[@"company"];
    cell.textLabel.text = [NSString stringWithFormat:@"%@ - %@", name, company];


    return cell;
}

search code

#pragma mark - UISearchDisplayController delegate methods

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{

    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[cd] %@", searchText];

    searchResult = [allDevices filteredArrayUsingPredicate:resultPredicate];

}



-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}

its working well when i add objects to array manually, like this

   allDevices = @[
                   @{@"name": @"iPhone", @"company": @"Apple"},
                   @{@"name": @"Galaxy", @"company": @"Samsung"},
                   @{@"name": @"iPad", @"company": @"Apple"},
                   ];
5
  • NSDictionary is meant for Key-Value coding in iOS, not the NSArray. Commented Nov 23, 2013 at 5:47
  • You can create NSDictionary with name and company key value pair, and then add dictionary to array. Commented Nov 23, 2013 at 5:50
  • i am using this array in tableview and searching in it with filteredArrayUsingPredicate and its not working with NSDictionary Commented Nov 23, 2013 at 5:54
  • can you share searching code snippet. Commented Nov 23, 2013 at 5:59
  • @Bhumeshwerkatre i shared the code, do you seen it Commented Nov 24, 2013 at 6:00

2 Answers 2

4

i found it,

thanks

 [allDevices addObjectsFromArray:[NSArray arrayWithObjects:[NSMutableDictionary dictionaryWithObjects:
                                    [NSArray arrayWithObjects: rowID, name, company, nil]
                            forKeys:[NSArray arrayWithObjects: @"rowID", @"name", @"company", nil]], nil]];
Sign up to request clarification or add additional context in comments.

Comments

1

Have a look at NSMutableDictionary.

You can do something like this:

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject: obj forKey: @"yourKey"];

where obj is the object you want to store.

Also see this post about conversion from NSDictionary to NSArray.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.