This chapter presented the Tip Calculator app that calculates and displays 15% and custom tip percentage tips and totals for a restaurant bill. The app uses Text Field and Slider UI components to receive user input and update suggested tips and bill totals in response to each user interaction.
We introduced Swift—Apple’s programming language of the future—and several of its object-oriented programming capabilities, including objects, classes, inheritance, methods and properties. As you saw, the app’s code required various Swift data types, operators, control statements and keywords.
You learned about strong and weak references and that only strong references affect an object’s reference count. You also learned that iOS’s automatic reference counting (ARC) removes an object from memory only when the object’s reference count becomes 0.
You used Interface Builder to design the app’s UI visually. We showed how to build your UI faster by duplicating UI components that had similar attribute settings. You learned that Labels (UILabel), Sliders (UISlider) and Text Fields (UITextField) are part of iOS’s UIKit framework that’s automatically included with each app you create.
We showed how to use import to give your code access to features in preexisting frameworks. You learned that a scene is managed by a view-controller object that determines what information is displayed and how user interactions with the scene’s UI are processed. Our view-controller class inherited from class UIViewController, which defines the base capabilities required by view controllers in iOS.
You used Interface Builder to generate @IBOutlet properties (outlets) in your view controller for programmatically interacting with the app’s UI components. You used visual tools in Interface Builder to connect a UI control to a corresponding outlet in the view controller. Once a connection was made, the view controller was able to manipulate the corresponding UI component programmatically.
You saw that interacting with a UI component caused a user-interface event and sent a message from the UI component to an action (event-handling method) in the view controller. You learned that an action is declared in Swift code as an @IBAction. You used visual tools in Interface Builder to connect the action to specific user-interface events.
Next, you learned that after all the objects in a storyboard are created, iOS sends a viewDidLoad message to the corresponding view controller so that it can perform view-specific tasks that can be executed only after the UI components in the view exist. You also called the UITextField’s becomeFirstResponder method in viewDidLoad so that iOS would display this keypad immediately after the view loaded.
You used NSDecimalNumbers for precise financial calculations. You also used class NSNumberFormatter to create locale-specific currency and percentage string representations of NSDecimalNumbers. You used Swift’s operator overloading capabilities to simplify NSDecimalNumber calculations.
In the next chapter, we present the Twitter Searches app, which allows you to save your favorite (possibly lengthy) Twitter search strings with easy-to-remember short tag names. You’ll store the search strings and their short tag names in Foundation framework collections. You’ll also use iCloud key–value pair storage so that you can sync your query between all your iOS devices that have the Twitter Searches app installed.