gpt4 book ai didi

objective-c - NSMutableArray- removeObject 结果删除对象和 nil 元素

转载 作者:搜寻专家 更新时间:2023-10-30 20:19:43 25 4
gpt4 key购买 nike

首先,我是 Objective C 的新手。

我的类 Song 有一对属性。在我的主类中,我得到了一个变量 allSongs,它是一个 NSMutableArray,在这个数组中我添加了我所有的歌曲对象。我的问题是在尝试调用 [self.allSongs removeObject:OBJECT] 时出现的;使用调试器,我可以看到在调用之前,列表看起来符合预期。但在调用之后,目标对象将被删除,而且数组中的第一个元素将变为 nil。这是一个常见的指针问题还是什么?

这是我的代码:

在h文件中

@property (nonatomic, strong) NSMutableArray *songs;
@property (nonatomic, strong) NSMutableArray *allChapters;

在m文件中

- (void)viewDidLoad
{

self.chosenChapter = [[NSString alloc]initWithFormat:self.chosenChapter];
self.allChapters = [[NSMutableArray alloc]init];

//Chapter names and chapter page range
chapters = [[NSArray alloc]initWithObjects:@"chapter1", @"chapter2", @"chapter3", nil];
chaptersRange = [[NSArray alloc]initWithObjects:@"25", @"51", @"88", nil];
//Filnames of every song
files = [[NSMutableArray alloc] initWithObjects:@"test", @"Feta_fransyskor", nil];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey: @"page" ascending: YES];
self.songs = [[NSMutableArray alloc]init];

for(int i = 0; i < chapters.count; i++){
Song *chapter = [[Song alloc]init];
[chapter setPage:(NSString *)self.chaptersRange[i]];
[chapter setTitle:(NSString *)self.chapters[i]];
[self.allChapters addObject:chapter];
[self.songs addObject:chapter];
}

NSString *filePath;
int i;
for (i = 0; i < files.count; i++) {
filePath = [[NSBundle mainBundle] pathForResource:files[i] ofType:@"txt"];
if(filePath){
NSError *error;
NSString *textFromfile = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error: &error];
/*
index
0 for page number
1 for title name
2 for melody name
3 -> for lyrics
*/

NSMutableArray *newLineseparatedText = (NSMutableArray *)[textFromfile componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];

if(newLineseparatedText){
Song *newSong = [[Song alloc]init];
[newSong setPage:newLineseparatedText[0]];
[newSong setTitle:newLineseparatedText[1]];
[newSong setMelody:newLineseparatedText[2]];

[newLineseparatedText removeObjectAtIndex:0]; //remove page number
[newLineseparatedText removeObjectAtIndex:0]; //remove title name
[newLineseparatedText removeObjectAtIndex:0]; //remove melody name

[newSong setLyric:[newLineseparatedText componentsJoinedByString:@"\n"]];

[songs addObject:newSong];
}
}
}

[self.songs sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

[super viewDidLoad];


}

-(void)addChapters{
for(int i = 0; i < self.allChapters.count; i++){
if([self.songs containsObject:self.allChapters[i]] == false)
[self.songs addObject:self.allChapters[i]];
}
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey: @"page" ascending: YES];
[self.songs sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

-(void)addChapters{
for(int i = 0; i < self.allChapters.count; i++){
if([self.songs containsObject:self.allChapters[i]] == false)
[self.songs addObject:self.allChapters[i]];
}
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey: @"page" ascending: YES];
[self.songs sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

-(void)removeChaptersExcept:(Song *) chapter{
for(int i = 0; i < self.allChapters.count; i++){
if(self.allChapters[i] != chapter && [self.songs containsObject:self.allChapters[i]])
[self.songs removeObject:self.allChapters[i]];
}

这段代码的最后一行是我得到一个错误,因为 mutableArray 有几个 nil 元素。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if([self.chosenChapter isEqualToString:@"Alla"]){
[self addChapters];
}
else{
Song *chapter = nil;
for(int i = 0; i < self.allChapters.count; i++){
if([((Song *)self.allChapters[i]).title isEqualToString:self.chosenChapter]){
chapter = self.allChapters[i];
break;
}

}
[self addChapters];
[self removeChaptersExcept:chapter];
}




NSString *cellIdentifier = nil;
UITableViewCell *cell = nil;

NSString *page = ((Song *)self.songs[indexPath.row]).page;

这里有一些屏幕颠簸

这是在删除第一个对象之前

enter image description here

这是在移除第一个对象之后。您看到一个元素如何按预期消失而另一个元素设置得太零了吗?

enter image description here

最佳答案

有时变量 View 会显示不正确的值。数组不能包含 nil 值,所以这绝对是值错误的情况。您声明您的应用程序在此行崩溃:

NSString *page = ((Song *)self.songs[indexPath.row]).page;

我的猜测是 self.songs[indexPath.row] 根本没有名为 page 的属性。尝试用这段代码替换这一行:

Song *s = self.songs[indexPath.row];
if (s == nil)
{
NSLog("Song is nil! How can that be?");
}
else
{
NSLog("Page is %@", s.page);
}

它将帮助您查明问题所在。祝你好运。

关于objective-c - NSMutableArray- removeObject 结果删除对象和 nil 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15460626/

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