Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSMutableArray- removeObject results with removing object and a nil element

Tags:

objective-c

Firstly, I am new with Objective C.

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?

Here is my code:

in h file

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

in m file

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

}

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;

and here are some screen bumps

This is before removing first object

enter image description here

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

enter image description here

like image 978
Furedal Avatar asked Feb 15 '26 01:02

Furedal


1 Answers

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;

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.

like image 102
Eli Ganem Avatar answered Feb 21 '26 14:02

Eli Ganem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!