When is it better to use an NSSet over an NSArray?

前端 未结 11 1554
-上瘾入骨i
-上瘾入骨i 2020-12-12 10:27

I have used NSSets many times in my apps, but I have never created one myself.

When is it better to use an NSSet as opposed to an NSArray and why?

相关标签:
11条回答
  • 2020-12-12 10:49

    Here you can find a pretty thorough comparison of the NSArray and NSSet datastructures.

    Short conclusions:

    Yes, NSArray is faster than NSSet for simply holding and iterating. As little as 50% faster for constructing and as much as 500% faster for iterating. Lesson: if you only need to iterate contents, don't use an NSSet.

    Of course, if you need to test for inclusion, work hard to avoid NSArray. Even if you need both iteration and inclusion testing, you should probably still choose an NSSet. If you need to keep your collection ordered and also test for inclusion, then you should consider keeping two collections (an NSArray and an NSSet), each containing the same objects.

    NSDictionary is slower to construct than NSMapTable — since it needs to copy the key data. It makes up for this by being faster to lookup. Of course, the two have different capabilities so most of the time, this determination should be made on other factors.

    0 讨论(0)
  • 2020-12-12 10:50

    The best answer is to this is Apple's own documentation.

    enter image description here

    The main difference is that NSArray is for an ordered collection and NSSet is for an unordered collection.

    There are several articles out there that talk about the difference in speed between the two, like this one. If you're iterating through an unordered collection, NSSet is great. However, in many cases, you need to do things that only an NSArray can do, so you sacrifice the speed for those abilities.

    NSSet

    • Primarily access items by comparison
    • Unordered
    • Does not allow duplicates

    NSArray

    • Can access items by index
    • Ordered
    • Allows duplicates

    That's all there really is to it! Let me know if that helps.

    0 讨论(0)
  • 2020-12-12 10:52

    The image from Apple's Documentation describes it very well:

    Objective-C Collections

    Array is an ordered (order is maintained when you add) sequence of elements

    [array addObject:@1];
    [array addObject:@2];
    [array addObject:@3];
    [array addObject:@4];
    [array addObject:@6];
    [array addObject:@4];
    [array addObject:@1];
    [array addObject:@2];
    
    [1, 2, 3, 4, 6, 4, 1, 2]
    

    Set is a distinct (no duplicates), unordered list of elements

    [set addObject:@1];
    [set addObject:@2];
    [set addObject:@3];
    [set addObject:@4];
    [set addObject:@6];
    [set addObject:@4];
    [set addObject:@1];
    [set addObject:@2];
    
    [1, 2, 6, 4, 3]
    
    0 讨论(0)
  • 2020-12-12 10:54

    NSArray:

    1. Ordered collection of data
    2. Allows duplicates
    3. It is collection type object

    NSSet:

    1. Unordered collection of data
    2. Does not allow duplicates
    3. It is also collection type object
    0 讨论(0)
  • 2020-12-12 10:55
    NSArray *Arr;
    NSSet *Nset;
    
    Arr=[NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"2",@"1", nil];
    Nset=[NSSet setWithObjects:@"1",@"2",@"3",@"3",@"5",@"5", nil];
    
    NSLog(@"%@",Arr);
    NSLog(@"%@",Nset);
    

    the array

    2015-12-04 11:05:40.935 [598:15730] ( 1, 2, 3, 4, 2, 1 )

    the set

    2015-12-04 11:05:43.362 [598:15730] { ( 3, 1, 2, 5 )}

    0 讨论(0)
  • 2020-12-12 10:58

    An array is used to access items by their index. Any item can be inserted into the array multiple times. Arrays mantain the order of their elements.

    A set is used basically only to check if the item is in the collection or not. The items have no concept of order or indexing. You cannot have an item in a set twice.

    If an array wants to check if it contains an element, it has to check all its items. Sets are designed to use faster algorithms.

    You can imagine a set like a dictionary without values.

    Note that array and set are not the only data structures. There are other, e.g. Queue, Stack, Heap, Fibonacci's Heap. I would recommend reading a book about algorithms and data structures.

    See wikipedia for more information.

    0 讨论(0)
提交回复
热议问题