将值附加到我的 NSMutableArray

发布于 2024-12-18 10:22:19 字数 1329 浏览 1 评论 0原文

好吧,由于某种原因我遇到了一个愚蠢的错误。

-(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那一片橙海, 2024-12-25 10:22:19

这一行:

listofsuppliers = [NSMutableArray arrayWithObjects:field1Str, nil]; 

是你的问题。

它丢弃旧数组并创建一个包含单个元素的新数组。

相反,在您想要的循环之前:

listofsuppliers = [[NSMutableSet alloc] init];

我使用了 Set 而不是 Array,因为您只想包含每个元素的单个副本。

然后在你的循环中:

[listofsuppliers addObject:field1Str];

另请注意,这里没有真正需要创建可变字符串。

This line:

listofsuppliers = [NSMutableArray arrayWithObjects:field1Str, nil]; 

is your problem.

It throws away the old array and creates a new one with a single element.

Instead, before your loop you want:

listofsuppliers = [[NSMutableSet alloc] init];

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:

[listofsuppliers addObject:field1Str];

Also note that there's no real need to create a mutable string here.

北座城市 2024-12-25 10:22:19
  1. 您需要在 while 循环外部创建您的 NSMutableArray。 while 循环的每次迭代都会重新创建它,当您将其打印到控制台时,它始终只有一个值。使用 -appendObject: 在循环内添加每个后续字符串值。

  2. 虽然使用 field1 utf8 字符串创建一个 NSMutableString 然后将其添加到数组中并没有什么重要的,但通常最好的做法是创建一个不可变的 NSString。如果您打算操作该字符串的内容,请使用 NSMutableString。

  1. 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.

  2. 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 mutable NSString. Use a NSMutableString if you plan to manipulate the contents of that string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文