iOS 开发中可能遇到的转换方法

测试推送 工具 Easy APNs Provider

1.字符串拼接的三种

//1.截取字符串

NSString *string =@"123456d890";
NSString *str1 = [string substringToIndex:5];//截取掉下标5之前的字符串
NSLog(@"截取的值为:%@",str1);
 NSString *str2 = [string substringFromIndex:3];//截取掉下标3之后的字符串
NSLog(@"截取的值为:%@",str2);

//2.匹配字符串

NSString *string =@"sd是sfsfsAdfsdf";
NSRange range = [string rangeOfString:@"Ad"];//匹配得到的下标
NSLog(@"rang:%@",NSStringFromRange(range));
string = [string substringWithRange:range];//截取范围内的字符串
NSLog(@"截取的值为:%@",string);

//3.分隔字符串

NSString *string =@"sdfsfsfsAdfsdf";
    
NSArray *array = [string componentsSeparatedByString:@"A"]; //从字符A中分隔成2个元素的数组
NSLog(@"array:%@",array); //结果是adfsfsfs和dfsdf
2.iOS11 关于导航栏searchbar 的调整
    [[UISearchBar appearance] setSearchFieldBackgroundImage:[self searchFieldBackgroundImage] forState:UIControlStateNormal];
    UITextField *txfSearchField = [_searchChatRecords valueForKey:@"_searchField"];
    [txfSearchField setDefaultTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13.5]}];

调用方法

//调用方法
- (UIImage*)searchFieldBackgroundImage {
    UIColor*color = [UIColor whiteColor];
    CGFloat cornerRadius = 5;
    CGRect rect =CGRectMake(0,0,28,28);
    
    UIBezierPath*roundedRect = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius];
    roundedRect.lineWidth=0;
    
    UIGraphicsBeginImageContextWithOptions(rect.size,NO,0.0f);
    [color setFill];
    [roundedRect fill];
    [roundedRect stroke];
    [roundedRect addClip];
    
    UIImage *image =UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
3.关于时间转换成系统时区

北京时间与UTC时间相差8个小时,因此每次会转换成当前时间,这个方法是转换成系统时区的时间

- (NSTimeInterval)getNowDateWithDate:(NSDate *)date{
    NSTimeZone *zone = [NSTimeZone systemTimeZone]; // 获得系统的时区
    NSTimeInterval time = [zone secondsFromGMTForDate:date];// 以秒为单位返回当前时间与系统格林尼治时间的差
    NSDate *nowDate = [date dateByAddingTimeInterval:time];
    NSTimeInterval timeInterval = [nowDate timeIntervalSince1970];
    return timeInterval;
}
4.字符串UT-8编码 与解码

编码:
iOS9以前格式:

NSString*hStr =@"你好啊";

NSString*hString = [hStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSLog(@"hString === %@",hString);

iOS9 以后格式:

NSString*hString = [hStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

解码
iOS9之前:

NSString*str3 =@"\u5982\u4f55\u8054\u7cfb\u5ba2\u670d\u4eba\u5458\uff1f";

NSString*str5 = [str3 stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSLog(@"str5 ==== %@",str5);

iOS9之后

NSString*str5 = [str3 stringByRemovingPercentEncoding];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 第5章 引用类型(返回首页) 本章内容 使用对象 创建并操作数组 理解基本的JavaScript类型 使用基本类型...
    大学一百阅读 8,607评论 0 4
  • 二、数学运算(math)函数2.1算术运算函数2.1.1rand()函数:产生随机数2.1.2abs()函数/la...
    问题饿阅读 8,861评论 1 11
  • 书澈 黎星拿着手机站起来,红着眼眶对书澈说:“书澈,我黎星比你有种!至少我敢大声说出来我爱你!好过我们在一起那段时...
    清十四L阅读 3,627评论 0 0
  • 我还记得第一次和老祝说话是上完剪辑课,在教室门口,她穿的是格子衬衣,牛仔裙加一双运动鞋,绑着马尾。 当时心想这新来...
    Birdy鸟鸟阅读 2,688评论 0 0
  • 第一讲 中国艺术与文化史 1、西方绘画:状物形Representing the object 例:丢勒《哀愁》 西...
    书书童阅读 8,960评论 0 3

友情链接更多精彩内容