iOS:初始值、零值、空值(二)--方法及程序行
2014-11-08 17:16
225 查看
测试环境:xcode 6.1 + ios sdk 8.0;
(2)字典:
a.下列俩个方法,如果x为0(某个为零,或者俩者同时为零),程序不会crash,只是tableview界面显示没有cell,
1。
(1)UIPickerView:
a.下列方法为必须实现的俩个方法,x可以0(某个为0,同时为0都行)。
数组等集合
(1)arr如果为nil,则[arr count]为0,程序不会崩溃.【场景:在某些sourceDelegate里面返回[arr count ]可能为零】。对nil发送消息,不会崩溃。(2)字典:
NSDictionary *dic; [dic setValue:@(0) forKey:@"key"]; if (nil == [dic objectForKey:@"key"]) { NSLog(@"message sent to nil no effect"); } NSDictionary *dic1; dic1 = [NSDictionary dictionary]; //[dic1 setValue:@(1) forKey:@"key1"]; NSLog(@"not mutable dic set for key crash"); NSMutableDictionary *dic2; dic2 = [NSMutableDictionary dictionary]; [dic2 setValue:@(2) forKey:@"key2"]; if (nil == [dic2 objectForKey:@"noExistKey"]) { NSLog(@"objectForKey for noExistKey is nil"); } NSMutableDictionary *dic3; dic3 = [NSMutableDictionary dictionary]; [dic3 setValue:nil forKey:@"key3"]; // 以前版本会crash if (nil == [dic3 objectForKey:@"key3"]) { NSLog(@"set nil for key not crash"); }
2014-11-08 14:51:59.218 AlwaysTest[42279:1425099] message sent to nil no effect 2014-11-08 14:51:59.220 AlwaysTest[42279:1425099] not mutable dic set for key crash 2014-11-08 14:51:59.220 AlwaysTest[42279:1425099] objectForKey for noExistKey is nil 2014-11-08 14:51:59.220 AlwaysTest[42279:1425099] set nil for key not crash
控件代理方法
(1)UITableView:a.下列俩个方法,如果x为0(某个为零,或者俩者同时为零),程序不会crash,只是tableview界面显示没有cell,
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return x; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return x; }b.下列方法如果return为nil,程序crash
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ return x; }备注:后俩个方法为protocol中required方法,必须实现,第一个方法默认返回值为
1。
(1)UIPickerView:
a.下列方法为必须实现的俩个方法,x可以0(某个为0,同时为0都行)。
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return x; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ return x; }备注:pickerView:titleForRow:forComponent:为optional方法。
控件
(1)tableview的删除cell方法动画【场景:本地存储一些数据,使用时直接展示本地的,但要在后台下载数据,再跟本地对比维护,如果新增则add cell,减少则del cell当】// 情况一:删除row,数组参数为nil没事 [self.tableview beginUpdates]; [self.tableview deleteRowsAtIndexPaths:nil withRowAnimation:UITableViewRowAnimationFade]; [self.tableview endUpdates]; // 情况二:删除section,NSIndxSet参数为nil,程序crash [self.tableview beginUpdates]; [self.tableview deleteSections:nil withRowAnimation:UITableViewRowAnimationFade]; [self.tableview endUpdates]; // 情况三:删除row,数组参数count 为0没事 [self.tableview beginUpdates]; NSArray *array = [NSArray array]; [self.tableview deleteRowsAtIndexPaths:array withRowAnimation:UITableViewRowAnimationFade]; [self.tableview endUpdates]; // 情况四:删除section,NSIndxSet参数集合count 为0 没事 [self.tableview beginUpdates]; NSIndexSet *set; set = [NSIndexSet indexSet]; [self.tableview deleteSections:set withRowAnimation:UITableViewRowAnimationFade]; [self.tableview endUpdates]; //总结:不管是删除row,section,参数为集合类型,如果参数为nil,则可能出问题,但集合存在,里面没有元素(count为0)则没事
相关文章推荐
- iOS:初始值、零值、空值(一)--变量及判断
- iOS 4中禁止程序退出后保留在后台的方法
- ios或mac程序中添加链接到评价页面的方法
- iOS程序开发引用的第三方库之间出现duplicate symbol时的处理方法
- ios程序無法選擇真機或模擬器解決方法
- ios 判断app程序第一次启动方法
- 程序的初始最大化和防止启动打开空文档的方法
- iOS判断程序第一次启动的方法
- iOS/调试器和程序崩溃时的一些处理方法
- ios:程序打包时出现The identity 'iPhone Distribution:XXX doesn't match any valid的解决方法
- iOS程序开发对iPhone和iPad兼容性的规划方法
- iOS 4程序直接退出不在后台运行的方法
- ios 判断app程序第一次启动方法
- ios 程序进入后台并返回 调用的方法
- IOS 程序内部切换语言 的一种方法
- iOS程序开发引用的第三方库之间出现duplicate symbol时的处理方法
- 把IOS测试程序部署给其它设备的方法
- iOS程序开发引用的第三方库之间出现duplicate symbol时的处理方法
- IOS 程序内部切换语言 的一种方法
- IOS拨打完电话回到原程序的方法