Created
June 5, 2014 22:20
-
-
Save johno/a9cb70fc7ba88764972b to your computer and use it in GitHub Desktop.
Playing around with Swift. http://johnotander.com/swift/2014/06/04/an-introduction-to-the-swift-programming-language-part-one/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Playground - noun: a place where people can play | |
import Cocoa | |
var str = "Hello, playground" | |
var myConstant = "This is an immutable string" | |
myConstant = "Can't do this" | |
var favoriteNumberDescr = "My favorite number is " | |
var favoriteNumber = 7 | |
"My favorite number is \(favoriteNumber)" | |
let arrayOne = String[]() | |
let arrayTwo = [] | |
let dictionaryOne = Dictionary<String, Float>() | |
let dictionaryTwo = [:] | |
func printStr(strToPrint: String) { | |
println(strToPrint) | |
} | |
func getHelloStr(helloRecipient: String) -> String { | |
return "Hello, \(helloRecipient)" | |
} | |
func makeIsOneCheck() -> (Int -> Bool) { | |
func isOne(val: Int) -> Bool { | |
return val == 1 | |
} | |
return isOne | |
} | |
func divideByTwo(number: Int) -> Int { | |
return number/2 | |
} | |
var isOneCheck = makeIsOneCheck() | |
isOneCheck(1) | |
isOneCheck(7) | |
let numbers = [1, 4, 8, 234, 12] | |
numbers.map(divideByTwo) | |
[1, 4, 8, 234, 12].map({ | |
(number: Int) -> Int in | |
return number / 2 | |
}) | |
[1, 4, 8, 234, 12].map({ number in number/2 }) | |
class User { | |
var username: String = "" | |
var email: String = "" | |
var favoriteFood: String = "" | |
var favoriteNumber: Int | |
init(email: String, favoriteNumber: Int) { | |
self.email = email | |
self.favoriteNumber = favoriteNumber | |
} | |
func description() -> String { | |
return "\(username) with email \(email) who likes the number \(favoriteNumber) and eating \(favoriteFood)" | |
} | |
func isAdmin() -> Bool { | |
return false | |
} | |
} | |
let u = User(email: "[email protected]", favoriteNumber: 7) | |
u.email | |
u.description() | |
class Admin: User { | |
var role: String { | |
willSet { | |
priorRoles.append(self.role) | |
} | |
} | |
var priorRoles: String [] | |
init(email: String, favoriteNumber: Int, role: String) { | |
self.role = role | |
self.priorRoles = [] | |
super.init(email: email, favoriteNumber: favoriteNumber) | |
} | |
override func isAdmin() -> Bool { | |
return true | |
} | |
} | |
let admin = Admin(email: "[email protected]", favoriteNumber: 7, role: "posts") | |
admin.role | |
admin.priorRoles | |
admin.role = "New Role" | |
admin.priorRoles | |
admin.isAdmin() | |
let immutableArray = ["1", "2", "3"] | |
func omgWtf(array: String[]) { | |
var copy = array | |
copy[0] = "omg" | |
copy[1] = "wtf" | |
} | |
omgWtf(immutableArray) | |
immutableArray // ["omg", "wtf", "3"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment