gpt4 book ai didi

ios - NSMutableArray 即使在将数组插入其中后也是空的

转载 作者:行者123 更新时间:2023-11-29 03:22:05 25 4
gpt4 key购买 nike

希望你能帮帮我,我的问题是:

  • 我有一个空的 NSMutableArray。
  • 我的方法之一是获取 SQLite 数据库(使用 FMDB 框架)并将“name”列中的所有值推送到 NSMutable 数组中。
  • 但是当我显示 NSMutableArray(或这个数组的任何值)时,它被定义为 null

大多数类似的问题是由于数组可用的“区域”(不是全局的而是本地的)但在这种情况下不是(我不这么认为)。

我没有收到来自 xCode 的警告和错误。

这是我的部分代码:

.h文件 #导入

@interface MasterViewController : UITableViewController

@property (nonatomic, assign) int nbUsers;
@property (nonatomic, assign) NSMutableArray *nameOfUsers;

@end

viewDidLoad 方法

- (void)viewDidLoad
{
[super viewDidLoad];

// Number of users
int tempNb = 0;
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [docPaths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"UserDatabase.sqlite"];

FMDatabase *database = [FMDatabase databaseWithPath:dbPath];

[...]

// Name of users
[database open];
int i = 0;
FMResultSet *results2 = [database executeQuery:@"SELECT * FROM userList"];
while ([results2 next]) {
[_nameOfUsers insertObject:[results2 stringForColumn:@"name"] atIndex:i];
NSLog(@"%@", _nameOfUsers[i]);
i++;
//[_nameOfUsers addObject:(NSString *)[results2 stringForColumn:@"name"]];
}
[database close];

self.navigationItem.leftBarButtonItem = self.editButtonItem;

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
}

我想做的是通过以下方式将这些名称显示到主视图中的单元格中:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myBasicCell"];
cell.textLabel.text = _nameOfUsers[indexPath.row];
NSLog(@"%D and the name is %@", indexPath.row, _nameOfUsers[indexPath.row]);
return cell;
}

但问题不在这里,因为通过使用 NSLog,我发现我的 SQLite DB 填充得很好,获取的值也被验证了,它是 NSMutableArray 没有正确填充(可能是我用来填充它的方法不好,但我遵循了许多描述相同方法的类似问题)。

提前感谢您的宝贵时间,

此致

最佳答案

对于 ARC 项目: @interface MasterViewController:UITableViewController

@property (nonatomic, readWrite) int nbUsers;
@property (nonatomic, strong) NSMutableArray *nameOfUsers;

@end

非 ARC:

@interface MasterViewController : UITableViewController

@property (nonatomic, readWrite) int nbUsers;
@property (nonatomic, retain) NSMutableArray *nameOfUsers;

@end

在viewDidLoad中:

- (void)viewDidLoad
{
[super viewDidLoad];
_nameOfUsers = [[NSMutableArray alloc] init];
// Number of users
int tempNb = 0;
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [docPaths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"UserDatabase.sqlite"];

FMDatabase *database = [FMDatabase databaseWithPath:dbPath];

[...]

// Name of users
[database open];
int i = 0;
FMResultSet *results2 = [database executeQuery:@"SELECT * FROM userList"];
while ([results2 next]) {
[_nameOfUsers insertObject:[results2 stringForColumn:@"name"] atIndex:i];
NSLog(@"%@", _nameOfUsers[i]);
i++;
//[_nameOfUsers addObject:(NSString *)[results2 stringForColumn:@"name"]];
}
[database close];

self.navigationItem.leftBarButtonItem = self.editButtonItem;

UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
}

关于ios - NSMutableArray 即使在将数组插入其中后也是空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20941346/

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