swift 입문 소스

2015. 11. 26. 15:26Programming/Swift

반응형

// playground 에서 아래 소스를 실행해 보자.

1.

//: Playground - noun: a place where people can play

import UIKit


class ExamClass {

    deinit {

        print("deinit")

    }

}


2.

import UIKit


var exam: ExamClass? = ExamClass()

exam = nil


for i in 0...10 {

    i * i

}


3.

import UIKit

import XCPlayground


let circleRect = CGRectMake(0,0,300,300)

class CircleView: UIView {

    override func drawRect(rect: CGRect) {

        let context:CGContextRef = UIGraphicsGetCurrentContext()!

        UIColor.whiteColor().setFill()

        CGContextFillRect(context, rect)

        UIColor.blackColor().setFill()

        CGContextFillEllipseInRect(context,circleRect)

    }

}


let view = CircleView(frame: circleRect)

XCPShowView("뷰어",view:view)


4.

import UIKit

import XCPlayground


XCPSetExecutionShouldContinueIndefinitely(true)


class Exam: NSObject {

    func timerStart() {

        NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "chk", userInfo: nil, repeats:true)

    }

    

    var cnt = 0

    func chk() {

        print("timer check!")

    }

}


let exam = Exam()

exam.timerStart()


5.

import UIKit

import XCPlayground


XCPSetExecutionShouldContinueIndefinitely(true)


class Exam: NSObject {

    func timerStart() {

        NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "chk", userInfo: nil, repeats:true)

    }

    

    var cnt = 0

    func chk() {

        XCPCaptureValue("count", value:++cnt)

        XCPCaptureValue("random", value:arc4random_uniform(100))    //난수 발생

    }

}


let exam = Exam()

exam.timerStart()


6.

import UIKit

import XCPlayground


let chk:Int? = 2

let db:[String] = ["","서울","대전","부산"]


if let unrapedChk = chk {

    let sido = db[unrapedChk]

    print(sido)

else {

    print("Error")

}


7.

class SwiftLab: CustomStringConvertible {

    var description: String {

        return "안녕하세요"

    }

}


let sLab = SwiftLab()

print(sLab)


8.

let apple = 5

assert(apple < 10"10개미만")

반응형