views:

356

answers:

1

I do not understand why i am getting this error. Here is the related code:

Photo.h

#import <CoreData/CoreData.h>

@class Person;

@interface Photo :  NSManagedObject  
{
}

@property (nonatomic, retain) NSData * imageData;
@property (nonatomic, retain) NSNumber * Latitude;
@property (nonatomic, retain) NSString * ImageName;
@property (nonatomic, retain) NSString * ImagePath;
@property (nonatomic, retain) NSNumber * Longitude;
@property (nonatomic, retain) Person * PhotoToPerson;

@end

Photo.m

#import "Photo.h"

#import "Person.h"

@implementation Photo 

@dynamic imageData;
@dynamic Latitude;
@dynamic ImageName;
@dynamic ImagePath;
@dynamic Longitude;
@dynamic PhotoToPerson;

@end

This is a mapViewController.m class i have created. If i run this, the CLLocationDegrees CLLat and CLLong lines:

                CLLocationDegrees CLLat = (CLLocationDegrees)photo.Latitude;    
                CLLocationDegrees CLLong = (CLLocationDegrees)photo.Longitude;

give me the error : pointer value used where a floating point value was expected.

for(int i = 0; i < iPerson; i++)
        {
            //get the person that corresponds to the row indexPath that is currently being rendered and set the text
            Person * person = (Person *)[myArrayPerson objectAtIndex:i];
            //get the photos associated with the person
            NSArray * PhotoArray =  [person.PersonToPhoto allObjects];
            int iPhoto = [PhotoArray count];

        for(int j = 0; j < iPhoto; j++)
        {
            //get the first photo (all people will have atleast 1 photo, else they will not exist). Set the image
            Photo * photo = (Photo *)[PhotoArray objectAtIndex:j];

            if(photo.Latitude != nil && photo.Longitude != nil)
            {
                MyAnnotation *ann = [[MyAnnotation alloc] init];
                ann.title = photo.ImageName;
                ann.subtitle = photo.ImageName;

                CLLocationCoordinate2D cord;                    
                CLLocationDegrees CLLat = (CLLocationDegrees)photo.Latitude;    
                CLLocationDegrees CLLong = (CLLocationDegrees)photo.Longitude;

                cord.latitude = CLLat;
                cord.longitude = CLLong;

                ann.coordinate = cord;
                [mkMapView addAnnotation:ann];              

            }       
        }   
    }
+5  A: 

NSNumber is not a float type, but a pointer, so you need to do this to convert it:

CLLocationDegrees CLLat = (CLLocationDegrees)[photo.Latitude doubleValue];    
CLLocationDegrees CLLong = (CLLocationDegrees)[photo.Longitude doubleValue];
fsmc
CLLocationDegrees is a `double`. You should use the `-doubleValue` method.
KennyTM
This works, thanks. Am i not able to do (double)photo.Latitude; because i am trying to convert a NSNumber pointer to a value by casting? Thanks!
Mausimo
Yes, casting won't work here because like in c, it will just reinterpret the address the pointer is pointing to as a double, which will give you a nonsensical value (although it might let you compile.) This is a place to be thankful for the error.
fsmc