Cart implimentation for eCommerce/Shopping app
I have to make a cart for my app, but I don't know the best way to implement it.
I'm thinking about using NSMutableArray
, but I am no开发者_C百科t sure if it's the best way to to this.
Any suggestions?
You can try an NSMutableOrderedSet.
Here's what they say in the AppleDocumentation:
"You can use ordered sets as an alternative to arrays when the order of elements is important and performance in testing whether an object is contained in the set is a consideration— testing for membership of an array is slower than testing for membership of a set."
Checking if a certain product is already in the cart can be difficult and inefficient - using an NSMutableOrderedSet saves you that trouble. It's much like an NSMutableArray but more efficient for this function.
Although it depends on the size of your cart, arrays are not the best option if you intend to effectively search/fetch the cart contents through ID tag or something. A NSDictionary would be a better option.
精彩评论