gpt4 book ai didi

iphone - CLPlacemark - 国家缩写?

转载 作者:IT王子 更新时间:2023-10-29 08:10:25 24 4
gpt4 key购买 nike

我想知道是否可以从 CLPlacemark 获取州的缩写?

在 Apple 的 CLPlacemark Reference 中它指出:

行政区域与地标关联的州或省。 (只读)@property(nonatomic, readonly) NSString *administrativeArea讨论例如,如果地标位置是 Apple 总部,则此属性的值将是字符串“CA”或“California”。

但每当我使用它时,我只会得到完整的州(即加利福尼亚州)而不是缩写(即 CA)。有人可以帮我吗?

最佳答案

对于需要为此解决方案的任何其他人,我已经为 CLPlacemark 创建了一个返回短状态字符串的类别类。您需要做的就是调用 myPlacemark shortState

CLPlacemark+ShortState.h

#import <CoreLocation/CoreLocation.h>
#import <Foundation/Foundation.h>

@interface CLPlacemark (ShortState)

- (NSString *)shortState;

@end

CLPlacemark+ShortState.m

#import "CLPlacemark+ShortState.h"

@interface CLPlacemark (ShortStatePrivate)

- (NSDictionary *)nameAbbreviations;

@end

@implementation CLPlacemark (ShortState)

- (NSString *)shortState {

NSString *state = [self.administrativeArea lowercaseString];

if (state.length==0)
return nil;

return [[self nameAbbreviations] objectForKey:state];

}

- (NSDictionary *)nameAbbreviations {

static NSDictionary *nameAbbreviations = nil;

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{

nameAbbreviations = [NSDictionary dictionaryWithObjectsAndKeys:
@"AL",@"alabama",
@"AK",@"alaska",
@"AZ",@"arizona",
@"AR",@"arkansas",
@"CA",@"california",
@"CO",@"colorado",
@"CT",@"connecticut",
@"DE",@"delaware",
@"DC",@"district of columbia",
@"FL",@"florida",
@"GA",@"georgia",
@"HI",@"hawaii",
@"ID",@"idaho",
@"IL",@"illinois",
@"IN",@"indiana",
@"IA",@"iowa",
@"KS",@"kansas",
@"KY",@"kentucky",
@"LA",@"louisiana",
@"ME",@"maine",
@"MD",@"maryland",
@"MA",@"massachusetts",
@"MI",@"michigan",
@"MN",@"minnesota",
@"MS",@"mississippi",
@"MO",@"missouri",
@"MT",@"montana",
@"NE",@"nebraska",
@"NV",@"nevada",
@"NH",@"new hampshire",
@"NJ",@"new jersey",
@"NM",@"new mexico",
@"NY",@"new york",
@"NC",@"north carolina",
@"ND",@"north dakota",
@"OH",@"ohio",
@"OK",@"oklahoma",
@"OR",@"oregon",
@"PA",@"pennsylvania",
@"RI",@"rhode island",
@"SC",@"south carolina",
@"SD",@"south dakota",
@"TN",@"tennessee",
@"TX",@"texas",
@"UT",@"utah",
@"VT",@"vermont",
@"VA",@"virginia",
@"WA",@"washington",
@"WV",@"west virginia",
@"WI",@"wisconsin",
@"WY",@"wyoming",
nil];
});

return nameAbbreviations;
}

@end

关于iphone - CLPlacemark - 国家缩写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11108424/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com