NSMutableArray- removeObject的结果是删除对象和一个nil元素 [英] NSMutableArray- removeObject results with removing object and a nil element

查看:98
本文介绍了NSMutableArray- removeObject的结果是删除对象和一个nil元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我对Objective C还是陌生的.

Firstly, I am new with Objective C.

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

I have my class Song that has a pair of attributes. In my main class i got a variable allSongs that is a NSMutableArray and in this array have I added all my song-objects. My problem comes when trying to call [self.allSongs removeObject:OBJECT]; Using the debugger, I can see that before the call, the list looks as expected. But after the call it will result that the targeted object will be removed but also the first element in the array will turn to nil. Is this a common pointer problem or what?

这是我的代码:

在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元素.

last line of this code is were i get an error, as the mutableArray has a couple of nil elements.

- (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;

这是一些屏幕凸起

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

This is before removing first object

这是在删除第一个对象之后.您看到一个元素如何如预期般消失,而另一个元素设置为零?

This is after the first object was removed. You see how one element disapeared as expected and the other is set too nil?

推荐答案

有时,变量视图"显示不正确的值. 数组不能包含nil值,因此绝对是错误值的情况. 您声明您的应用程序在此行崩溃:

Sometimes the Variables View shows incorrect values. An array can't contain nil values, so this is definitely a case where the values are wrong. You state that your application crashes on this line:

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

我的猜测是self.songs [indexPath.row]根本没有称为page的属性. 尝试用以下代码替换此行:

My guess is that self.songs[indexPath.row] simply doesn't have a property called page. Try to replace this line with this code:

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

它将帮助您查明问题.祝你好运.

It will help you pin-point the problem. Good luck.

这篇关于NSMutableArray- removeObject的结果是删除对象和一个nil元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆