UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

What you learned

You probably haven’t realized it yet, but the projects you just completed were some of the most important in the series. The end results are nice enough, but what they teach is more important – you made your first class completely from scratch, which is how you’ll tackle many, many problems in the future:

  • You met UICollectionView and saw how similar it is to UITableView. Sure, it displays things in columns as well as rows, but the method names are so similar I hope you felt comfortable using it.
  • You also designed a custom UICollectionViewCell, first in the storyboard and then in code. Table views come with several useful cell types built in, but collection views don’t so you’ll always need to design your own.
  • We used UIImagePickerController for the first time in this project. It’s not that easy to use, particularly in the way it returns its selected to you, but we’ll be coming back to it again in the future so you’ll have more chance to practice.
  • The UUID data type is used to generate universally unique identifiers, which is the easiest way to generate filenames that are guaranteed to be unique. We used them to save images in project 10 by converting each UIImage to a Data that could be written to disk, using jpegData().
  • You met Apple’s appendingPathComponent() method, as well as my own getDocumentsDirectory() helper method. Combined, these two let us create filenames that are saved to the user’s documents directory. You could, in theory, create the filename by hand, but using appendingPathComponent() is safer because it means your code won’t break if things change in the future.

Project 11 was the first game we’ve made using SpriteKit, which is Apple’s high-performance 2D games framework. It introduced a huge range of new things:

  • The SKSpriteNode class is responsible for loading and drawing images on the screen.
  • You can draw sprites using a selection of blend modes. We used .replace for drawing the background image, which causes SpriteKit to ignore transparency. This is faster, and perfect for our solid background image.
  • You add physics to sprite nodes using SKPhysicsBody, which has rectangleOf and circleWithRadius initializers that create different shapes of physics bodies.
  • Adding actions to things, such as spinning around or removing from the game, is done using SKAction.
  • Angles are usually measured in radians using CGFloat.

Lastly, you also met UserDefaults, which lets you read and write user preferences, and gets backed up to iCloud automatically. You shouldn’t abuse it, though: try to store only the absolute essentials in UserDefaults to avoid causing performance issues. In fact, tvOS limits you to just 500KB of UserDefaults storage, so you have no choice!

For the times when your storage needs are bigger, you should either use the Codable protocol to convert your objects to JSON, or use NSKeyedArchiver and NSKeyedUnarchiver. These convert your custom data types into a Data object that you can read and write to disk.

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 5.0/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.