gpt4 book ai didi

ios - NSMutableArray insertObject :atIndex causing crashes

转载 作者:行者123 更新时间:2023-11-28 22:52:23 24 4
gpt4 key购买 nike

我正在为 iPad 开发幻灯片创建应用程序,当我让用户重新排列幻灯片时遇到了一些麻烦。当调用 slideDropped 方法时,如果我只是删除幻灯片然后将其添加到幻灯片放映的末尾,那么一切正常。如果我将 5 张幻灯片中的第 2 张幻灯片移动到第 3 张幻灯片,然后当我尝试循环遍历 slideshow.slides(这只是一个 NSMutableArray)时,它就会在新幻灯片上出现错误访问错误而崩溃。

-(void)slideDropped:(Slide *)slide
{
int destIndex = (int)(slide.frame.origin.x) / 152;
destIndex = MIN(destIndex, slideshow.slides.count - 1);
destIndex = MAX(0, destIndex);
int sourceIndex = [slideshow.slides indexOfObject:slide];
[slideshow removeSlideAtIndex:sourceIndex];
if(destIndex >= slideshow.slides.count)
[slideshow addSlide:slide];
else
[slideshow insertSlide:slide atIndex:destIndex];
[self arrangeSlides];
}

-(void)arrangeSlides
{
[scrSlideshow.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
int x = 0;
NSLog(@"There are %i slides",slideshow.slides.count);
for(Slide* slide in slideshow.slides)
{
NSLog(@"%@",slide);//crashes here on the new slide if I insert at a specific index rather than appending the slide to the end
CGRect frame = CGRectMake(x++ * 152, 21, 150, 150);
slide.frame = frame;
[scrSlideshow addSubview:slide];
}
[scrSlideshow setContentSize:CGSizeMake(slideshow.slides.count * 152, 150)];
}

//in the Slideshow class

-(void)addSlide:(Slide *)slide
{
[slides addObject:slide];
}

-(void)removeSlideAtIndex:(int)index
{
[slides removeObjectAtIndex:index];
}

-(void)insertSlide:(Slide *)slide atIndex:(int)index
{
[slides insertObject:slides atIndex:index];
}

关于我可能做错了什么有什么想法吗?我怀疑我没有正确管理幻灯片,但我不确定该怎么做。

最佳答案

查看该代码的底部。您有 insertObject:slides。这应该只是“幻灯片”还是您打算将其设为复数?那可能是你的问题

关于ios - NSMutableArray insertObject :atIndex causing crashes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11621583/

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