views:

50

answers:

2

This is my first project with Core Data, I followed the Event tutorial provided by Apple that helped me to understand the basic of core data in iPhone.

But now, working over my project, I've a problem adding data into my database.

When i create an object and set the data, if I try to get it back, the system returns me a strange sequence of characters.

This is what i see in log if I try to log it:

2010-05-11 00:16:43.523 FG[2665:207] Package: ‡}00å
2010-05-11 00:16:43.525 FG[2665:207] Package: ‡}00å
2010-05-11 00:16:43.526 FG[2665:207] Package: ‡}00å
2010-05-11 00:16:43.527 FG[2665:207] Package: ‡}00å
2010-05-11 00:16:43.527 FG[2665:207] Package: ‡}00å
2010-05-11 00:16:43.527 FG[2665:207] Items: 5

What kind of problem could be this?

Edit:

This is the part of the code that generate the error:

package = (Package *)[NSEntityDescription insertNewObjectForEntityForName:@"Package" inManagedObjectContext:moc];

    theNodes = [doc nodesForXPath:@"//pack" error:&error];
    for (CXMLElement *theElement in theNodes)
    {       
        // Create a counter variable as type "int"
        int counter;

        // Loop through the children of the current  node
        for(counter = 0; counter < [theElement childCount]; counter++) {

            if([[[theElement childAtIndex:counter] name] isEqualToString: @"id"])
                [package setIdPackage:[[theElement childAtIndex:counter] stringValue]];
            if([[[theElement childAtIndex:counter] name] isEqualToString: @"title"])
                [package setPackageTitle:[[theElement childAtIndex:counter] stringValue]];
            if([[[theElement childAtIndex:counter] name] isEqualToString: @"category"])
                [package setCategory:[[theElement childAtIndex:counter] stringValue]];
            if([[[theElement childAtIndex:counter] name] isEqualToString: @"lang"])
                [package setLang:[[theElement childAtIndex:counter] stringValue]];
            if([[[theElement childAtIndex:counter] name] isEqualToString: @"number"]) {
                NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
                [f setNumberStyle:NSNumberFormatterDecimalStyle];
                NSNumber * myNumber = [f numberFromString:[[theElement childAtIndex:counter] stringValue]];
                [f release];                
                [package setNumber:myNumber];
            }

        }

    }

    NSLog([NSString stringWithFormat:@"=== %s ===\nID: %s\nCategory: %s\nLanguage: %s",[package packageTitle], [package idPackage] ,[package category],[package lang]]);
A: 

You should use %@ instead of %s in your NSLog format, this will allow the Core Data object properties to show up properly (%@ is able to display pretty much every built-in NSObject classes).

Martin Cote
What a stupid error... You're right! Thanks!
Junior B.
A: 

Another tip in your code:

package = (Package *)[NSEntityDescription insertNewObjectForEntityForName:@"Package" inManagedObjectContext:moc];

Is a completely unnecessary cast. -[NSEntityDescription insertNewObjectForEntityForName: inManagedObjectContext: returns an id which is a generic pointer. No need to cast id ever. This line should read:

package = [NSEntityDescription insertNewObjectForEntityForName:@"Package" inManagedObjectContext:moc];
Marcus S. Zarra