住所文字列がこのメソッドに渡され、streetNumber、streetName、cityName を含む 3 つの文字列オブジェクトを持つ配列に変換されます。次に、配列が呼び出し元に返されます。
-(NSArray *)brakeAddress:(NSString *)address{
NSMutableArray *arr=[NSMutableArray arrayWithArray:[address componentsSeparatedByString:@","]];
if (arr.count>2) {
[arr removeObjectAtIndex:0];
}
NSInteger streetNameInd=[arr count]-2, cityNameInd=[arr count]-1;
NSMutableArray *streetNameArray=[NSMutableArray arrayWithObjects:arr[0], nil];
if ([arr[streetNameInd] intValue]) {
streetNameArray=[NSMutableArray arrayWithArray:([arr[streetNameInd] componentsSeparatedByString:@" "])];
[streetNameArray removeObjectAtIndex:0];
if ([streetNameArray[0] intValue] ==[arr[streetNameInd] intValue]) {
[streetNameArray removeObjectAtIndex:0];
}
}
NSString *streetName=[streetNameArray componentsJoinedByString:@" "];
NSString *streetNumber=@"";
if ([arr[streetNameInd] intValue]!=0) {
streetNumber=[NSString stringWithFormat:@"%d", [arr[streetNameInd] intValue]];
}
NSString *city=arr[cityNameInd];
// NSLog(@"\nstreet number :%@\nstreet name :%@\ncity name :%@",streetNumber, streetName,city);
NSArray *addressParts=[NSArray arrayWithObjects:streetNumber, streetName, city, nil];
return addressParts;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
NSString *str1=@"34 Ellis Street, San Francisco";
NSString *str2=@"4FL, 800 Market Street, San Francisco";
NSString *str3=@"Ellis & Market, San Francisco";
NSArray *firstAddress=[self brakeAddress:str1];
NSArray *secondAddress=[self brakeAddress:str2];
NSArray *thirdAddress=[self brakeAddress:str3];
NSLog(@"\n1st : street number :%@\nstreet name :%@\ncity name :%@",firstAddress[0],firstAddress[1],firstAddress[2]);
NSLog(@"\n2nd : street number :%@\nstreet name :%@\ncity name :%@",secondAddress[0],secondAddress[1],secondAddress[2]);
NSLog(@"\n3rd : street number :%@\nstreet name :%@\ncity name :%@",thirdAddress[0],thirdAddress[1],thirdAddress[2]);
}