Group Group Group Group Group Group Group Group Group

raywenderlich.com Forums

Collection Data Structures in Swift

Learn about the fundamental collection data structures in this tutorial: arrays, dictionaries and sets.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/1172-collection-data-structures-in-swift

Thank you for taking the time to create this awesome resource for those of us who are learning about data in iOS. I have gone through tutorials and built apps using these structures, but this tutorial provides a great context for it all. Thanks!

I’d like to point out to @brickm (and to this, otherwise, excellent site in general), that solely referring to iOS in comments or articles that also pertain to MacOS (tvOS, etc), does a disservice to the XCode community, in that searches for interesting articles by MacOS (tvOS,…) developers may be less successful, even when the content is perfectly useful in those devs’ contexts.
Clearly Swift’s data collection API is intended for MacOS as well as for iOS.
This also applies to the libraries of frameworks that are published, which are sometimes unnecessarily restricted to iOS (or unduly hard to convert, where such conversion should be relatively easy).
There is a danger that a separate developer community is created that produces MacOS agnostic code. Reminiscent of “embrace, extend, extinguish” of the MS days.
I realize that Apple’s more energetic drive to test new software concepts first for the mobile world, then tries to merge them into the MacOS look-and-feel (especially when the MacOS API is slightly different), is partly to blame for this trend.
So, please, let’s try not to go the “iOS-only” way and ignore MacOS.

I think 1 billion devices running iOS means that word is going to show up more.

1 Like

Hey brickm, I’m glad to hear that it was helpful!

@vanfruniken Luckily, Apple has done a great job creating Xcode, and allowing many skills developed for one platform to carry over to another.

The sample playground appears to have an access control problem so I can’t download. Click on the link and I get this:

    <Error>
<Code>AccessDenied</Code>
<Message>Access Denied</Message>
<RequestId>35E2F501C8B4A9FA</RequestId>
<HostId>
xsWjjUxWQK+et8o2c9J4VtDzyZqKoUSL35TRAIXTV/jeDhoJ+VWaHS+Zv9RN3NldoRH416Gbd3E=
</HostId>
</Error>

How can we get the sample playground?

Thanks,
Dave

@nivivon - even to me, it shows up the same error while downloading the Xcode project from given link https://koenig-media.raywenderlich.com/uploads/2015/12/DataStructures_Swift_3.zip

I am using NSHashTable to implement database to store simulated number of registered automobiles in California. As of December 2016, there are 25,244,537 registered automobiles. My program first generates a hashtable having half of this amount. It then searches for a particular plate number, say 5AAA333. The timing report is below:

2017-06-03 prompt> Generate 12622268 licence plates for the registered cars in California in 136.330 sec.
2017-06-03 prompt> Find out if there is a duplication in 0.000191987 sec.
2017-06-03 prompt> Database using hashTable does not contain object 5AAA333.

Here is the Objective-C code:
//==
-(void)hashTableCreation {
//BOOL contain = NO;
NSString *contain;
NSHashTable *hashTable = [NSHashTable weakObjectsHashTable];

    //static NSString *letters = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    static NSString *letters = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    NSString *str;
    NSString *searchStr = @"5AAA333"; //@"XX000";

    int numId = 25244537/2; // 9*26*26*1000; //9*26*26*26*1000 = 158,184,000;  9*26*26*1000 = 6084000
    BOOL containedDuplication = NO;
    int firstDigit;

    NSDate *start = [NSDate date];
    for (int i=0; i<numId; i++) {
        NSMutableString *randomString = [NSMutableString stringWithCapacity:3];
        for (int j=0; j<3; j++) {
            [randomString appendFormat: @"%C", [letters characterAtIndex: arc4random() % [letters length]]];
        }
        
        firstDigit = arc4random_uniform(10);
        if (firstDigit != 0) {
            str = [NSString stringWithFormat:@"%d%@%.3d", firstDigit, randomString, arc4random_uniform(1000)];
        }
        if ((i == numId - 1) && containedDuplication) {
            searchStr = str;
            //NSLog(@"search string = %@", searchStr);
        }
        //NSLog(@"%@", str);
        [hashTable addObject:str];
        str = nil;
    }
    NSTimeInterval timeInterval = fabs([start timeIntervalSinceNow]);
    NSLog(@"Generate %d licence plates for the registered cars in California in %0.3f sec (Dec. 2016)", numId, timeInterval);
    
    start = [NSDate date];
    //NSLog(@"search string = %@", searchStr);
    contain = ([hashTable containsObject:searchStr]) ? @"does" : @"does not";
    timeInterval = fabs([start timeIntervalSinceNow]);
    NSLog(@"Find out if there is a duplication in %0.9f sec", timeInterval);
    
    NSLog(@"Database using hashTable %@ contain object %@", contain, searchStr);

}

//==

This tutorial is more than six months old so questions are no longer supported at the moment for it. We will update it as soon as possible. Thank you! :]