Swift

Getting Started with CocoaPods

Installation
  1. make sure the ruby system is up to date
    1. $ sudo gem update --system
  2. install cocoapods
    1. $ sudo gem install cocoapods
    2. If you get an error message about fuzzy_match, try this command instead,
    3. $ sudo gem install -n /usr/local/bin cocoapods
  3. setup the pods
    1. $ pod setup
To Test / use cocoa pods
  1. Create a new single view project in Xcode
  2. Quit Xcode
  3. navigate to the folder of the new proj

Connecting a second ViewContoller to another Swift file

  1. Make sure the swift file looks like the default ViewContoller.swift file i.e. import UIKit, inherits from UIViewController, have at least the viewDidLoad function
  2. Click on the yellow dot that represents the view controller in the story board
  3. Select the identify inspector page
  4. In the Class drop down at the top, you should be able to select the name of your new class NOTE: you may have to save and or compile you app to get the new class to appear on the list.

Detecting iPad or iPhone in swift

To detect if the user is using an iPad or iPhone or something else in Swift do something like this: switch UIDevice.current.userInterfaceIdiom { case .phone: deviceLavel.text = "iPhone" case .pad: deviceLavel.text = "iPad" case .unspecified: deviceLavel.text = "unspecified" case .tv: deviceLavel.text = "tv" case .carPlay: deviceLavel.text = "carPlay" }

Optionals in Swift 3

Swift Optionals

A type the either has a "wrapped" value or is nil. e.g. let imagePaths = ["star": "/glyphs/star.png","portrait": "/images/content/portrait.jpg","spacer": "/images/shared/spacer.gif"] Getting a dictionary’s value using a key returns an optional value, so imagePaths["star"] has the type String? e.g. if let imagePath = imagePaths["star"] { print("The star image is at '(imagePath)'") } else { print("Couldn't find the image") } would print "The star image is at '/glyphs/star.png'" but "if let starPath = imagePaths[puppy]..... would print "Couldn't find the image" T

Convert numbers to and from strings in Swift

Convert numbers to and from strings in Swift // To convert a number to a string var costString = String(9.99) // To convert a string to an Int var wholeNumber = Int("27") // To convert a string to an Float let lessPrecisePI = Float("3.14") // To convert a string to an Double let morePrecisePI = Double("3.1415926536")

Theme by Danetsoft and Danang Probo Sayekti inspired by Maksimer