I retrieve names from the database and store their first and last name in an NSMutableDictionary and store those in an NSMutableArray. In the database I have names that have special characters, such as
‘ñ’, ‘Ç’ and of sorts and when I store the name in a variable, it stores , which crashes my program.
I don’t want to restrict the user input, so I have to keep these characters. But is there a work around to retrieving these names with such characters without getting for output?
ConDAO *con = [[ConDAO alloc] init];
NSMutableArray *resultSet = [con executeMySqlCommand:[NSString stringWithFormat:(Long query here)]];
NSMutableDictionary * endList = [[NSMutableDictionary alloc] init];
[endList setObject: @"" forKey:@"surname"];
[endList setObject: @"" forKey:@"name"];
resultSet =(NSMutableArray *)[resultSet arrayByAddingObject:endList];
for (int i =0; i< [resultSet count]-1; ++i)
{
NSMutableDictionary *temp = (NSMutableDictionary *) [resultSet objectAtIndex:i];
...
}
-(NSMutableArray *) executeMySqlCommand: (NSString *) mysqlCommand{
LoginData* loginData=[self dataObject];
NSString *post =[NSString stringWithFormat:@"username=%@&password=%@&mysqlCommand=%@",
loginData.loginName, loginData.loginPassword, mysqlCommand ];
NSString *hostStr = @myIP;
hostStr = [hostStr stringByAppendingString:post];
NSString *encodeURL =[hostStr stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
NSData *dataURL = [NSData dataWithContentsOfURL: [ NSURL URLWithString: encodeURL ]];
NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];
if ([serverOutput length]<=0) return nil;
NSError * error;
NSMutableArray *data = [NSJSONSerialization JSONObjectWithData:dataURL options:0 error:&error];
return data;
}
It was actually a problem of using the .php. The encoding that was used did not cover the special characters, so I had to use UTF8 encoding inside the .php when it makes the DB call.