【问题标题】:How can I store a float value in an NSArray?如何将浮点值存储在 NSArray 中?
【发布时间】:2010-04-06 14:04:53
【问题描述】:
for ( tempI = 0; tempI < 10; tempI++ )  
{
    tempJ = 1;
    NSArray *objectsForArray = [NSArray arrayWithObjects:@"array[tempI][tempJ]", @"array[tempI][tempJ+1]",  @"array[tempI][tempJ+2]", nil];
}

我可以像上面那样写代码吗?我需要在NSArray 中存储一个浮点值(array[][])。我可以吗?
我的问题是,我有一个矩阵作为

1.0   0.4   0.3   0.5  
2.0   0.4   0.5   0.5  
3.0   4.3   4.9   0.5  

我需要使用 1.0, 2.0 3.0 检索值 (0.4, 0.3, 0.5)。我该如何使用 NSDictionary?
谢谢你

for( tempI = 0; tempI < 5; tempI++)
{
        NSDictionary *dictionary[tempI] = [ [NSDictionary alloc] initWithObjects:@"array[tempI][tempI + 1]", @"array[tempI][tempI + 2]", @"array[tempI][tempI + 3]", @"array[tempI][tempI + 4]", @"array[tempI][tempI + 5]", nil];
}

我可以这样写吗? Objective C 接受它吗?

我收到一个错误

error: variable-sized object may not be initialized

【问题讨论】:

  • 请问您为什么要将3D矩阵存储到NSArray中?

标签: objective-c cocoa-touch cocos2d-iphone


【解决方案1】:

NSArray 只能存储对象,不能存储基元,幸运的是,NSNumber 类有一个方便的方法,它接受一个浮点基元并返回一个浮点对象:

+ (NSNumber *)numberWithFloat:(float)value

因此你可以像这样填充你的数组:

float exampleFloat = 5.4;

NSArray *anArrayOfFloatObjects = [NSArray arrayWithObjects:
                                    [NSNumber numberWithFloat:10.0],
                                    [NSNumber numberWithFloat:2],
                                    [NSNumber numberWithFloat:4],
                                    [NSNumber numberWithFloat:exampleFloat],
                                    nil]; // Don't forget the nil to signal
                                          // end of the array

至于你的具体问题,你可以写:

NSMutableArray *tmpArray; // this is necessary since an NSArray can only be initialized 
                          // once and we will need to have all the objects that will be 
                          // added to the array available to us at once.

tmpArray = [NSMutableArray arrayWithCapacity:12]; // returns an autoreleased empty array


for (int col=0; col<=3; col++) {
    for (int row=0; row<=2; row++) {
        [tmpArray addObject:[NSNumber numberWithFloat:array[col][row]]];
    }
}
NSArray *myArray = [NSArray arrayWithArray:tmpArray];

就使用字典来检索矩阵值而言,我能想到的唯一方法是对矩阵值进行键编码:

A1 A2 A3 A4

B1 B2 B3 B4

C1 C2 C3 C4

D1 D2 D3 D4

例如:

 NSMutableDictionary *myDictionary;

[myDictionary setObject:[NSNumber numberWithFloat:5.0] forKey:@"A1"];

...

NSNumber *myFloat = [myDictionary objectForKey:@"A1"];

此外,重要的是在这里指出,每当以 @"something here" 格式编写某些内容时,它实际上就是一个 NSString 对象。所以当你写的时候:

NSArray *objectsForArray = [NSArray arrayWithObjects:
                                             @"array[tempI][tempJ]",
                                             @"array[tempI][tempJ+1]",                                                                                                                                            
                                             @"array[tempI][tempJ+2]",
                                             nil];

这和写完全一样:

NSString *newString = @"Roses are red";    // example strings
NSString *newString1 = @"Violets are blue";
NSString *newString2 = @"array[tempI][tempJ+1]";
NSString *newString3 = @"These are all string objects";

NSArray *objectsForArray = [NSArray arrayWithObjects:
                                             @"array[tempI][tempJ]",
                                             newString2,                                                                                                                                          
                                             @"array[tempI][tempJ+2]",
                                             nil];

【讨论】:

  • 非常感谢您对 NSArray 和 NSDictionary 的说明。这对我很有帮助。
【解决方案2】:

试试:

for ( int tempI = 0; tempI < 10; tempI++ )  
{
    int tempJ = 1;
    fl_tempI = float(tempI);
    NSArray *objectsForArray = [NSArray arrayWithObjects:@"array[fl_tempI][tempJ]", @"array[fl_tempI][tempJ+1]",  @"array[fl_tempI][tempJ+2]", nil];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    • 2012-01-14
    • 1970-01-01
    相关资源
    最近更新 更多