好久没上博客了,说明自己确实懒啦、、最近又刚好转做ios开发,碎片的知识点着实另我头疼,好多记不住。所以就想到了写篇博客整理下吧。主要是供自己参考的,当然,如果能帮助到他人,我也十分荣幸。
1.NSData与base64相互转换:
NSData转base64:
NSData *icon;
[icon base64EncodedStringWithOptions:0];
base64转NSData:
NSString *dataString;
[[NSData alloc]initWithBase64EncodedString:dataString options:0];
2.json与NSArray或NSDictionary相互转换:
json转NSArray或NSDictionary:
NSString *json;
NSData *jsonData=[json dataUsingEncoding:NSUTF8StringEncoding];
id jsonObject=[NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error];
NSArray或NSDictionary转json:
NSMutableDictionary *projectDic;
NSData *data=[NSJSONSerialization dataWithJSONObject:projectDic options:NSJSONWritingPrettyPrinted error:&error];
NSString *json=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
3.移除一个控件上的所有子控件:
[self.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
4.ios中的timer:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//do something
});
多久时间后执行代码。这里想到了ios有点让我费解的东西。有时候你刚创建出Controller就去对界面进行操作得不到预想的结果,貌似是界面还没完全加载完成,这时候着实蛋疼,就可以用这个方法,我也是遇到这种情况才遇到这个方法的。
5.整个界面关闭键盘事件:
[self.view endEditing:NO];
6.键盘关闭或弹出触发
notification,以便处理事件:
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(showKeyBoard) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hideKeyBoard) name:UIKeyboardWillHideNotification object:nil];
[self.navigationController pushViewController:sortViewsController animated:YES];
[self.navigationController popViewControllerAnimated:YES];
8.在当前页面弹出界面和关闭界面:
[self presentViewController:navController animated:YES completion:nil];
[self dismissViewControllerAnimated:YES completion:nil];
9.设置button点击高亮:
_colorBtn.showsTouchWhenHighlighted=YES;
10.view的tag属性有秒用,关键时候可以绑定数据,方便开发。
11.UIColor对象转换为NSString方便存储:
UIColor *color= view.backgroundColor;
NSData *colorData= [NSKeyedArchiver archivedDataWithRootObject:color];
NSString *colorString= [colorData base64EncodedStringWithOptions:0];
从NSString恢复UIColor对象:
NSData *colorData=[[NSData alloc]initWithBase64EncodedString:colorString options:0];
UIColor *color = [NSKeyedUnarchiver unarchiveObjectWithData:colorData];
目前大致就这些碎知识点,后续会更新一些关于Tableview和collectionview方面的。但是不是系统讲解的。