views:

432

answers:

1

Hey all, I'm a total noob when it comes to Objective-C / iPhone development.

I'm trying to pull in text from a SQLite DB. I have a while loop that looks like this:

while(sqlite3_step(selectstmt) == SQLITE_ROW) {

And within that loop, this prints to the log just fine:

NSLog(@"Text: %s",sqlite3_column_text(selectstmt, 1));

This does not work:

Category *categoryObj = [[Category alloc] initWithPrimaryKey:primaryKey];
categoryObj.categoryName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
NSLog(@"cat name: %s",categoryObj.categoryName);

When I run the above and look at the logs I see:

cat name: ‡}00å

I tried to write the field out to a label, thinking it might be something specific to the NSLog but nothing shows up there. Clearly I'm missing something fundamental but I'm at a loss for what it is.

+4  A: 

Log your string with %@ instead of %s and you'll be fine. NSStrings aren't pointers to characters, they're full-fledged objects, so you need to use the "object" placeholder in the log format string.

This has the added advantage of doing the right thing with non-ASCII strings and all of the other important things that NSString gives you.

Note that if you had just logged the result from SQLite directly instead of creating an NSString with it, then your %s would have been correct.

Remember: %s is for C strings, %@ is for Objective-C objects.

Jim Puls