Monday, December 30, 2013

Interface design issues discussed on the NSMutableArray

In Objective C development, should be used NSMutableArray, temporarily understood as an array of variable length bar.
This stuff provides addObject method also provides a removeObject methods.

Problem Description:
In programming, I will add a set of objects pointer to array, the array now has the ownership of these objects.

Now I removeObject delete an item, according to the interface design removeObject no return value, so I do not know whether the array have this one, do not know whether to delete a success.

Under this premise then, more terrible things happened, which one you want to delete the current object ownership how do I determine it? I did not know whether he is re-array, do not know whether he has been deleted.

Question:
What method can determine the attribution of ownership again, both in the finish after the delete operation to determine whether it can certainly remove operation succeeded.

I think several solutions:

One is not removeObject method for cyclic query directly compare the contents of the array you want to delete entries get deleted index values ​​with removeObjectAtIndex. Such owned by the caller, release out reasonable. However, this method is difficult to see. (NSMutableArray does not provide query methods, only for the cycle, this can be understood because it can not guarantee an orderly array, there is no faster than for a query method)

The second option, or use removeObject method compares array array size before and after the call to see if there are changes in the instructions to remove one less successful, this approach is two, not thread safe. But the code is simpler than the first method.

Do not know if I was wrong, experienced friend there is no better solution. Thank you.
Reply:
removeObject certainly be successful unless the index is out of range.
Reply:
Oh, I was not clear, removeObject parameters may not be an array of items.

Also think of a solution:
 
- (Void) removeCard: (AddressCard *) card
{
NSLog (@ "AddressBook removeCard!!");

NSUInteger index = [addressArray indexOfObject: card];
if (index! = NSNotFound) {
id temp = [addressArray objectAtIndex: index];
[AddressArray removeObjectAtIndex: index];

[Temp release];
}
}


I think this tricky number.
Reply:
NSMutableArray methods have indexOfObject
Reply:
reference to the second floor biosli reply:
Oh, I was not clear, removeObject parameters may not be an array of items.

Also think of a solution:
C / C + + code

- (Void) removeCard: (AddressCard *) card
{
NSLog (@ "AddressBook removeCard!!");

NSUInteger index = [addressArray indexOfObjec ......

The above code has a problem, the array obj will automatically send a release message is deleted, you then [temp releas], will lead to memory errors
Reply:
After testing, I think LS was not right, if you do not call the temp release program does not call my overloaded dealloc function (in which I played the log does not call), meaning that there will be wild pointer.
Reply:
Alive, really, oh, Array remove when the reference count will be reduced one, but when it should correspond to add a plus so it is balanced ~ ~ ah ~ ~
Reply:
The reply was deleted at 2011-03-31 09:59:58 moderator

Reply:
Just a point of personal recommendations.

The landlord is also the feeling from other development languages ​​into Object-C, so habitually will first confirm the presence of the old data before deletion.

I think the message is Object-C automatic mechanism to ignore the failure, there is no need to care about the deletion failure.

In other words, identify and remove from the array elements are realized threw NSMutableArray do. While it is true to the internal processing of abnormal operating functions are lost.

But Apple's money so dark, if NSMutableArray not delete the data of the deleted words, when the point is not to close the compensation fee can do?

Reply:
Really was upstairs said, I really developed over from other languages.

I used to figure out the underlying ⋯ ⋯ do not know if I have the book in-depth explanation of cocoa and objective-c?
Reply:
reference to the 8th floor czl21 reply:
just point a personal recommendation.

The landlord is also the feeling from other development languages ​​into Object-C, so habitually will first confirm the presence of the old data before deletion.

I think the message is Object-C automatic mechanism to ignore the failure, there is no need to care about the deletion failure.

In other words, identify and remove from the array elements are realized threw NSMutableArray do. While it is true to the internal processing of abnormal operating functions are lost.

But Apple's money so dark, if NSMutableArray did not put the ......

Learn from other languages ​​turn this really easy to follow the old way of thinking to the problem.

No comments:

Post a Comment