Objective c Obj-C,需要创建一个项目来保存字符串/数字,然后用计算更新吗?

Objective c Obj-C,需要创建一个项目来保存字符串/数字,然后用计算更新吗?,objective-c,xcode,nsarray,nsdictionary,Objective C,Xcode,Nsarray,Nsdictionary,我需要创建一个数组或字典来保存字符串和值,我正在生成一个饼图。我需要遍历数据库查询中的行,并向对象添加字符串和价格 然后,我需要将所有项目的总价相加,并更改对象中的值,使其小于1 我解释得不是很好 这就是我正在用动态数据替换的内容 BNPieChart* chart = [[BNPieChart alloc] initWithFrame:myImageRect]; [chart addSlicePortion:0.1 withName:@"Orange 10%"]; [chart addSlic

我需要创建一个数组或字典来保存字符串和值,我正在生成一个饼图。我需要遍历数据库查询中的行,并向对象添加字符串和价格

然后,我需要将所有项目的总价相加,并更改对象中的值,使其小于1

我解释得不是很好

这就是我正在用动态数据替换的内容

BNPieChart* chart = [[BNPieChart alloc] initWithFrame:myImageRect];
[chart addSlicePortion:0.1 withName:@"Orange 10%"];
[chart addSlicePortion:0.2 withName:@"Fandango 10%"];
[chart addSlicePortion:0.1 withName:@"Blue 10%"];
[chart addSlicePortion:0.1 withName:@"Cerulean 10%"];
[chart addSlicePortion:0.3 withName:@"Green 10%"];
[chart addSlicePortion:0.1 withName:@"Yellow 10%"];
[chart addSlicePortion:0.1 withName:@"Pink 10%"];
请注意,添加切片值时,它们的总数为1

我试过使用字典,但价格似乎不适合作为一个值或键

NSMutableDictionary *dictData = [[[NSMutableDictionary alloc] init] autorelease];

if(error_code == SQLITE_OK) {

    while(sqlite3_step(statementTMP) == SQLITE_ROW)
    {
        // HERE you need to put the data into an array so you can 
        // perform calculations on it after the array is filled

        // get price and category string

        [dictData setObject:accumPrice forKey:category];
        i ++;
    }
}

建议?

您需要使用
NSNumber
在字典中存储int、float、double等

NSMutableDictionary *dictData = [[[NSMutableDictionary alloc] init] autorelease];

if(error_code == SQLITE_OK) {

    while(sqlite3_step(statementTMP) == SQLITE_ROW)
    {
        // HERE you need to put the data into an array so you can 
        // perform calculations on it after the array is filled

        // get price and category string

        [dictData setObject:[NSNumber numberWithFloat:accumPrice] forKey:category];
        i ++;
    }
}

您需要使用
NSNumber
在字典中存储int、float、double等

NSMutableDictionary *dictData = [[[NSMutableDictionary alloc] init] autorelease];

if(error_code == SQLITE_OK) {

    while(sqlite3_step(statementTMP) == SQLITE_ROW)
    {
        // HERE you need to put the data into an array so you can 
        // perform calculations on it after the array is filled

        // get price and category string

        [dictData setObject:[NSNumber numberWithFloat:accumPrice] forKey:category];
        i ++;
    }
}

价格是基本价格(即浮动还是双精度)?如果是,请使用NSNumber。集合(NSDictionary等)需要一个对象,不能使用标量等。price是原语(即float还是double)?如果是,请使用NSNumber。集合(NSDictionary等)需要一个对象,不能使用标量等。