I have this simple method to put the first element of an array at the end and move everything down by one index:
-(NSArray*)backUpNotes:(NSArray*)notes {
NSMutableArray* newNotes = [NSMutableArray arrayWithArray:notes];
Note* note = [newNotes objectAtIndex:0];
[newNotes removeObjectAtIndex:0];
[newNotes addObject:note];
return (NSArray*)newNotes;
}
the array notes contains two Note* objects, Note A and Note B. after the line
Note* note = [newNotes objectAtIndex:0];
note contains Note A -- as expected. After the line
[newNotes removeObjectAtIndex:0];
newNotes ONLY contains Note A --- This is NOT expected. Note A is at index 0, and I can see that from the debugger. If I instead do
[newNotes removeObjectAtIndex:1];
newNotes still ONLY contains Note A -- That is expected, since I'm removing note B in that case. It seems to me that I cannot for the life of me remove Note A from this array. I even tried doing:
[newNotes removeObject:note];
and still having newNotes containing ONLY note A -- definitely unexpected.
Any insight would be amazing.
Try this:
NSMutableArray* newNotes = [NSMutableArray arrayWithArray:notes];
for (Note *n in newNotes) {
if ([n isEqual:note]) {
[newNotes removeObject:n];
break;
}
}
Or:
int x = 0;
NSMutableArray* newNotes = [NSMutableArray arrayWithArray:notes];
for (Note *n in newNotes) {
if ([n isEqual:note]) {
break;
}
++x;
}
[newNotes removeObjectAtIndex:x];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.