gpt4 book ai didi

iphone - 如何交换 NSMutableArray 中的值?

转载 作者:太空狗 更新时间:2023-10-30 03:27:10 24 4
gpt4 key购买 nike

我的这段代码段错误,知道为什么吗? allButtons 是一个NSMutableArray,它包含3个对象,a=0, b=1,a和b都是int 输入

 if(a != -1 && b!= -1){
//Swap index in "allButtons"
id tempA = [allButtons objectAtIndex:a];
id tempB = [allButtons objectAtIndex:b];
[allButtons replaceObjectAtIndex:a withObject:tempB]; //Seg fault here?????
[allButtons replaceObjectAtIndex:b withObject:tempA];
needLoad = false;
[self setUpButtons];
}

编辑:

 NSMutableArray *allButtons = //fetch the array from Coredata. This work since I display the data onto the screen, plus, [allButtons count] return 3, and a=0, b=1
f(a != -1 && b!= -1){
//Swap index in "allButtons"
[allButtons exchangeObjectAtIndex:a withObjectAtIndex:b];
needLoad = false;
[self setUpButtons];
}

最佳答案

第一次调用 replaceObjectAtIndex: 将释放旧对象 (tempA),但这不应导致段错误。正如@Zoran 提到的,尝试记录 tempA 的 retainCount 并验证其计数。

同样为了交换数组中的元素,你应该使用 exchangeObjectAtIndex:withObjectAtIndex而不是 replaceObjectAtIndex:withObject。 iPhone 2.0 支持它。

关于iphone - 如何交换 NSMutableArray 中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2590037/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com