gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:54:38 25 4
gpt4 key购买 nike

如何使用数据库中的键将对象添加到 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com