Extensions
Up to now, you have accessed the assets you added to your apps through Interface Builder, but assets can also be accessed programmatically. Each image and color you added to the Asset Catalog has a name, which is how you reference it in code:
let happyImage: UIImage? = UIImage(named: "happy") let happyColor: UIColor? = UIColor(named: "happyTurquoise")
Note that the resources here are looked up by their names, which are strings. If you were to enter a resource name in your code incorrectly, the resource would not be located at runtime. Also, because there may not be a resource associated with a given string (even if it is entered correctly), these initializers are failable and therefore must return an optional.
In short, ...
Get iOS Programming: The Big Nerd Ranch Guide, 7th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.