开发者

can I store an id value in array of float type?

I used, for(id value in values) to get the value from an NSArray. Now I want to store it in 2 dimensional float array[][]. When I try to assign the values to array it is giving error:incompatible types in assignment. I tried to cast the value but I got error: pointer value used where a floating point value was expected. I need to store the values in an 2 dimensional array . How can I make it ? Thank You.

@implementation fromFileRead1
NSString *fileNameString;
int numberOfEnemies, numberOfValues;
-(id)init
{
    if( (self = [super init]) ) 
    {
        NSString *path = @"/Users/sridhar/Desktop/Projects/exampleOnFile2/enemyDetals.txt";
        NSString *contentsOfFile = [[NSString alloc] initWithContentsOfFile:path];
        NSArray *lines = [contentsOfFile componentsSeparatedByString:@"\n"]; 
        numberOfEnemies = [lines count];
        NSLog(@"The number of Lines: %d", numberOfEnemies);
        for (id line in lines)
        {
            NSLog(@"Line %@", line );
            NSString *string1 = line;
            NSArray *split1 = [string1 componentsSeparatedByString:@","];
            numberOfValues = [split1 count];
            NSLog(@"The number of values in Row: %d", numberOfValues);
            for (id value in split1)
            {
                NSLo开发者_JAVA技巧g(@"value %@", value);
                float value1;
                value1 = [split1 objectAtIndex:2]);
                NSLog(@"VAlue of Value1 at index 2: %f", value1 );
            }
        }
    }
    return self;
}
@end

In enemyDetal.txt I have

1,3,3
2,3,2.8
10,2,1.6


Storing an object (e.g. id) in a float array is most certainly not what you want, and will give you the weirdest results.

The question is what you really want to do. If you have NSNumber objects in your array containing float values, then you can use [value floatValue] to convert your object to a float primitive.

If your intention was really to store a pointer as a float try (float)((int)value)). This might work but be warned that this you will most likely not be able to retrieve the pointer again.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜