Reputation: 3
I'm moving my initial steps in the Core Data realm (and I'm quite new to iPhone development, too) and I found a behavior I cannot explain. I declared a subclass of a NSManagedObject and defined a few properties, some of them of type NSString *, MyObject.h is something like:
@interface MyObject : NSManagedObject {
}
@property (nonatomic, retain) NSString *contentFile;
@property (nonatomic, retain) NSString *contentPath;
@property (nonatomic, retain) NSDate *creationDate;
@property (nonatomic, retain) NSString *name;
@end
Now, if I try to create an object instance and assign a value to the name property, when I try to print back the content of the property it seems it's mangled. In the AppDelegate, where the whole Core Data stack is defined, I write:
MyObject *newObject = [NSEntityDescription insertNewObjectForEntityForName:@"MyObject"
inManagedObjectContext:self.managedObjectContext];
newObject.name = @"Testing";
NSLog([NSString stringWithFormat:@"Name: %s\n", newObject.name]);
What I get in the output console is
2009-08-24 20:03:55.176 MyApp[15727:20b] Name: ‡}00»
I can't understand if I'm doing something wrong or if I forgot something. Anyone can help, please?
Upvotes: 0
Views: 727
Reputation: 243166
You should use %@, not %s. %s is for char* strings. You're passing in an objective-c object.
Upvotes: 0
Reputation: 21882
The correct format specifier for Objective-C objects is %@
, not %s
.
Upvotes: 1
Reputation: 38015
You need to use the %@
format specifier in stringWithFormat:
, since NSString
is an Objective-C object:
NSLog([NSString stringWithFormat:@"Name: %@\n", newObject.name]);
%s
is used for C-strings (char*
s). For more info look at String Format Specifiers.
Upvotes: 7