I have a problem when I add an arabic string to NSMutableArray or NSMutableDictionary.
Example:
NSMutableDictionary *data = [[NSMutableDictionary alloc]init];
[data setObject:@"فرسان" forKey:@"name"];
NSLog(@"%@",data);
Output:
2012-01-18 21:55:05.646 aa[367:207] {
name = "\U0641\U0631\U0633\U0627\U0646";
}
my problem exactly i save this data to sqlite [data objectForKey:@”name”] its saved \U0641\U0631\U0633\U0627\U0646 and when fetch data to to put it in UILabel or anything like it the text be \U0641\U0631\U0633\U0627\U0646
Any Help? Thank you 🙂
- (void)SaveMessage {
NSMutableDictionary *TableProperties = [[NSMutableDictionary alloc]init];
[TableProperties setObject:@"INSERT" forKey:@"Operation"];
[TableProperties setObject:@"savedmessages" forKey:@"tableName"];
NSString *string = [[NSString alloc]initWithFormat:@" %@",MessageBox.text];
[TableProperties setValue:string forKey:@"message"];
NSString *msgResult;
NSArray *result = [[DatabaseFunctions database] DataBaseOperation:TableProperties];
if ([[result objectAtIndex:0] isEqualToString:@"Done"])
msgResult = @"تم حفظ الرسالة بنجاح";
else
msgResult = @"لم تت العملية بنجاح حاول لاحقا";
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"حفظ الرسالة"
message:msgResult
delegate:self
cancelButtonTitle:@"موافق"
otherButtonTitles:nil, nil ];
[alert show];
}
- (NSArray *)DataBaseOperation:(NSMutableDictionary *)TableProperties{
NSMutableArray *retval = [[NSMutableArray alloc] init];
NSString *Operation = [TableProperties objectForKey:@"Operation"];
if ([Operation isEqualToString:@"SELECT" ]) {
NSString *tableColumns = [TableProperties objectForKey:@"tableColumns"];
NSString *tableName = [TableProperties objectForKey:@"tableName"];
NSString *tableWhere = [TableProperties objectForKey:@"tableWhere"];
NSString *tableOrder = [TableProperties objectForKey:@"tableOrder"];
NSString *tableLimit = [TableProperties objectForKey:@"tableLimit"];
NSString *Query;
if ([tableLimit isEqualToString:@"NO"]) {
Query = [[NSString alloc] initWithFormat:@"SELECT %@ FROM %@ WHERE %@ ORDER BY %@",
tableColumns,tableName,tableWhere,tableOrder,tableLimit];
}else{
Query = [[NSString alloc] initWithFormat:@"SELECT %@ FROM %@ WHERE %@ ORDER BY %@ LIMIT %@",
tableColumns,tableName,tableWhere,tableOrder,tableLimit];
}
NSArray *FieldArray = [[TableProperties objectForKey:@"tableColumns"] componentsSeparatedByString:@","];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(_database, [Query UTF8String], -1, &statement, nil) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
NSMutableDictionary *Row = [[NSMutableDictionary alloc]init];
NSString *uniqueId = [[NSString alloc] initWithFormat:@"%i",sqlite3_column_int(statement, 0)];
[Row setObject:uniqueId forKey:@"uniqueId"];
for (int i = 1; i<[FieldArray count]; i++) {
NSString *column = [[NSString alloc] initWithUTF8String:(char *) sqlite3_column_text(statement, i)];
[Row setObject:column forKey:[FieldArray objectAtIndex:i]];
}
Objects *rowOfTable = [[Objects alloc] initSelectDataFromTables:Row];
Row = nil;
[retval addObject:rowOfTable];
}
sqlite3_finalize(statement);
}
}
else if([Operation isEqualToString:@"INSERT" ]) {
NSString *tableName = [TableProperties objectForKey:@"tableName"];
[TableProperties removeObjectForKey:@"Operation"];
[TableProperties removeObjectForKey:@"tableName"];
NSArray *Columns = [TableProperties allKeys];
NSArray *Values = [TableProperties allValues];
NSString *Query = [[NSString alloc] initWithFormat:@"INSERT INTO %@ %@ VALUES %@",tableName,Columns,Values];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(_database, [Query UTF8String], -1, &statement, nil) == SQLITE_OK)
if (SQLITE_DONE!=sqlite3_step(statement)){
NSLog(@"Error when inserting %s",sqlite3_errmsg(_database));
[retval addObject:@"Error"];
}else{
NSLog(@"Data inserted Successfully");
[retval addObject:@"Done"];
}
else{
NSLog(@"Error when inserting %s",sqlite3_errmsg(_database));
[retval addObject:@"Error"];
}
sqlite3_finalize(statement);
}
return retval;
}
When you log an object using
%@,NSLogsends thedescriptionmessage to the object and prints the resulting string.An
NSDictionaryresponds to thedescriptionmessage by encoding its keys and values in a “safe” format, escaping non-ASCII characters using\U####codes.If you pass the Arabic string to
NSLogdirectly, it will just print the string without the escape codes:Output:
If you don’t like the way
NSDictionaryresponds todescription, you will have to write your own method to format a dictionary as a string and use it to log your dictionary.Update
I have looked at the source code you posted that talks to sqlite. The problem is that you are turning the values array (
[TableProperties allValues]) into a string using the%@format specifier. This sends thedescriptionmethod to theNSArray, which returns a string. Just likeNSDictionary,NSArrayformats the description string in a “safe” format, escaping non-ASCII characters using\U####codes.You need to write your own method that takes an array and turns it into a string and does not escape special characters. (It needs to escape quotes though.)