iPhone stringWithCString is deprecated

ghiboz picture ghiboz · Jul 24, 2010 · Viewed 19.6k times · Source

I use this code to read data from sqlite database:

keyFromSql = [NSString stringWithCString:(char *)sqlite3_column_text(preparedStatement, 1)];

but the compiler give me the warning wrote in the title... so, what is the right and not deprecated method to retrieve a value from sqlite?

thanks!

Answer

Austen Green picture Austen Green · Jul 24, 2010
+(id)stringWithCString:(const char *)cString encoding:(NSStringEncoding)enc

I think you'll typically use NSUTF8StringEncoding, so your code would look like

keyFromSq1 = [NSString stringWithCString:(char *)sqlite3_column_text(preparedStatement, 1) encoding:NSUTF8StringEncoding];

Alternatively you can use

keyFromSq1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(preparedStatement, 1)];