Giter Club home page Giter Club logo

swiftcookbook's Introduction

h4labs Swift Cookbook

This content will (eventually) be mirrored on my site: http://www.h4labs.com/dev/ios/swift_cookbook.html

This repo will be mainly about the Swift language. I have another repo with small iOS projects

Swift 5.x compatible

Variable Declarations

Common types: Int, String, Bool, CGFloat, Double, Float

let is used to declare immutable values, while var is used to declare mutable variables.

let language = "Swift" // Immutable String
var i = 0 // mutable Int
i += 1
var x:CGFloat = 2
var name:String
let max:Int
max = 99
var isDone = false // Bool

if

let fuel = 9
let msg:String // Must assign in all cases

if fuel < 10 {
    msg = "Fuel dangerously low: \(fuel)"
} else if fuel < 50 {
    msg = "Fuel low: \(fuel)"
} else {
    msg = "Fuel OK"
}

ternary operator

let color = Bool.random() ? "red" : "black"

switch

switch ticker {

 case "AAPL":

 case "GOOGL":

 case "MSFT":

 default:

}

for

for i in 0..<10 {
    print("\(i)")
}

for i in 0...9 {
    print("\(i)")
}

for enumeration

for company in ["Apple", "Google", "Microsoft"] {
    print("\(company)")
}

function declarations

func add1(i:Int) -> Int { i + 1 } // return word not needed if we have 1 expression
let r0 = add1(i: 1) // 2

Same function but add _ to not need a named parameter

func add1(_ i:Int) -> Int { i + 1 } // return word not needed if we have 1 expression
let r0 = add1(1) // 2

Multiple return values using tuples

func bestLanguage() -> (String, Int) {
   return ("Swift", 1)
}

let (language, rank) = bestLanguage()

Randomness

let rnd = Int.random(in: 0...1) // 0 or 1
Bool.random() // true or false

Int.random(in: 20...29) // Random in range 20 through 29
Float.random(in: 0...1) // Random between 0 and 1

[1,2,3].randomElement() // random Array element
Set([1,2,3]).randomElement() // random Set element

Shuffle

Sometimes, it's better to shuffle an Array then simply iterate of the result to get a random element. This allows each element to be chosen at random exactly once.

let shuffled = [1,2,3].shuffled()

enum

swiftcookbook's People

Contributors

melling avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.