Problem storing coordinates - iphone

I'm doing the following to store the longitude and latitude of a location in a custom (NSManagedObject) Bookmark object:
CLLocationCoordinate2D coordinate = [location coordinate];
// Set bookmark variables.
[bookmark setLatitude:[NSNumber numberWithDouble:coordinate.latitude]];
[bookmark setLongitude:[NSNumber numberWithDouble:coordinate.longitude]];
But something goes amiss along the way, and printing out the values of coordinate and bookmark yields this:
2011-03-09 12:56:30.793 XXXXXX[562:307] 55.615258, 12.985627 <- coordinate
2011-03-09 12:56:30.798 XXXXXX[562:307] 0.000000, 12.985626 <- bookmark
What happened to my Bookmark?
Bookmark.h
#interface Bookmark : NSManagedObject
{
}
#property (nonatomic, retain) NSDate * dateCreated;
#property (nonatomic, retain) NSString * longText;
#property (nonatomic, retain) NSString * shortText;
#property (nonatomic, retain) NSNumber * longitude;
#property (nonatomic, retain) NSNumber * latitude;
#end
Bookmark.m
#import "Bookmark.h"
#implementation Bookmark
#dynamic dateCreated;
#dynamic longText;
#dynamic shortText;
#dynamic longitude;
#dynamic latitude;
#end

Since everything seems to be fine from the code I suggest you take a look at managed object model. Probably entity with wrong data type? Also it could be that your NSLog (or other logging statement) has wrong placeholder in format (like %f instead of %#, since property is NSNumber object).

Related

core data fetching the information in the array objective-c

I'm quite new to Objective-C. I've been trying to fetch from my core data entity. The following code fetches the right row because it only returns 1 result.
but when I try to NSlog() it to see its values what I get is:
iGym[3922:c07] (
"<User: 0x8397690> (entity: User; id: 0x8341580 <x-coredata://114815EF-85F4-411F-925B-8479E1A94770/User/p19> ; data: <fault>)"
)
I am used to PHP that I just do a var_dump() and i get all the information in the array... specially when I am expecting 16 results as this entity has 16 fields.
Could anyone tell me how could I inspect that array?
and also, most important, how do I ultimately do this: fetch the Gender field of the matched email field and assign it to a NSString variable.
The query i want to do in sql is SELECT Gender FROM myTable WHERE email = "something#something.com;
-(NSInteger*)selectedGenderMethod
{
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:#"User" inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
request.predicate = [NSPredicate predicateWithFormat:#"email = %#",_currentUser];
NSError *error = nil;
NSArray *matches = [[context executeFetchRequest:request error:&error] mutableCopy];
NSString * someVar= [matches[0] objectForKey:#"email"];
NSLog(#"%#",someVar);
//More to come once this is sorted
return 0;
}
This fetching code is happening in my genderPickerViewController : UIViewController
NSLog(#"%#", matches[0]);
returns
<User: 0x83a6b00> (entity: User; id: 0x83995f0 <x-coredata://114815EF-85F4-411F-925B-8479E1A94770/User/p19> ; data: <fault>)
This is my User.h:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#interface User : NSManagedObject
#property (nonatomic, retain) NSDate * dob;
#property (nonatomic, retain) NSString * email;
#property (nonatomic, retain) NSNumber * firstTime;
#property (nonatomic, retain) NSString * gender;
#property (nonatomic, retain) NSString * height;
#property (nonatomic, retain) NSNumber * idFB;
#property (nonatomic, retain) NSNumber * idUserExternal;
#property (nonatomic, retain) NSNumber * idUserInternal;
#property (nonatomic, retain) NSNumber * isPT;
#property (nonatomic, retain) NSString * language;
#property (nonatomic, retain) NSString * metricSystem;
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) NSString * nickname;
#property (nonatomic, retain) NSString * password;
#property (nonatomic, retain) NSString * surname;
#property (nonatomic, retain) NSString * weight;
#end
User.m:
#implementation User
#dynamic dob;
#dynamic email;
#dynamic firstTime;
#dynamic gender;
#dynamic height;
#dynamic idFB;
#dynamic idUserExternal;
#dynamic idUserInternal;
#dynamic isPT;
#dynamic language;
#dynamic metricSystem;
#dynamic name;
#dynamic nickname;
#dynamic password;
#dynamic surname;
#dynamic weight;
#end
Is the user object a NSDictionary or an NSArray by chance? If that is the case you are simply logging out the object, you will need to specify a specific entity in the object to output.
For example if it's a NSDictionary
NSString *name = [matches[0] objectForKey:#"name"];
NSLog("Name %#", name);
You could also try
NSString *email = (User *)matches[0].email;
NSLog("Email %#", email);
What you are doing is right, you usually get the full objects and not only one attribute, Core Data is an object storage.
So you only need [((User *)matches[0]) gender] to get the gender of the returned user.
If you want to get some extra information from objects you must implement:
- (NSString *)description
It is used for converting to string and logging.
You can use the following code,
[NSPredicate predicateWithFormat:#"email CONTAINS[c] %#", _currentUser];
OR
[NSPredicate predicateWithFormat:#"email CONTAINS[cd] %#", _currentUser];
then you can print the fetched results as type po OBJECT_NAME.PROPERTY in debug area

internal inconsistency error with core data mappings

I am getting the following error:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Illegal container for relationship: value = (
"<CVStar: 0x6a2e990> (entity: CVStar; id: 0x6a2e980 <x-coredata:///CVStar/t20E391D3-6FA6-4521-84FE-EAA9469E012029> ; data: {\n \"date_created\" = \"1312050431.973905\";\n message = nil;\n user = \"0x6a2be10 <x-coredata:///CVLogin/t20E391D3-6FA6-4521-84FE-EAA9469E012030>\";\n})",
"<CVStar: 0x6a2aca0> (entity: CVStar; id: 0x6a2b0d0 <x-coredata:///CVStar/t20E391D3-6FA6-4521-84FE-EAA9469E012031> ; data: {\n \"date_created\" = \"1312011314.591517\";\n message = nil;\n user = \"0x6a36490 <x-coredata://B8EAEF54-AC90-46F0-B442-93077B937C3F/CVLogin/p28>\";\n})"
); relationship = "stars".'
Any idea on how to debug this? I have a class CVMessage which has an NSArray of CVStar.. I've setup a relationship on the model from CVMessage as a one to many to CVStar (A CVMessage can have many CVStar). This however doesn't work, how is this even possible?
Here's some code:
#class CVLogin;
#class CVTopic;
#interface CVMessage : NSManagedObject {
}
#property (nonatomic, retain) NSNumber * date_created;
#property (nonatomic, retain) NSString * message;
#property (nonatomic, retain) NSNumber * mid;
#property (nonatomic, retain) CVLogin * creator;
#property (nonatomic, retain) NSArray * stars;
#property (nonatomic, retain) NSArray * embeds;
#property (nonatomic, retain) CVTopic * topic;
#property (nonatomic, assign) BOOL options;
#end
#interface CVStar : NSManagedObject {
}
#property (nonatomic, retain) NSNumber * date_created;
#property (nonatomic, retain) CVLogin * user;
#end
some screenshots if it helped
NSManagedObjects cannot have NSArray as a property type, since Core Data does not support it. Instead, use NSSet, which should describe a relationship. If you must have ordered data, then add a property which describes the order of your objects. Also, keep in mind that Core Data is much more than a persistent store, and is not a SQL database. You'll do much better if you don't think of things as rows and columns, but rather as objects.

Cannot find protocol declaration MKAnnotation

The code is:
#interface RouteMapAnnotation : NSObject <MKAnnotation>
{
CLLocationCoordinate2D coordinate;
NSString* title;
NSString* subtitle;
}
#property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
#property (nonatomic, copy) NSString* title;
#property (nonatomic, copy) NSString* subtitle;
#end
Here's a snapshot of the error I have:
https://skitch.com/kuntul/rws3c/smartrek-routemapannotation.h
What is wrong? I've done this on the same exact project and it works..
Did you remember to #import <MapKit/MapKit.h>?
have you imported MKAnnotation.h ?
You forgot to #import <MapKit/MapKit.h>.
There really is no other reason why this would come up.
Just insert One line
#import < MapKit/MapKit.h>

Obj-c: Custom Object handling

I am creating my own custom object, and I am wondering if I need to retain my properties or use something else such as copy (which does what?)?
#interface Asset : NSObject {
NSString *name;
NSNumber *assetId;
NSNumber *linkId;
NSNumber *parentId;
}
#property (nonatomic, retain) NSString *name; // should I use retain here or something else?
#property (nonatomic, retain) NSNumber *assetId;
#property (nonatomic, retain) NSNumber *linkId;
#property (nonatomic, retain) NSNumber *parentId;
#end
Also, in my .m, do I also need synthesize as well as release?
The chapter on Declared Properties in The Objective-C Programming Language explains what copy does and about synthesizing accessors.
My personal preference:
#interface Asset : NSObject {
// no need to declare them here the #synthesize in the .m will sort all that out
}
// use copy for NSString as it is free for NSString instances and protects against NSMutableString instances being passed in...thanks to #bbum for this
#property (nonatomic, copy) NSString *name;
// no need for copy as NSNumber is immutable
#property (nonatomic,retain) NSNumber *assetId;
#property (nonatomic,retain) NSNumber linkId;
#property (nonatomic,retain) NSNumber parentId;
#end
For typical cases your .m will have lines like this:
#synthesize name;
...
That tells the compiler to automatically emit the getter/setter methods. You can also write/override these yourself.
So, when someone does a fooAsset.name = x, your object will retain its own reference to 'x'.
The other thing you need is a dealloc method to release the references to your members:
- (void)dealloc {
[name release];
....
[super dealloc];
}
Note that it'll still be appropriate if 'name' is never assigned; nil will silently eat the 'release' message.

Simple Question about Core Data

I using the project named coredatabooks from apple developer examples.
#import
#interface Book : NSManagedObject
{
}
#property (nonatomic, retain) NSNumber *active;
#property (nonatomic, retain) NSString *title;
#property (nonatomic, retain) NSString *author;
#property (nonatomic, retain) NSDate *copyright;
#end
I need to use a bool object, but I don't know how to implement it in my class.. I'm using right now NSNumber, using an integer (0, 1) but is not a Bool... maybe it will be more correct to use this ...
#property (nonatomic, retain) bool active;
but is not working.
You can still use an NSNumber, but pack it from a bool:
[NSNumber numberWithBool: myBool];
and unpack it to a bool:
[myNSNumber boolValue];