views:

163

answers:

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?

+7  A: 

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.

Perspx
Thanks, I knew it should have been a silly thing...
Eugenio
+1  A: 

The correct format specifier for Objective-C objects is %@, not %s.

Daniel Dickison
Thanks for your help
Eugenio
A: 

You should use %@, not %s. %s is for char* strings. You're passing in an objective-c object.

Dave DeLong
Thanks for your help
Eugenio