I am new to iOS programming. I am developing an app which gets some sentences from my web service which I insert into my local database However, some sentences contain turkish characters such as; "ü, ğ, ç, ö". I can insert these characters to in my database fine, but when I output them to a UILabel
it looks like: "ş ğ ü ö". I have tried to convert to NSUTF8StringEncoding but to no avail.
I have an NSMutableArray
, named questions, which contains some turkish characters. How can I convert this Array to NSUTF8String format?
Sorry for my bad english :) Thanks.
Ahh, I think the problem is not that you need to convert your array - it is how you display the strings.
Check this code which has an array of Turkish footballers:
NSMutableArray *questions = [[NSMutableArray alloc] initWithObjects:@"Emre Belözoğlu", @"Gökhan Gönül", @"Mevlüt Erdinç", @"Nuri Şahin", nil];
const char *utf8string = [[NSString stringWithFormat:@"%@", [questions objectAtIndex:0]] UTF8String];
self.title = [NSString stringWithUTF8String:utf8string];
It should now display the title of the page as "Emre Belözoğlu" in correct formatting.
The problem could alternatively be with the way the data is parsed from the SQLite to your NSMutableArray
. Here is how I have achieved it for a Formula 1 app.
In viewDidLoad
I call [self loadNamesFromDatabase];
which looks like:
- (void)loadNamesFromDatabase
{
NSString *file = [[NSBundle mainBundle] pathForResource:@"Formula1" ofType:@"sqlite"];
sqlite3 *database = NULL;
if (sqlite3_open([file UTF8String], &database) == SQLITE_OK) {
sqlite3_exec(database, "select driver_id, driver_name, driver_nation, driver_team from drivers", MyCallback, drivers, NULL);
}
sqlite3_close(database);
}
and the MyCallback
function you see there is as follows:
static int MyCallback(void *context, int count, char **values, char **columns)
{
NSMutableArray *drivers = (NSMutableArray *)context;
NSString *columnName;
NSString *columnValue;
NSMutableDictionary * record = [NSMutableDictionary dictionary];
for (int i=0; i < count; i++) {
columnName = [NSString stringWithUTF8String:columns[i]];
if (values[i]){
columnValue = [NSString stringWithUTF8String:values[i]];
}
else{
columnValue = @"";
}
[record setObject:columnValue forKey:columnName];
}
[drivers addObject:record];
return SQLITE_OK;
}
where drivers
is an NSMutableArray
. The key thing here is stringWithUTF8String
and you would not need to use the code at the top as all data should be encoded properly and thus will be presented correctly.
Can you try another font style. e.g.;
myLabel.font = [UIFont fontWithName:@"Futura" size:10];
And what it seems on NSLog lines?
Here is the alternative answer:
NSString *text = brokenString;
NSData *data = [text dataUsingEncoding: [NSString defaultCStringEncoding] ];
NSString *convertedToTurkishString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding ];
const char *utfString = [strText cStringUsingEncoding:NSMacOSRomanStringEncoding];