views:

131

answers:

1

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)
            {
                NSLog(@"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
+2  A: 

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.

frenetisch applaudierend
I think I did not explain my problem well. I tried the solution you have given, it is not giving any errors but result is some big floating value like 2322222.000000. I have edited my program. Thank You
srikanth rongali
Ah, so your values are NSStrings. To get a primitive value (like a float) you must first convert them. NSString provides the method floatValue so you can do something like this in your loop: `float value1 = [[split1 objectAtIndex:2] floatValue];`. It's important to note is that Objective-C does not autobox/unbox privmitives like Java does.
frenetisch applaudierend
Hi,Thank you it helped me.
srikanth rongali
No problem, always glad to help :-)
frenetisch applaudierend