修复NSString中的unicode字符

时间:2012-08-28 13:02:22

标签: objective-c ios nsstring character

我从Google Directions API获得字符串响应,其中包含应该代表不同语言字符的字符。与Δ߬≥类似,类似于这些类型的角色。我认为这些角色都是葡萄牙语,但无论如何我的字符串包含如下:

\ U00e3o

(我不确定这些是零还是'O')

我不确定这些字符的技术术语是什么,但如何在字符串中修复它们以便正确打印。

谢谢

更新:

我已使用正确的术语“unicode”更新了我的问题标题。

我查了几个问题:

NSString Unicode display

Detect Unicode characters in NSString on iPhone

iOS HTML Unicode to NSString?

还有其他一些人。我已按照答案,但unicode字符不固定。

更新:

这是我从GDirection获取响应的代码。

形成请求并获得回复:

        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];
}

从响应对象中检索信息:

- (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) {

/ * DIRECTION SET =========================================== ================================================== ===================================             * /

        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 <= 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 < numberOfSteps; index++) {
        CLLocation *location = [_path objectAtIndex:index];
        CLLocationCoordinate2D coordinate = location.coordinate;

        coordinates[index] = coordinate;
    }

    polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
    [self.mapView addOverlay:polyLine];
}

}

}

在标签中显示文字:

        NSString *overviewAddressText = [NSString stringWithFormat: @"%@ to %@", [directionOverview objectForKey: @"origin"], [directionOverview objectForKey: @"destination"]];

    overviewAddress.text = overviewAddressText;

更新:

Image of label

正如您所看到的,在标签中,文本包含这种子字符串:S/U00e3o,这是一个具有不受支持的字符的单词。我该如何解决这个问题,以便unicode变成这样:São

0 个答案:

没有答案