如何使用数据库中的密钥向NSArray添加对象?

时间:2013-11-23 05:46:25

标签: ios nsarray

如何使用数据库中的密钥向NSArray添加对象?

我正在使用FMDatabase类打开数据库,其工作正常,我的问题是如何用NSArray添加带有对象的对象,请参阅此代码

@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;
}

搜索代码

#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;
}

当我手动向对象添加对象时,它的工作正常,如此

   allDevices = @[
                   @{@"name": @"iPhone", @"company": @"Apple"},
                   @{@"name": @"Galaxy", @"company": @"Samsung"},
                   @{@"name": @"iPad", @"company": @"Apple"},
                   ];

2 个答案:

答案 0 :(得分:4)

我找到了,

感谢

 [allDevices addObjectsFromArray:[NSArray arrayWithObjects:[NSMutableDictionary dictionaryWithObjects:
                                    [NSArray arrayWithObjects: rowID, name, company, nil]
                            forKeys:[NSArray arrayWithObjects: @"rowID", @"name", @"company", nil]], nil]];

答案 1 :(得分:1)

查看NSMutableDictionary

您可以这样做:

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

其中obj是您要存储的对象。

另见this post关于从NSDictionary到NSArray的转换。