地址字符串传递给此方法,然后将其转换为具有 3 个字符串对象的数组,其中包含 streetNumber、streetName 和 cityName。然后将数组返回给调用者。
-(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]);
}