Objc,需要创建一个对象来保存字符串/数字,然后用计算更新?

时间:2011-01-19 12:57:13

标签: objective-c xcode nsarray nsdictionary

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

然后我需要将所有项目的总价格相加并更改对象中的值,使它们小于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 ++;
    }
}

建议?

1 个答案:

答案 0 :(得分:0)

您需要使用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 Class Refernce