Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Dino Paškvan](https://secure.gravatar.com/avatar/ed70065c529ecafe5e2a0337493b9267?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Dino Paškvan
Courses Plus Student 44,108 PointsCrystal Ball and Blog Reader in Swift
I decided to go through the Crystal Ball tutorial again, but this time, using Swift. It wasn't too hard to do and Swift makes certain things really easy (and less verbose).
I'm really starting to like this programming language.
Anyway, if anyone is interested in the source code, it's available here.
I really hope I'm not breaking any Treehouse rules by posting this on GitHub, if I am, please let me know, and I'll remove it promptly.
I was rushing this, so I can't guarantee I've implemented everything from the Crystal Ball tutorial, but most of it should be there.
Also, I've replaced the AudioToolbox framework with AVFoundation because I couldn't make AudioToolbox work. So, the sound effect uses the AVAudioPlayer
class, and different methods.
![Brett Doffing](https://uploads.teamtreehouse.com/production/profile-photos/115974/micro_2607_1064170001433_889459_n.jpg)
Brett Doffing
4,978 PointsCan't wait to start using Swift. I've started reading up on it, but don't have the time to make anything yet.
8 Answers
![qvwzucovkm](https://secure.gravatar.com/avatar/438e246d63522621eb1334b32a87b2ae?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
qvwzucovkm
8,080 PointsHey Dino! Thanks for posting.
I am learning too so I figured I would try at least something with the code. Here is an one-liner for:
for num in 1...60 {
if num < 10 {
imagesArr.append(UIImage(named: "CB0000(num)")) }
else {
imagesArr.append(UIImage(named: "CB000(num)"))
}
}
//swifter!
for num in 1...60 {
imagesArr.append(UIImage(named: "CB000(Double(num)/10)".stringByReplacingOccurrencesOfString(".", withString: "")))
}
Writing a String extension would be a swifter option, maybe.
Hey Aaron! Well, I am not going to rewrite the code, ehem, but check this out from the official guide.
![Dino Paškvan](https://secure.gravatar.com/avatar/ed70065c529ecafe5e2a0337493b9267?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Dino Paškvan
Courses Plus Student 44,108 PointsBoth of these have been updated to reflect changes to Swift's array syntax introduced in Xcode 6 beta 3 (I had to move one character :) ).
![Thomas Nilsen](https://secure.gravatar.com/avatar/897eff05ae62fe42b8a2109867700453?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Thomas Nilsen
14,957 PointsI'm reading the book published by apple on swift. About halfway done. Examples like this really helps. Good work! :)
![Dino Paškvan](https://secure.gravatar.com/avatar/ed70065c529ecafe5e2a0337493b9267?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Dino Paškvan
Courses Plus Student 44,108 PointsAnd the Blog Reader in Swift, although with slightly more awkward code. I'll need to spend more time with the documentation to see if what I'm doing is actually the right way to do things.
![agreatdaytocode](https://uploads.teamtreehouse.com/production/profile-photos/122453/micro_Aaron_A.jpg)
agreatdaytocode
24,757 PointsSweet I'll check it out.
![Marlon Henry](https://uploads.teamtreehouse.com/production/profile-photos/649902/micro_Photo_on_3-18-12_at_4.51_PM__2.jpg)
Marlon Henry
6,885 PointsI get a crash with the thumbnail line. I take out Swift blogPost.thumbnail = bpDictionary["thumbnail"] as String
and everything is fine, I leave it in and I get bad access
![Dino Paškvan](https://secure.gravatar.com/avatar/ed70065c529ecafe5e2a0337493b9267?s=60&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Dino Paškvan
Courses Plus Student 44,108 PointsI've updated the code in the repository so it works with the latest Xcode release.
![Björn Steneram](https://uploads.teamtreehouse.com/production/profile-photos/118397/micro_174499_610235921_3563278_n.jpg)
Björn Steneram
5,523 PointsLooks like Swift will mean less code and less code is always better :) Thanks for these examples!
![James Barnett](https://secure.gravatar.com/avatar/43ea08b4fc547e8a5da87c207bea2123?s=60&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
James Barnett
39,199 Points>
less code and less code is always better
Not really no, see code golf
![Thomas Nilsen](https://secure.gravatar.com/avatar/897eff05ae62fe42b8a2109867700453?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Thomas Nilsen
14,957 PointsI was wondering about this:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return blogPosts.count
}
The function returns an int, which is fine. It takes two parameters: a UITableView and a section. Why does it have 'numberOfRowsInSection' right before the section parameter?
![Dino Paškvan](https://secure.gravatar.com/avatar/ed70065c529ecafe5e2a0337493b9267?s=60&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Dino Paškvan
Courses Plus Student 44,108 PointsnumberOfRowsInSection
is the external parameter name (which is more descriptive) and section
is the local parameter name.
I guess the idea was to replicate the parameter passing when sending messages in Objective-C.
You can read about it here.
![agreatdaytocode](https://uploads.teamtreehouse.com/production/profile-photos/122453/micro_Aaron_A.jpg)
agreatdaytocode
24,757 PointsAny ideas on how to translate this into swift?
-(NSString *)generateSHA256:(NSString *)signature secretKey:(NSString *)secretKey{
NSLog(@"[+] %@", NSStringFromSelector(_cmd));
const char *cKey = [secretKey cStringUsingEncoding:NSASCIIStringEncoding];
const char *cData = [signature cStringUsingEncoding:NSASCIIStringEncoding];
unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC
length:sizeof(cHMAC)];
NSString *hash = [HMAC base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSLog(@"hash==> %@", hash);
return hash;
}
![Dino Paškvan](https://secure.gravatar.com/avatar/ed70065c529ecafe5e2a0337493b9267?s=60&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Dino Paškvan
Courses Plus Student 44,108 PointsThis isn't a straightforward re-write. The problematic part here is the CCHmac()
function call. It's a C function, and Apple hasn't yet provided bindings for all of them. So, CommonCrypto/CommonHMAC is not available (at least I couldn't get it to run directly).
What you could theoretically do is create your own Objective-C class that exposes the parts of CommonHMAC that you need and then use that inside of Swift. So far, I've tried to avoid using bridging headers.
![agreatdaytocode](https://uploads.teamtreehouse.com/production/profile-photos/122453/micro_Aaron_A.jpg)
agreatdaytocode
24,757 PointsI was afraid that was the case. Thanks for the help. I think I'll wait for apple to update this before pushing this over to swift.
![qvwzucovkm](https://secure.gravatar.com/avatar/438e246d63522621eb1334b32a87b2ae?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
qvwzucovkm
8,080 PointsThe comments section needs a delete post option.
agreatdaytocode
24,757 Pointsagreatdaytocode
24,757 PointsNice job! thanks for sharing this.