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 project in Terminal
  4. create a new podfile
    1. $ pod init
  5. This will create a file called “Podfile”
  6. Open the file for editing
  7. Un comment the platform line. (the second line)
  8. Make sure the use_frameworks! line is not commented out if you’re using swift
  9. after the first Pods for <project> line add a Pod file
  10. e.g. pod ‘Firebase/Core’
  11. run install command
    1. $ pod install
  12. not the output about using the new xcworkspace file instead of the old project file.

 
To Install FireBase

  1. Do all of the above
  2. Then go to the Firebase console and register your app
  3. Download the google plist file and add it to your project
  4. Open the application.swift file
  5. add the following line to the top of the file
    1. import Firebase
  6. in the didFinishLaunchingWithOptions method, add the following line
    1. FirebaseApp.configure()
  7. NOTE: You have to do step 3, otherwise you’ll get and error for the line you add at 6A

 
 

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”
To safely access the properties and methods of a wrapped instance, use the postfix optional chaining operator (postfix ?).
e.g.

if let isPNG = imagePaths["star"]?.hasSuffix(".png") {
 print("The star image is in PNG format")
 }
// Prints "The star image is in PNG format"

if you want to supply a “default” value for when the optional is nil, use the nil-coalescing operator (??)

let defaultImagePath = "/images/default.png"
let imagePath = imagePaths["puppy"] ?? defaultImagePath
print(imagePath)
// Prints "/images/default.png"

Unconditionally Unwrapping
When you’re certain that an optional contains a value, you can unconditionally unwrap it using the forced unwrap operator (!)

let number = Int("42")!
print(number)
// Prints "42"

Automatic Unwrapping
If you declare an optional variable with a ! instead of a ? it will automatically unwrap.

var myString:String?
myString = "Hello, Swift!"
if myString != nil {
 println( myString! )
 }else {
 println("myString has nil value")
 }

Vs.

var myString:String!
myString = "Hello, Swift!"
if myString != nil {
 println(myString)
 }else {
 println("myString has nil value")
 }

NOTE: Unconditionally unwrapping a nil instance with ! triggers a runtime error.
see more at https://developer.apple.com/documentation/swift/optional