Passing id to parameter of incompatible type 'float'

Discussion in 'Mac Programming' started by johnmerlino, Nov 19, 2011.

  1. johnmerlino macrumors member

    Joined:
    Oct 22, 2011
    #1
    Hey all,

    xcode is complaining that I am passing an element with id type as a parameter to a getter/setter method which was declared as a float. Its true it was declared with float:

    Code:
    @interface Report : NSObject
    {
        @private
        NSNumber *speed;
        float latitude;
        float longitude;
    }
    @property (strong, nonatomic)  NSNumber *speed;
    @property float latitude;
    @property float longitude;
    
    //report.m
    @synthesize speed = _speed;
    @synthesize latitude;
    @synthesize longitude;
    
    But here's what throws me off. The return value of [rep objectForKey:mad:"latitude"] and [rep objectForKey:mad:"longitude"] are both floats:

    Code:
    for (NSDictionary *dict in resultsDictionary) {
                    
                    NSDictionary *rep = [dict objectForKey:@"report"];
                    
                    if(rep){
                        Report *report = [[Report alloc] init];
                        report.speed = [rep objectForKey:@"speed"];
                        report.latitude = [rep objectForKey:@"latitude"];
                        report.longitude = [rep objectForKey:@"longitude"];
    
    So since its passing a float to the latitude and longitude methods, why does it tell me that its passing an id?

    thanks for response
     
  2. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #2
    You can only put objects into a dictionary, no int, float, double etc. And that means you cannot get a float out of a dictionary, only objects. Very strange that you use one NSNumber* and two float.

    By the way, what is your reason to use float and not double? The rule is: Unless you have a very good reason that you would be able to explain, you should use double and not float.
     
  3. johnmerlino thread starter macrumors member

    Joined:
    Oct 22, 2011
    #3
    Can I just use an NSNumber rather than a float or double and get the same expected results (e.g. -82.1362888900)
     
  4. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #4
    If you want to put a number into a dictionary, you have to wrap it into an NSNumber, like [NSNumber numberWithDouble:82.1362888900]. So what you get out of the dictionary will be an NSNumber*. Now only you can know what you want to do with this number, but if you want to use it in a calculation, then you most likely want a double. So you take the number out of the dictionary, check that it is an NSNumber* object, and get [theNumber doubleValue]. I haven't checked any of the method names.
     
  5. Shawnpk macrumors 6502

    Joined:
    Jan 13, 2011
    Location:
    Los Angeles, CA
    #5
    Yes. You can wrap the float in an NSNumber like so: [NSNumber numberWithFloat 82.136288]. But I believe a float is only going to give you 6 places past the decimal and the round if there are more than 6 places. If you want a larger number, I think it's [NSNumber numberWithDouble: 82.1362888900].
     
  6. johnmerlino thread starter macrumors member

    Joined:
    Oct 22, 2011
    #6
    I inspected the return value that JSONKit was giving and i noticed that the value of the latitude and longitude were strings. So I did this:

    Code:
                        NSString *latitude = [rep objectForKey:@"latitude"];
                        report.latitude = [latitude doubleValue];
                        NSString *longitude = [rep objectForKey:@"longitude"];
                        report.longitude = [longitude doubleValue];     
    
    This almost gave me what I wnated, problem is it strips away the 0s at the end of the number stored in database. For example, it converted this 29.1572135400 to 29.15721354. And I need those zeros.
     
  7. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #7
    Then either store strings, or always compose on the extra zeroes. Example using the NSNumberFormatter class or a format string like @"%0.10f"
     
  8. johnmerlino, Nov 19, 2011
    Last edited: Nov 19, 2011

    johnmerlino thread starter macrumors member

    Joined:
    Oct 22, 2011
    #8
    even when I keep them as strings, it still strips the 0s out.
     
  9. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #9
    And you THINK that because...?

    Are you looking in the debugger, are you NSLog'ing it out? printf?

    ----------

    What? So you are composing a URL (NSString) from your stored float's?
     
  10. johnmerlino thread starter macrumors member

    Joined:
    Oct 22, 2011
    #10
    I noticed that and I fixed that, but even after running it again, it still strips the strings out. I have an NSLog:

    http://maps.google.com/maps?q=29.15721354 -82.12478889

    It should be producing this:

    http://maps.google.com/maps?q=29.1572135400 -82.1247888900

    It strips the 0s for both the NSString and the double.
     
  11. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #11
    And 'It' in this example is what? The assignment?

    NSLog(@"http://blah/maps?q=%0.10f %0.10f", 29.15721354, -82.12478889);
    or
    NSLog(@"http://blah/maps?q=%@ %@, @"29.1572135400", @"-82.1247888900")

    The first would be if they are stored as doubles ,the second as strings. I don't have a compiler on this machine, but I'm pretty sure they will both output what you were looking for.
    That said, since 10 decimal places is pretty close to the limit of good precision on doubles, I'd recommend storing them as NSStrings assuming that you don't need to perform math on them. You could also store them as NSDecimalNumber's for bonus points.
     
  12. johnmerlino thread starter macrumors member

    Joined:
    Oct 22, 2011
    #12
    I just noticed the cause of the problem. It wasnt the string or double stripping the 0s. It was the JSONKit plugin I am using. I took a look at the data it was returning and long and behold:

    Code:
        latitude = "29.06861979";
        longitude = "-82.13628889";
    
    It was stripping out the 0s before I even had an opportunity to work with the data.
     
  13. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #13
    Ok that makes sense, however if you're storing them as doubles and use an appropriate format string, you can still get 10 displayed digits after the decimal point.
    Just keep in mind that this is effectively the 'fringe' of double's accuracy.
    I would recommend learning to work with, and store the numbers in NSDecimalNumber instances. (from the NSStrings that you take in).
     

Share This Page