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:
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.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.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.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()
.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:
SKSpriteNode
class is responsible for loading and drawing images on the screen..replace
for drawing the background image, which causes SpriteKit to ignore transparency. This is faster, and perfect for our solid background image.SKPhysicsBody
, which has rectangleOf
and circleWithRadius
initializers that create different shapes of physics bodies.SKAction
.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.
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.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.