ios - 如何使用数据库中的键将对象添加到 NSArray?

标签 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"},
                   ];

最佳答案

我找到了,

谢谢

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

关于ios - 如何使用数据库中的键将对象添加到 NSArray?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20159048/

相关文章:

objective-c - 访问另一个 NSMutableArray 中对象的 NSMutableArray(即嵌套对象访问)

ios - Spika-iOS : "incompatible block pointer types"

iphone - NSDictionary到包含键和值的NSArray

Apple 的 C# MDM 服务器,无法发送推送通知

ios - Swift:如何使viewWillTransition仅影响横向的iPhone

ios - 更改 rightCalloutAccessoryView 的颜色

ios - NSMutableArray 上的@synchronized 是否也对其元素产生同步效果?

objective-c - 对 Objective-C 的指针内存管理的误解

android - 如何搜索 HTML 中的内容而不是标签

ios - UINavigationController – 覆盖后退按钮操作