当deference子类NSObject时,它会崩溃

时间:2014-02-18 09:42:17

标签: ios iphone nsobject

@interface PADiscover : NSObject

@property (nonatomic, assign) unsigned int dw3C;
@property (nonatomic, assign) BOOL isSet;
@property (nonatomic, assign) PAContactModel model;

@end

在另一个VC中

@property (nonatomic, strong) NSMutableDictionary *shakeDict;

然后我添加了一些对象shakeDict

- (void)viewDidLoad {
            NSNumber *num = [NSNumber numberWithUnsignedInt:message.sDeviceInfo.dw3CId];
            PADiscover *discover = [[PADiscover alloc] init];
            discover.dw3C = message.sDeviceInfo.dw3CId;
            discover.model = message.sDeviceInfo.dwDeviceType;
            discover.isSet = message.sDeviceInfo.fgPasswdFlag;

            [_shakeDict setObject:discover forKey:[NSString stringWithFormat:@"%@", num]];

            [_shakeTV reloadData];
 }


  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
}
PADiscover *discover = [[_shakeDict allKeys] objectAtIndex:indexPath.row];


**cell.textLabel.text = [NSString stringWithFormat:@"ID %u, ISSet %d", discover.dw3C, discover.isSet];//crashed!**
cell.detailTextLabel.text = [NSString stringWithFormat:@"model %d", discover.model];
cell.detailTextLabel.font = [UIFont boldSystemFontOfSize:11];

return cell;
}

似乎不能成功地尊重 PADiscover ,但我不确定何时会出错。 任何人都可以为我解释一下吗?

2 个答案:

答案 0 :(得分:0)

试试这个,

NSString *key = [[_shakeDict allKeys] objectAtIndex:indexPath.row];
PADiscover *discover = [_shakeDict objectForKey:key];

而不是

PADiscover *discover = (PADiscover *)[[_shakeDict allKeys] objectAtIndex:indexPath.row];

答案 1 :(得分:0)

至少这一行可能是错误:

@property (nonatomic, assign) PAContactModel model;

如果是模型类,则更改为strong。

@property (nonatomic, strong) PAContactModel model;