Note that there are some explanatory texts on larger screens.

plurals
  1. PONSMutableArray- removeObject results with removing object and a nil element
    primarykey
    data
    text
    <p>Firstly, I am new with Objective C.</p> <p>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?</p> <p>Here is my code:</p> <p>in h file</p> <pre><code>@property (nonatomic, strong) NSMutableArray *songs; @property (nonatomic, strong) NSMutableArray *allChapters; </code></pre> <p>in m file</p> <pre><code>- (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 &lt; 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 &lt; files.count; i++) { filePath = [[NSBundle mainBundle] pathForResource:files[i] ofType:@"txt"]; if(filePath){ NSError *error; NSString *textFromfile = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error: &amp;error]; /* index 0 for page number 1 for title name 2 for melody name 3 -&gt; 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 &lt; 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]]; </code></pre> <p>}</p> <pre><code>-(void)addChapters{ for(int i = 0; i &lt; 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]]; </code></pre> <p>}</p> <pre><code>-(void)removeChaptersExcept:(Song *) chapter{ for(int i = 0; i &lt; self.allChapters.count; i++){ if(self.allChapters[i] != chapter &amp;&amp; [self.songs containsObject:self.allChapters[i]]) [self.songs removeObject:self.allChapters[i]]; } </code></pre> <p>}</p> <p>last line of this code is were i get an error, as the mutableArray has a couple of nil elements.</p> <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if([self.chosenChapter isEqualToString:@"Alla"]){ [self addChapters]; } else{ Song *chapter = nil; for(int i = 0; i &lt; 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; </code></pre> <p>and here are some screen bumps</p> <p>This is before removing first object</p> <p><img src="https://i.stack.imgur.com/EN9t0.png" alt="enter image description here"></p> <p>This is after the first object was removed. You see how one element disapeared as expected and the other is set too nil?</p> <p><img src="https://i.stack.imgur.com/snAuI.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload