如何将浮点值存储在 NSArray 中? [英] How can I store a float value in an NSArray?

查看:14
本文介绍了如何将浮点值存储在 NSArray 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

for ( tempI = 0; tempI <10; tempI++ ){温度J = 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.52.0 0.4 0.5 0.53.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 接受它吗?

我收到一个错误

<块引用>

错误:可变大小的对象可能没有被初始化

解决方案

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 来表示信号//数组结束

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

NSMutableArray *tmpArray;//这是必要的,因为 NSArray 只能被初始化//一次,我们需要拥有所有的对象//立即添加到我们可用的数组中.tmpArray = [NSMutableArray arrayWithCapacity:12];//返回一个自动释放的空数组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:@"数组[tempI][tempJ]",@"数组[tempI][tempJ+1]",@"数组[tempI][tempJ+2]",零];

这和写完全一样:

NSString *newString = @"玫瑰是红色的";//示例字符串NSString *newString1 = @"紫罗兰色是蓝色的";NSString *newString2 = @"array[tempI][tempJ+1]";NSString *newString3 = @"这些都是字符串对象";NSArray *objectsForArray = [NSArray arrayWithObjects:@"数组[tempI][tempJ]",新字符串2,@"数组[tempI][tempJ+2]",零];

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

Can I write the code as above. I need to store an float value (array[][]) in NSArray. Can I do it ?
My problem is, I have a matrix as

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

I need to retrieve values (0.4, 0.3, 0.5) using 1.0, 2.0 3.0 . How can I use NSDictionary for this?
Thank You

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];
}

Can I write in this way ? Does Objective C accepts it ?

I am getting an error as

error: variable-sized object may not be initialized

解决方案

NSArray can only store objects, not primitives, fortunatly, the NSNumber class has a convenience method that takes a float primitive and returns a float object as such:

+ (NSNumber *)numberWithFloat:(float)value

therefore you could populate your array like this:

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

As for your specific issue, you could write:

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];

as far as using a dictionary to retrive matrix values, the only way I can think off would be to key code your matrix values as such:

A1 A2 A3 A4

B1 B2 B3 B4

C1 C2 C3 C4

D1 D2 D3 D4

for example:

 NSMutableDictionary *myDictionary;

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

...

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

Also, it is important to point here that whenever something is written under the format @"something here", it literally is an NSString object. so when you write:

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

this is exactly the same as writting:

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 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆