CLPlacemark - State Abbreviations?

后端 未结 7 1504
隐瞒了意图╮
隐瞒了意图╮ 2021-02-19 00:24

I was wondering if it was possible to get the state abbreviations from CLPlacemark?

In the CLPlacemark Reference from Apple it states:

administrativeArea The sta

相关标签:
7条回答
  • 2021-02-19 00:59

    I think you can't get the abbreviations of the states but you can make your own class for this..

    • List all the states(states are standards)
    • compare those states and return the abbreviation

    Code..

    Class StateAbbreviation

    StateAbbreviation.h

    @interface StateAbbreviation : NSString {
    
    }
    
    + (NSString *)allStates:(int)index;
    + (NSString *)abbreviatedState:(NSString *)strState;
    
    @end
    

    StateAbbreviation.m

    @implementation StateAbbreviation
    + (NSString *)allStates:(NSString *)strState {
       // Remove all space on the string
       strState = [strState stringByReplacingOccurrencesOfString:@" " withString:@""];
       //Sample states
       NSArray *states = [[NSArray alloc] initWithObjects:
                           @"ALABAMA",                      
                           @"ALASKA",        //AK
                           @"AMERICANSAMOA", //AS
                           @"ARIZONA",       //AZ
                           @"ARKANSAS",      //AR
                           @"CALIFORNIA",    //CA
                           nil];
      NSUInteger n = [states indexOfObject:strState];
      if (n > [states count] - 1) {
         strAbbreviation = @"NOSTATE";
      }
      else {
         strAbbreviation =[self abbreviatedState:n];
      }
      [states release];
      return strAbbreviation;
    }
    
    + (NSString *)abbreviatedState:(int)index {
        NSArray *states = [[NSArray alloc] initWithObjects:
                           @"AL",
                           @"AK",
                           @"AS",
                           @"AZ",
                           @"AR",
                           @"CA",
                           nil];
           NSString *strAbbreviation = [states objectAtIndex:index];
           [states release];
           return strAbbreviation;
    }
    @end
    

    When you call the class it should be something like this

    NSString *upperCase = [@"California" uppercaseString]; // California could be from (NSString *)placemark.administrativeArea;
    NSString *abbr = [StateAbbreviation allStates:upperCase];
    NSLog(@"%@", abbr); // Result should be CA
    

    This are only samples you can research all states something like this, states and their abbreviations also like this states and their abbreviations

    0 讨论(0)
提交回复
热议问题