Error: incompatible types in assignment (OMG)

Discussion in 'iOS Programming' started by Tiwaz, Nov 19, 2009.

  1. macrumors newbie

    Joined:
    Nov 19, 2009
    #1
    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:
    Code:
    [array addObject: name]; //name is an NSString
    [array addObject: [NSString stringWithFormat:@"%f", stat]]; //stat is a float
    
    Loading:
    Code:
    name = [array objectAtIndex:1];
    stat = [[array objectAtIndex:3] floatValue]; //ERROR
    
    Help? Everywhere I look tells me to use floatValue...

    Thanks,
    -T
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
  3. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    First off,
    Code:
    [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".
     
  4. thread starter macrumors newbie

    Joined:
    Nov 19, 2009
    #4
    kainjow:
    Code:
    @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
     
  5. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #5
    You're declaring it as a pointer. Don't:
    Code:
    float stat;
     
  6. thread starter macrumors newbie

    Joined:
    Nov 19, 2009
    #6
    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
     
  7. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    Also, since it's a primitive (i.e. a float), don't make the property nonatomic, make it assign.
     
  8. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #8
    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.
     
  9. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    EDIT: Sorry, brain fart. @"%f" is fine!

    But perhaps you might consider storing the float in an NSNumber instead of NSString.
     
  10. thread starter macrumors newbie

    Joined:
    Nov 19, 2009
    #10
    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
     
  11. macrumors regular

    Joined:
    Mar 30, 2008
    Location:
    Uppsala, Sweden
    #11
    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.
     
  12. thread starter macrumors newbie

    Joined:
    Nov 19, 2009
    #12
    Alright, here's what I have now:

    Error message: incompatible types in assignment

    Code:
    [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:
    Code:
    [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
    Code:
    @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
     
  13. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #13
    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.
     

Share This Page