修改一个 属性 的随机 NSPoint 并将新版本放回 NSMutableArray
Modify a property of one random NSPoint and put the new version back in an NSMutableArray
我有一个 NSMutableArray,我正在添加随机数的 CGPoint,最终将使用标准 [array addObject:[NSValue valueWithCGPoint:point] 生成 UIBezierPath;我需要从该数组访问一个随机元素并仅修改该元素的 y 属性 并将其放回从中拉出的同一随机点。我的问题是找到将修改后的元素放回的随机点。下面的逻辑在运行时崩溃,出现 "index out of range" NSRangeException 异常。这对我来说很有意义,我认为指针指向一个完全不同的内存地址,该地址远远超出有序数组的范围,但我没有看到如何将新修改的元素同时随机返回到数组中spot if 被拉出来了。以下是我失败的尝试...
NSValue *randomPoint = [self.midPointsArray objectAtIndex:arc4random() % [self.midPoints2 count]];
CGPoint newPoint = [randomPoint CGPointValue];
newPoint.y = newPoint.y + 20;
[self.midPointsArray replaceObjectAtIndex:randomPoint withObject:[NSValue valueWithCGPoint:newPoint]];
只保存随机索引:
int randomIndex = arc4random() % [self.midPoints2 count];
NSValue *randomPoint = [self.midPointsArray objectAtIndex:randomIndex];
CGPoint newPoint = [randomPoint CGPointValue];
newPoint.y = newPoint.y + 20;
[self.midPointsArray replaceObjectAtIndex:randomIndex withObject:[NSValue valueWithCGPoint:newPoint]];
我有一个 NSMutableArray,我正在添加随机数的 CGPoint,最终将使用标准 [array addObject:[NSValue valueWithCGPoint:point] 生成 UIBezierPath;我需要从该数组访问一个随机元素并仅修改该元素的 y 属性 并将其放回从中拉出的同一随机点。我的问题是找到将修改后的元素放回的随机点。下面的逻辑在运行时崩溃,出现 "index out of range" NSRangeException 异常。这对我来说很有意义,我认为指针指向一个完全不同的内存地址,该地址远远超出有序数组的范围,但我没有看到如何将新修改的元素同时随机返回到数组中spot if 被拉出来了。以下是我失败的尝试...
NSValue *randomPoint = [self.midPointsArray objectAtIndex:arc4random() % [self.midPoints2 count]];
CGPoint newPoint = [randomPoint CGPointValue];
newPoint.y = newPoint.y + 20;
[self.midPointsArray replaceObjectAtIndex:randomPoint withObject:[NSValue valueWithCGPoint:newPoint]];
只保存随机索引:
int randomIndex = arc4random() % [self.midPoints2 count];
NSValue *randomPoint = [self.midPointsArray objectAtIndex:randomIndex];
CGPoint newPoint = [randomPoint CGPointValue];
newPoint.y = newPoint.y + 20;
[self.midPointsArray replaceObjectAtIndex:randomIndex withObject:[NSValue valueWithCGPoint:newPoint]];