Can I write code like the following? It seems to work, but I want to make sure that it is allowed/safe:
// menu is a NSMenu*
for (NSMenuItem *item in [menu itemArray]) {
if (some condition) {
[menu removeItem:item];
}
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If it doesn’t immediately throw an exception (as modifying an array directly when enumerating it does) then it’s probably okay.
NSMenumay be giving you a copy of its internal array (assuming it’s keeping its items in anNSArray).For guaranteed safety, however, you might like to do this in two steps. Create another array to hold the items for removal, then enumerate that and do the removal: