Skip to content

Instantly share code, notes, and snippets.

@jollyjoester
Last active August 29, 2015 14:03
Show Gist options
  • Save jollyjoester/0442b6db56c076aa7c0c to your computer and use it in GitHub Desktop.
Save jollyjoester/0442b6db56c076aa7c0c to your computer and use it in GitHub Desktop.
swift trial
// Playground - noun: a place where people can play
import UIKit
var str = "Hello, playground"
var str2 = "Stinrg"
str2 = "Strign2"
let str3 = "String3"
//str3 = "strgin4"
var num = 2
var float = 2.5
var name:String = "Nanashima"
var age:Integer = 32
if name.isEmpty {
name.isEmpty
}else{
name = "Mr." + name
}
let array = [1,2,3,4]
array[2]
let dict = ["hoge":"duge", "hgae":"boke"]
dict["hoge"]
let job:String? = dict["hhhh"]
if let job2 = job {
let message = "great!"
}else{
let message = "NEET \(name)"
}
var taple = (32, "Nanashima")
taple.0
taple.1
switch taple {
case let(age, name) where age > 30:
let message = "\(name) is 30 over"
case let(age, name):
let message = "under 30"
fallthrough
default:
let message = "default"
}
num
while num < 10 {
num++
}
for var i=0; i<5; i++ {
num++
}
for i in 1..5 {
num++
}
for i in 1...5 {
num++
}
let names = ["taro", "jirou", "saburo"]
for name in names {
name
}
let citDict = ["Japan":"Tokyo", "France":"Paris"]
for (country, city) in citDict {
city
country
}
func sum(first:Float, second:Int)->Float{
return first + Float(second)
}
let price = sum(1.5, 2)
func hello<T>(x:T)->(String){
return "Hello, \(x)"
}
hello("Swift");
hello(941);
hello(1.08)
hello([1,1,2,3,5,8,13]);
hello(["Swift":0]);
class Stone {
var color:String{
willSet{
//some
}
didSet{
//some
}
}
let name:String? = nil
init(color:String="white"){
self.color = color
self.name = "\(color)Stone"
}
func whoAmI()->String {
return "This is \(self.name), and It's \(self.color)"
}
func getClassName() -> String {
return "Stone"
}
}
var blackStone = Stone(color:"black")
blackStone.whoAmI()
protocol humanProtocol {
var name:String {get set}
var age:Integer{get}
func walk()
}
struct nanshima : humanProtocol{
var name:String
var age:Integer
func walk(){
//walk
}
}
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)
let image = UIImage(contentsOfFile: "/Users/jollyjoester_pro/Desktop/[email protected]")
let imageView = UIImageView(image: image)
imageView.image
view.addSubview(imageView)
@jollyjoester
Copy link
Author

add willSet and didSet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment