In this post we will learn swift by building a tipster iphone app using Swift.

This is my third attempt at learning iOS and I must say Swift has really helped me in going forward and getting aquainted with iOS ecosystem. I finally got an iphone app in the app store and there is one currently in app review process. I understand there are many people who are for and against Swift, but I personally feel Swift has been really an awesome platform to get started.

Tipster will be a simple app which will help us to calculate the tip and the total amount to be paid.

The app also will included a Setting page which will help us to set the default Tip amount

Creating the layout

Open Main.storyboard

Change the UIView background color to #B7AEF2

Drag a label .Change the label text to Bill Amount

Drag a text label and place it infront of the Bill Amount label

Drag a label. Change the label text to Tip

Drag a label and place it next to the Tip label. Set the default o $0.00

label. Set the default o $0.00 Drag a UIView and set the height to 5px and change the background to white.

Drag a label and change the text to Total Amount

Drag another label and place it next to the Total Amount and set the value to $0.00

and set the value to $0.00 Finally drag a segmented control to the storyboard and change the number of segments to 3 and set the value to 18%, 20% and 25%.

Also embed the uiviewcontroller in a navigation controller. For more about autolayouts you could look at these tutorials Adaptive Layouts, Adaptive Layouts, AppCoda.

// // ViewController.swift // Tipster // // Created by Shrikar Archak on 12/13/14. // Copyright (c) 2014 Shrikar Archak. All rights reserved. // import UIKit class ViewController: UIViewController { @IBOutlet weak var tipControl: UISegmentedControl! @IBOutlet weak var tipLabel: UILabel! @IBOutlet weak var amountLabel: UITextField! @IBOutlet weak var totalLabel: UILabel! var userDefaults : NSUserDefaults? let tips = [ 18, 20, 25] var defaultTip : Int? override func viewDidLoad() { super.viewDidLoad() self.tipLabel.text = "$0.00" self.totalLabel.text = "$0.00" tipControl.layer.cornerRadius = 5.0 tipControl.layer.masksToBounds = true tipControl.tintColor = UIColor(red: 92.0/255, green: 69.0/255, blue: 133.0/255, alpha: 0.8) tipControl.selectedSegmentIndex = 0 userDefaults = NSUserDefaults.standardUserDefaults() defaultTip = userDefaults?.integerForKey("defaultTip") for (index,tmp) in enumerate(tips) { NSLog("### Val :\(index) \(tmp)") if(defaultTip == tmp){ tipControl.selectedSegmentIndex = index } } } override func viewWillAppear(animated: Bool) { defaultTip = userDefaults?.integerForKey("defaultTip") for (index,tmp) in enumerate(tips) { if(defaultTip == tmp){ tipControl.selectedSegmentIndex = index } } editTextAction(self) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } @IBAction func onTap(sender: AnyObject) { view.endEditing(true) } @IBAction func editTextAction(sender: AnyObject) { let bill = (amountLabel.text as NSString).doubleValue let total = bill + bill * (Double(tips[tipControl.selectedSegmentIndex])/100) totalLabel.text = NSString(format: "$%.2f", total) tipLabel.text = NSString(format: "$%.2f", bill * Double(tips[tipControl.selectedSegmentIndex])/100) } }

NSUserDefaults

NSUserDefaults provides a way for an app to store user details. NSUserdefaults caches the information efficiently and synchronizes the data at periodic intervals. Ideally NSUserdefaults should be used for small amount of data like preferences , setting etc. NSUserDefaults are persisted across app restarts however they are deleted if the app is deleted.

In our app we will use NSUserdefaults to store the default tip setting.

// // SettingsViewController.swift // Tipster // // Created by Shrikar Archak on 12/13/14. // Copyright (c) 2014 Shrikar Archak. All rights reserved. // import UIKit class SettingsViewController: UIViewController { @IBOutlet weak var tipLabel: UILabel! @IBOutlet weak var tipControl: UISegmentedControl! let tips = [18, 20, 25] var userDefaults : NSUserDefaults? let defaultTip: Double? override func viewDidLoad() { super.viewDidLoad() userDefaults = NSUserDefaults.standardUserDefaults() let tipVal = userDefaults?.integerForKey("defaultTip") tipControl.selectedSegmentIndex = 0 for (index,tmp) in enumerate(tips) { if(tipVal == tmp){ tipControl.selectedSegmentIndex = index } } tipControl.layer.cornerRadius = 5.0 tipControl.layer.masksToBounds = true tipControl.tintColor = UIColor(red: 92.0/255, green: 69.0/255, blue: 133.0/255, alpha: 0.8) } override func viewWillAppear(animated: Bool) { self.view.alpha = 0.6 UIView.animateWithDuration(1.5, animations: { () -> Void in self.view.alpha = 1.0 }) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } @IBAction func donePressed(sender: AnyObject) { self.dismissViewControllerAnimated(true , completion: { () -> Void in }) } @IBAction func valueChanged(sender: AnyObject) { let currTip = tips[tipControl.selectedSegmentIndex] self.tipLabel.text = "Default tip is \(currTip)%" userDefaults?.setInteger(currTip, forKey: "defaultTip") userDefaults?.synchronize() } }

Please let me know if you have any questions/feedback.