PDA

View Full Version : Error: incompatible types in assignment (OMG)




Tiwaz
Nov 19, 2009, 12:12 PM
So I'm trying to set up data persistence by writing values to a mutable array (NSStrings and floats, all input as NSString). The problem I'm getting is when I try to load them. Loading an NSString into another NSString works fine, but loading an NSString into a float gives me an error. Here's my code:

Saving:

[array addObject: name]; //name is an NSString
[array addObject: [NSString stringWithFormat:@"%f", stat]]; //stat is a float


Loading:

name = [array objectAtIndex:1];
stat = [[array objectAtIndex:3] floatValue]; //ERROR


Help? Everywhere I look tells me to use floatValue...

Thanks,
-T



kainjow
Nov 19, 2009, 12:29 PM
How is stat declared?

dejo
Nov 19, 2009, 12:30 PM
First off,
[NSString stringWithFormat:@"%f", stat]
removes any precision of the float when converted to string. So, if stat contained 1.25, the string will be @"1".

Tiwaz
Nov 19, 2009, 12:43 PM
kainjow:
@interface{
float *stat;
}

@property (nonatomic) float *stat;
..in the .h file


dejo:
Do you have any recommendations for not losing the floatyness?

Thank you both!
-T

kainjow
Nov 19, 2009, 12:48 PM
You're declaring it as a pointer. Don't:
float stat;

Tiwaz
Nov 19, 2009, 12:51 PM
When I do that I get a bunch of other errors from other methods and it doesn't actually fix the original error, could you perhaps explain the reasoning behind your suggestion? Thanks again!

-T

dejo
Nov 19, 2009, 12:52 PM
Also, since it's a primitive (i.e. a float), don't make the property nonatomic, make it assign.

kainjow
Nov 19, 2009, 12:54 PM
Did you update the @property too, to not be a pointer?

Obj-C objects are always used as pointers. However, floats and other primitives (int, char, etc) normally aren't unless you specifically need to deal with them as pointers (usually in functions that modify their values).

If you're still having errors post the first error and the code for it.

dejo
Nov 19, 2009, 01:00 PM
EDIT: Sorry, brain fart. @"%f" is fine!

But perhaps you might consider storing the float in an NSNumber instead of NSString.

Tiwaz
Nov 19, 2009, 02:35 PM
dejo: I'm not at my work computer right now, but I will definitely switch *nonatomic* to *assign*. I have no idea what those do yet, but I will look them up soon. NSNumber sounds like a much better way to go... I'll try that out soon!

kainjow: Yeah, I did update the @property as well. I'm actually working with a team of other students, so I'll have to see how removing the pointer aspect will affect future coding, but assuming you're right in that they're generally not pointers it shouldn't be a problem.

I'll try to get an updated error message up here soon. I really appreciate the help!

-T

bredell
Nov 19, 2009, 03:38 PM
I'm a bit confused about your indices. When you add the objects to the array you add name first and then stat. But when you access the objects from the array you use index 1 and 3. If these are the only two objects in the array you should use index 0 and 1. But maybe you're not showing all additions to the array.

Anyway, the exact runtime error message should give you a clue as to what is wrong.

Tiwaz
Nov 23, 2009, 08:12 PM
Alright, here's what I have now:

Error message: incompatible types in assignment

[array addObject: [NSNumber numberWithFloat:*stat]];
//saves the float as an NSNumber

stat = [[array objectAtIndex:2] floatValue]; //Try to load
//Error here^
//This is the same as before
//PS: I'm not showing all the entries to the save log, there are many. This is just an example of one that does not work, hence index of 2...

When I change my variable to be a non-pointer here's the errors I get:
[array addObject: [NSNumber numberWithFloat:*stat]];
//Error message here: invalid type argument of "unary *"


stat = [[array objectAtIndex:2] floatValue];
//No errors here, but errors do crop up in other methods using the variable...

Any more suggestions? I'm really stumped. By the way, I changed the @property to @property (assign) float *stat; What does the "assign" do. I do not understand the reason for that line of code. I do know that I need it, however!

Thanks,
-T

dejo
Nov 23, 2009, 11:31 PM
[array addObject: [NSNumber numberWithFloat:*stat]];
//saves the float as an NSNumber
...
By the way, I changed the @property to @property (assign) float *stat;
Why are you using "*stat" all over the place? The "*" is not even necessary under these circumstances even if stat was an object, which it's not.

Anyways, I feel like I'm starting to sound like a broken record with the number of times I've suggested this but: Time to step back from the real coding and go (re)learn the basics of Objective-C and OOP before you continue.