Note that there are some explanatory texts on larger screens.

plurals
  1. POMutual changes in two NSMutableArray objects
    text
    copied!<p>Assume, your .h file looks like this:</p> <pre><code>@property (retain, nonatomic) NSMutableArray *songs; - (NSMutableArray *)popularSongs; - (void)make20PercentDiscountToPopularSongs </code></pre> <p>Your .m file looks like this:</p> <pre><code>- (NSMutableArray *)popularSongs { NSMutableArray *popularSongs = [NSMutableArray array]; for (Song *song in self.songs) { if (song.isPopular) { [popularSongs addObject:song]; } } return popularSongs; } - (void)make20PercentDiscountToPopularSongs { for (Song *song in self.popularSongs) { song.price = song.price * 0.8; } } </code></pre> <p>The code above adds a 20% discount to popular songs. I recognise that there is a more simplistic way of doing this. You could have the "make20PercentDiscountToPopularSongs" and "popularSongs" function in a single function but let's assume that the code is written the way it is written above.</p> <p>In the example above, will the line:</p> <pre><code>song.price = song.price * 0.8; </code></pre> <p>Actually, make any changes to the object:</p> <pre><code>@property (retain, nonatomic) NSMutableArray *songs; </code></pre> <p>Or not? Because, it seems as if the line will change the newly created popularSongs NSMutableArray rather than the songs NSMutableArray, am I right? This problem is bothering me for a lot of time now. <strong>I would like to make changes just to the original songs array. To me popularSongs works as if you are storing the pointers to the songs in the songs array.</strong></p>
 

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