You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.7 KiB

//
// CountryModel.m
// tongxin
//
// Created by WeiChaoZheng on 2018/6/25.
// Copyright © 2018xTT. All rights reserved.
//
#import "CountryModel.h"
#import "MJExtension.h"
#import "MyHttp.h"
@implementation CountryModel
+ (void)getObjsWithSuccess:(void (^)(NSMutableArray *arr))success
failure:(void (^)(NSError *error))failure{
NSString *urlStr = @"/getway/accounts/country";
NSArray *arr = [UserDefaults valueForKey:kCountryCodeListKey];
if(arr){
NSString *timeStr = [UserDefaults valueForKey:kgetCountryListTime];
if(timeStr){
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSDate *nowDate = [NSDate date];
NSString *nowDay = [formatter stringFromDate:nowDate];
if([timeStr isEqualToString:nowDay]){
success([CountryModel mj_objectArrayWithKeyValuesArray:arr]);
return;
}
}
}
[xMyHttp URL:urlStr
method:@"GET" parameters:nil
success:^(NSURLSessionDataTask *task, id responseObject)
{
if ([responseObject[@"code"] intValue] == HTTP_SUCCESS) {
// 存到配置
[UserDefaults setValue:responseObject[@"countrys"] forKey:kCountryCodeListKey];
//存当前获取的时间
NSDateFormatter *formatter = [NSDateFormatter new];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSDate *nowDate = [NSDate date];
NSString *nowDay = [formatter stringFromDate:nowDate];
[UserDefaults setValue:nowDay forKey:kgetCountryListTime];
NSMutableArray *arr = [CountryModel mj_objectArrayWithKeyValuesArray:responseObject[@"countrys"]];
success(arr);
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if (failure) {
failure(error);
}
}];
}
+ (NSString *)getFirstLetterFromString:(NSString*)country{
NSMutableString *mStr = [NSMutableString stringWithString:country];
// 将中文转换成带声调的拼音
CFStringTransform((__bridge CFMutableStringRef)mStr, NULL, kCFStringTransformToLatin, NO);
// 去掉声调 (忽略声调)(用此方法大大提高遍历的速度)
NSString * pinyinString = [mStr stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:[NSLocale currentLocale]];
// 将拼音首字母装换成大写
pinyinString = [pinyinString uppercaseString];
NSString *firstStr = [pinyinString substringWithRange:NSMakeRange(0, 1)];
NSString *regexA =@"^[A-Z]$";
NSPredicate *predA = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regexA];
return [predA evaluateWithObject:firstStr] ? firstStr : @"#";
}
/*
swfit
// MARK: - 获取首字母(传入汉字字符串, 返回大写拼音首字母)
class func getFirstLetterFromString(aString: String) -> (String) {
// 注意,这里一定要转换成可变字符串
let mutableString = NSMutableString.init(string: aString)
// 将中文转换成带声调的拼音
CFStringTransform(mutableString as CFMutableString, nil, kCFStringTransformToLatin, false)
// 去掉声调(用此方法大大提高遍历的速度)
let pinyinString = mutableString.folding(options: String.CompareOptions.diacriticInsensitive, locale: NSLocale.current)
// 将拼音首字母装换成大写
let strPinYin = pinyinString.uppercased()
// 截取大写首字母
let firstString = strPinYin.substring(to: strPinYin.index(strPinYin.startIndex, offsetBy:1))
// 判断首位是否为大写字母
let regexA = "^[A-Z]$"
let predA = NSPredicate.init(format: "SELF MATCHES %@", regexA)
return predA.evaluate(with: firstString) ? firstString : "#"
}*/
@end