Note that there are some explanatory texts on larger screens.

plurals
  1. POFix unicode characters in NSString
    text
    copied!<p>I am getting a string response from the Google Directions API that contains characters that are supposed to represent a different-language character. Similiar to ∆߬≥, similiar to these type of characters. I think the characters are portugese, but anyways my string contains like this:</p> <p>\U00e3o </p> <p>(I am not sure if those are zeroes or 'O's)</p> <p>I am not sure what the technical term for these characters are, but how can I fix them in my string so they print properly.</p> <p>Thank you</p> <p>UPDATE:</p> <p>I have updated my question title with the correct term 'unicode'.</p> <p>I have checked a few questions:</p> <p><a href="https://stackoverflow.com/questions/6839287/nsstring-unicode-display">NSString Unicode display</a></p> <p><a href="https://stackoverflow.com/questions/1706633/detect-unicode-characters-in-nsstring-on-iphone">Detect Unicode characters in NSString on iPhone</a></p> <p><a href="https://stackoverflow.com/questions/7602693/ios-html-unicode-to-nsstring">iOS HTML Unicode to NSString?</a></p> <p>And a few others. I have followed the answer, but the unicode characters are not fixed. </p> <p>UPDATE:</p> <p>Here is my code to get the response from GDirection. </p> <p>Forming the request and getting a response:</p> <pre><code> AFHTTPClient *_httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://maps.googleapis.com/"]]; [_httpClient registerHTTPOperationClass: [AFJSONRequestOperation class]]; [_httpClient setDefaultHeader:@"Accept" value:@"application/json"]; NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init]; [parameters setObject:[NSString stringWithFormat:@"%f,%f", location.coordinate.latitude, location.coordinate.longitude] forKey:@"origin"]; [parameters setObject:[NSString stringWithFormat:@"%f,%f", location2.coordinate.latitude, location2.coordinate.longitude] forKey:@"destination"]; [parameters setObject:@"false" forKey:@"sensor"]; [parameters setObject:@"driving" forKey:@"mode"]; [parameters setObject:@"metric" forKey: @"units"]; NSMutableURLRequest *request = [_httpClient requestWithMethod:@"GET" path: @"maps/api/directions/json" parameters:parameters]; request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData; AFHTTPRequestOperation *operation = [_httpClient HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) { NSInteger statusCode = operation.response.statusCode; if (statusCode == 200) { [self parseResponse:responseObject]; } else { } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { }]; [_httpClient enqueueHTTPRequestOperation:operation]; } </code></pre> <p>Retrieving information from response object:</p> <pre><code>- (void)parseResponse:(NSDictionary *)response { NSString *status = [response objectForKey: @"status"]; if (![status isEqualToString: @"OK"]) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat: @"Google Directions Response Status: %@", status] delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, nil]; [alert show]; } else { NSArray *routes = [response objectForKey:@"routes"]; NSDictionary *routePath = [routes lastObject]; if (routePath) { NSString *overviewPolyline = [[routePath objectForKey: @"overview_polyline"] objectForKey:@"points"]; legs = [routePath valueForKey: @"legs"]; if (legs) { </code></pre> <p>/* DIRECTION SET ================================================================================================================================ */</p> <pre><code> directionOverview = [[NSMutableDictionary alloc] init]; NSString *legsDis = [NSString stringWithFormat: @"%@", [[legs valueForKey: @"distance"] valueForKey: @"text"]]; NSString *kmDistance = [self cutStringToPreference: legsDis]; if (kmDistance) { [directionOverview setObject:kmDistance forKey: @"distance"]; milesLabel.text = kmDistance; milesLabel.font = [UIFont fontWithName:@"interstate" size: 20.0]; } NSString *durationText = [NSString stringWithFormat: @"%@", [[legs valueForKey: @"duration"] valueForKey: @"text"]]; durationText = [self cutStringToPreference: durationText]; if (durationText) { [directionOverview setObject:durationText forKey: @"duration"]; } NSString *startAddress = [NSString stringWithFormat: @"%@", [legs valueForKey: @"start_address"]]; startAddress = [self cutStringToPreference: startAddress]; NSString *endAddress = [NSString stringWithFormat: @"%@", [legs valueForKey: @"end_address"]]; endAddress = [self cutStringToPreference: endAddress]; [directionOverview setObject:startAddress forKey: @"origin"]; [directionOverview setObject:endAddress forKey: @"destination"]; NSArray *steps = [legs valueForKey: @"steps"]; if (steps) { instructionArray = [[NSMutableArray alloc] init]; durationArray = [[NSMutableArray alloc] init]; distanceArray = [[NSMutableArray alloc] init]; int number = [[[steps lastObject] valueForKey: @"html_instructions"] count]; for (int i = 1; i &lt;= number; ++i) { NSString *instruction = [[[steps lastObject] valueForKey: @"html_instructions"] objectAtIndex: i-1]; instruction = [self cutStringToPreference: instruction]; instruction = [self flattenHTML: instruction]; instruction = [self stringByDecodingHTMLEntitiesInString: instruction]; [instructionArray addObject: instruction]; NSString *distance = [[[[steps lastObject] valueForKey: @"distance"] objectAtIndex: i-1] valueForKey: @"text"]; [distanceArray addObject: distance]; NSString *duration = [[[[steps lastObject] valueForKey: @"duration"] objectAtIndex: i-1] valueForKey: @"text"]; [durationArray addObject: duration]; } } } _path = [self decodePolyLine:overviewPolyline]; NSInteger numberOfSteps = _path.count; CLLocationCoordinate2D coordinates[numberOfSteps]; for (NSInteger index = 0; index &lt; numberOfSteps; index++) { CLLocation *location = [_path objectAtIndex:index]; CLLocationCoordinate2D coordinate = location.coordinate; coordinates[index] = coordinate; } polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps]; [self.mapView addOverlay:polyLine]; } } } </code></pre> <p>Displaying the text in a label:</p> <pre><code> NSString *overviewAddressText = [NSString stringWithFormat: @"%@ to %@", [directionOverview objectForKey: @"origin"], [directionOverview objectForKey: @"destination"]]; overviewAddress.text = overviewAddressText; </code></pre> <p>UPDATE:</p> <p><a href="https://i.stack.imgur.com/oE7LE.png" rel="nofollow noreferrer">Image of label</a></p> <p>So as you can see, in the label, the text contains this kind of substring: <code>S/U00e3o</code>, which is a word that has an unsupported character. How can I fix that so that unicode turns into this: São </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload