Last active
August 29, 2015 14:05
-
-
Save jollyjoester/eaa6e3583bd68d50e557 to your computer and use it in GitHub Desktop.
playground_trial
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 UIKit | |
| var str = "Hello, playground" | |
| /** constant & value **/ | |
| let const = 10 | |
| //const = 15 //error | |
| var value = 20 | |
| value = 25 | |
| /** type inference **/ | |
| let lastName = "Nanashima" // String | |
| let age = 32 // Int | |
| let height = 169.5 // Double | |
| let isMale = true // Boolean | |
| let info = lastName + String(age) // Values are never implicitly converted another type. | |
| let info2 = "He is \(height) cm tall." // Include values in strings. | |
| /** type annotation **/ | |
| let firstName:String = "Hideyuki" | |
| let weight:Float = 75 | |
| /** array & dictionary **/ | |
| var array = [1,2,3,4] | |
| array[2] | |
| array[2] = 5 | |
| var dictionary = ["Japan":"Tokyo", "France":"Paris"] | |
| dictionary["Japan"] = "Kyoto" | |
| /** tuple **/ | |
| let tuple = (32, "Nanashima") | |
| tuple.0 | |
| tuple.1 | |
| /** optionals **/ | |
| var optionalTitle:String? = "Mr." | |
| optionalTitle = nil | |
| //str = nil //error | |
| /** if **/ | |
| var name:String | |
| if let title = optionalTitle { | |
| name = title + lastName | |
| }else{ | |
| name = "Guest" | |
| } | |
| /** switch **/ | |
| // no need to explicitly break | |
| // error without default in this case | |
| let vegetable = "red pepper" | |
| switch vegetable { | |
| case "celery": | |
| let vegetableComment = "Add some raisins and make ants on a log." | |
| case "cucumber", "watercress": | |
| let vegetableComment = "That would make a good tea sandwich." | |
| case let x where x.hasSuffix("pepper"): | |
| let vegetableComment = "Is it a spicy \(x)?" | |
| default: | |
| let vegetableComment = "Everything tastes good in soup." | |
| } | |
| /** loop **/ | |
| var n = 2 | |
| while n < 100 { | |
| n = n * 2 | |
| } | |
| n | |
| var m = 2 | |
| do { | |
| m = m * 2 | |
| } while m < 100 | |
| m | |
| var num = 0 | |
| for var i=0; i<5; i++ { | |
| num++ | |
| } | |
| for _ in 1..<5 { | |
| num++ | |
| } | |
| for _ in 1...5 { | |
| num++ | |
| } | |
| let names = ["taro", "jirou", "saburo"] | |
| for name in names { | |
| name | |
| } | |
| let radius = 10.0 | |
| for circleProp in 1...100 { | |
| let alpha = Double(circleProp) / 100.0 * 2.0 * M_PI | |
| let y = radius * (1 - cos(alpha)) | |
| let x = radius * (alpha - sin(alpha)) | |
| } | |
| let citDict = ["Japan":"Tokyo", "France":"Paris"] | |
| for (country, city) in citDict { | |
| city | |
| country | |
| } | |
| /** functions **/ | |
| func sum(first:Int, second:Int = 0) -> Int{ | |
| return first + second | |
| } | |
| sum(1, second:2) | |
| sum(1) | |
| func calculateStatistics(scores: Int...) -> (min: Int, max: Int, avg: Int) { | |
| var min = scores[0] | |
| var max = scores[0] | |
| var avg = 0 | |
| for score in scores { | |
| if score > max { | |
| max = score | |
| } else if score < min { | |
| min = score | |
| } | |
| avg += score / scores.count | |
| } | |
| return (min, max, avg) | |
| } | |
| let result = calculateStatistics(5, 3, 100, 3, 9) | |
| result.avg | |
| result.2 | |
| // nested function, first-class type, closure | |
| /** class **/ | |
| class Person { | |
| var name:String? = nil | |
| var age = 0 | |
| init(name:String, age:Int){ | |
| self.name = name | |
| self.age = age | |
| } | |
| func whoAmI() -> String { | |
| return "I am \(name!), \(age) years old" | |
| } | |
| } | |
| var nanashima = Person(name: "Nanashima", age:32) | |
| nanashima.whoAmI() | |
| // computed property & property observers | |
| class Engeneer : Person { | |
| var workingHours:Int{ | |
| get{ | |
| return self.workingHours + 4 | |
| } | |
| set{ | |
| self.workingHours = newValue | |
| } | |
| } | |
| var salary:Int = 0{ | |
| willSet{ | |
| // You can use `newValue` | |
| } | |
| didSet{ | |
| // You can use `oldValue` | |
| } | |
| } | |
| override func whoAmI() -> String { | |
| return super.whoAmI() + "and working \(workingHours) hours" | |
| } | |
| } | |
| // enum, struct | |
| // protocol, extension | |
| // generics | |
| // UIKit | |
| let frame = CGRectMake(0, 0, 300, 100) | |
| let view = UIView(frame: CGRectMake(0, 0, 320, 568)) | |
| view.backgroundColor = UIColor.grayColor() | |
| let label = UILabel(frame: frame) | |
| label.text = "Hello playground" | |
| view.addSubview(label) | |
| var imageURL:NSURL = NSURL(string: "http://goo.gl/ys7ACa")! | |
| let imageData:NSData = NSData(contentsOfURL: imageURL)! | |
| let image = UIImage(data: imageData) | |
| let imageView = UIImageView(image:image) | |
| imageView.center = CGPoint(x:150, y:300) | |
| view.addSubview(imageView) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment