您的位置:首页 > 移动开发 > IOS开发

iOS:初始值、零值、空值(二)--方法及程序行

2014-11-08 17:16 225 查看
测试环境:xcode 6.1 + ios sdk 8.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)则没事
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: