Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

johnmerlino

macrumors member
Original poster
Oct 22, 2011
81
0
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
 
So since its passing a float to the latitude and longitude methods, why does it tell me that its passing an id?

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.
 
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.

Can I just use an NSNumber rather than a float or double and get the same expected results (e.g. -82.1362888900)
 
Can I just use an NSNumber rather than a float or double and get the same expected results (e.g. -82.1362888900)

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.
 
Can I just use an NSNumber rather than a float or double and get the same expected results (e.g. -82.1362888900)

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].
 
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.
 
Then either store strings, or always compose on the extra zeroes. Example using the NSNumberFormatter class or a format string like @"%0.10f"
 
even when I keep them as strings, it still strips the 0s out.
 
Last edited:
actually I think its only keeping the 6 digits after the decimels, even for doubles

And you THINK that because...?

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

----------

actually I think its only keeping the 6 digits after the decimels, even for doubles and I cant store them as strings either, otherwise in the end it looks like this:

http://maps.google.com/maps?q=0.000000 -1.991600

What? So you are composing a URL (NSString) from your stored float's?
 
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.

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.
 
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.

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.
 
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.

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).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.