将值附加到我的 NSMutableArray
好吧,由于某种原因我遇到了一个愚蠢的错误。
-(void) getAllRowsFromTableNamed: (NSString *) tableName {
NSString *qsql = @"SELECT supplier from products";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2( db, [qsql UTF8String], -1, &statement, nil) ==
SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
char *field1 = (char *) sqlite3_column_text(statement,0);
NSMutableString *field1Str = [[NSMutableString alloc] initWithUTF8String: field1];
listofsuppliers = [NSMutableArray arrayWithObjects:field1Str, nil];
NSLog(@"list of suppliers %@",listofsuppliers);
[field1Str release];
}
sqlite3_finalize(statement);
}
}
在我的日志中,我得到以下内容:
QueensWayInventory[13192:f803] list of suppliers ("(null)")
QueensWayInventory[13192:f803] list of suppliers ("(null)")
QueensWayInventory[13192:f803] list of suppliers (Dell)
QueensWayInventory[13192:f803] list of suppliers (Apple)
QueensWayInventory[13192:f803] list of suppliers (Apple)
QueensWayInventory[13192:f803] list of suppliers (Dell)
我想要这个:
QueensWayInventory[13192:f803] list of suppliers (
Dell,
Apple
)
这一定是一些非常简单的事情,我做错了。当我创建静态列表时,我会按照我想要的方式得到它,但当我尝试从数据库创建动态读取时却不会。
OK, for some reason I'm getting a stupid error.
-(void) getAllRowsFromTableNamed: (NSString *) tableName {
NSString *qsql = @"SELECT supplier from products";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2( db, [qsql UTF8String], -1, &statement, nil) ==
SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
char *field1 = (char *) sqlite3_column_text(statement,0);
NSMutableString *field1Str = [[NSMutableString alloc] initWithUTF8String: field1];
listofsuppliers = [NSMutableArray arrayWithObjects:field1Str, nil];
NSLog(@"list of suppliers %@",listofsuppliers);
[field1Str release];
}
sqlite3_finalize(statement);
}
}
In my log I'm getting the following:
QueensWayInventory[13192:f803] list of suppliers ("(null)")
QueensWayInventory[13192:f803] list of suppliers ("(null)")
QueensWayInventory[13192:f803] list of suppliers (Dell)
QueensWayInventory[13192:f803] list of suppliers (Apple)
QueensWayInventory[13192:f803] list of suppliers (Apple)
QueensWayInventory[13192:f803] list of suppliers (Dell)
I want this instead:
QueensWayInventory[13192:f803] list of suppliers (
Dell,
Apple
)
It must be something very simple I'm doing wrong. When I create a static list I get it how I want it to be but not when I try to create a dynamic reading from a database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这一行:
是你的问题。
它丢弃旧数组并创建一个包含单个元素的新数组。
相反,在您想要的循环之前:
我使用了 Set 而不是 Array,因为您只想包含每个元素的单个副本。
然后在你的循环中:
另请注意,这里没有真正需要创建可变字符串。
This line:
is your problem.
It throws away the old array and creates a new one with a single element.
Instead, before your loop you want:
I've used a Set instead of an Array since you want to include only a single copy of each element.
Then inside your loop:
Also note that there's no real need to create a mutable string here.
您需要在 while 循环外部创建您的
NSMutableArray
。 while 循环的每次迭代都会重新创建它,当您将其打印到控制台时,它始终只有一个值。使用-appendObject:
在循环内添加每个后续字符串值。虽然使用 field1 utf8 字符串创建一个
NSMutableString
然后将其添加到数组中并没有什么重要的,但通常最好的做法是创建一个不可变的NSString
。如果您打算操作该字符串的内容,请使用 NSMutableString。You need to create your
NSMutableArray
outside the while loop. You are recreating it every iteration of the while loop, and when you print it to the console, it will always only have one value. Use-appendObject:
to add each subsequent string value within the loop.While there is nothing critical about creating a
NSMutableString
with the field1 utf8 string to then add to your array, usually it is better practice to create a non mutableNSString
. Use aNSMutableString
if you plan to manipulate the contents of that string.