在写项目的时候,调用下面的方法时
- (void)fetchOwnedStockSocketData
{
[self.ownedStockDataArray enumerateObjectsUsingBlock:^(DNUserOptionalListModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[self.secket dn_establishConnectionWithParams:obj.prod_code subscribeType:DNSocketMsgSendSubscribeTypeSubscribe successOnMainThread:^(NSDictionary * _Nullable data) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:idx inSection:0];
DNUserOptionalListModel *userOptionalEntity = self.ownedStockDataArray[indexPath.row];
if ([userOptionalEntity.last_px floatValue] != [data[@"last_px"] floatValue]) {
userOptionalEntity.last_px = data[@"last_px”];
userOptionalEntity.px_change_rate = data[@"px_change_rate”];
DLog(@"prod_code=====%@",data[@"prod_code”]);
if ([obj.prod_code isEqualToString:data[@"prod_code"]] && self.searchController.active == NO) {
[self.ownedStocktableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
}
}];
}];
}
崩溃日志如下:
111.png
。
其实这是数组越界造成的:解决方案如下
- (void)fetchOwnedStockSocketData
{
[self.ownedStockDataArray enumerateObjectsUsingBlock:^(DNUserOptionalListModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
[self.secket dn_establishConnectionWithParams:obj.prod_code subscribeType:DNSocketMsgSendSubscribeTypeSubscribe successOnMainThread:^(NSDictionary * _Nullable data) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:idx inSection:0];
if (indexPath.row <= self.ownedStockDataArray.count-1) {
DNUserOptionalListModel *userOptionalEntity = self.ownedStockDataArray[indexPath.row];
if ([userOptionalEntity.last_px floatValue] != [data[@"last_px"] floatValue]) {
userOptionalEntity.last_px = data[@"last_px"];
userOptionalEntity.px_change_rate = data[@"px_change_rate"];
DLog(@"prod_code=====%@",data[@"prod_code"]);
if ([obj.prod_code isEqualToString:data[@"prod_code"]] && self.searchController.active == NO) {
[self.ownedStocktableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
}
}
}];
}];
}
增加了防止数组越界的判断,这句代码
if (indexPath.row < self.ownedStockDataArray.count) {
}
网友评论