diff --git a/TBAnnotationClustering/TBCoordinateQuadTree.m b/TBAnnotationClustering/TBCoordinateQuadTree.m index 7028980..7e4d2af 100644 --- a/TBAnnotationClustering/TBCoordinateQuadTree.m +++ b/TBAnnotationClustering/TBCoordinateQuadTree.m @@ -22,13 +22,17 @@ TBQuadTreeNodeData TBDataFromLine(NSString *line) TBHotelInfo* hotelInfo = malloc(sizeof(TBHotelInfo)); - NSString *hotelName = [components[2] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; - hotelInfo->hotelName = malloc(sizeof(char) * hotelName.length + 1); - strncpy(hotelInfo->hotelName, [hotelName UTF8String], hotelName.length + 1); - - NSString *hotelPhoneNumber = [[components lastObject] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; - hotelInfo->hotelPhoneNumber = malloc(sizeof(char) * hotelPhoneNumber.length + 1); - strncpy(hotelInfo->hotelPhoneNumber, [hotelPhoneNumber UTF8String], hotelPhoneNumber.length + 1); + const char *hotelName = [[components[2] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] UTF8String]; + size_t hotelNameLength = strlen(hotelName) + 1; + hotelInfo->hotelName = malloc(hotelNameLength); + memset(hotelInfo->hotelName, 0, hotelNameLength); + strncpy(hotelInfo->hotelName, hotelName, hotelNameLength); + + const char *hotelPhoneNumber = [[[components lastObject] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] UTF8String]; + size_t hotelPhoneNumberLength = strlen(hotelPhoneNumber) + 1; + hotelInfo->hotelPhoneNumber = malloc(hotelPhoneNumberLength); + memset(hotelInfo->hotelPhoneNumber, 0, hotelPhoneNumberLength); + strncpy(hotelInfo->hotelPhoneNumber, hotelPhoneNumber, hotelPhoneNumberLength); return TBQuadTreeNodeDataMake(latitude, longitude, hotelInfo); } @@ -90,7 +94,7 @@ @implementation TBCoordinateQuadTree - (void)buildTree { @autoreleasepool { - NSString *data = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"USA-HotelMotel" ofType:@"csv"] encoding:NSASCIIStringEncoding error:nil]; + NSString *data = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"USA-HotelMotel" ofType:@"csv"] encoding:NSUTF8StringEncoding error:nil]; NSArray *lines = [data componentsSeparatedByString:@"\n"]; NSInteger count = lines.count - 1; @@ -133,8 +137,8 @@ - (NSArray *)clusteredAnnotationsWithinMapRect:(MKMapRect)rect withZoomScale:(do count++; TBHotelInfo hotelInfo = *(TBHotelInfo *)data.data; - [names addObject:[NSString stringWithFormat:@"%s", hotelInfo.hotelName]]; - [phoneNumbers addObject:[NSString stringWithFormat:@"%s", hotelInfo.hotelPhoneNumber]]; + [names addObject:[NSString stringWithFormat:@"%@", [NSString stringWithUTF8String:hotelInfo.hotelName]]]; + [phoneNumbers addObject:[NSString stringWithFormat:@"%@", [NSString stringWithUTF8String:hotelInfo.hotelPhoneNumber]]]; }); if (count == 1) {