2016-10-05 2 views
0

나는 아이 패드 스위프트 놀이터 실험하고 있고 나는 그것을 반복적으로 나타나고 내가 그것을 실행할 때마다 오류 그러나이XCPlayground 대체품이 있습니까?

import UIKit 
import ObjectiveC 
import CoreFoundation 
import XCPlayground 

XCPSetExecutionShouldContinueIndefinitely() 

class StopWatch { 
    var myCounter = 0 

    func timer() { 
     var timer = Timer.scheduledTimer(
      timeInterval: 1, 
      target: self, 
      selector: Selector("incrementCounter:"), 
      userInfo: nil, 
      repeats: true 
     ) 
    } 

    @objc func incrementCounter(mytimer:Timer) { 
     myCounter = myCounter + 1 
     print(myCounter) 
    } 
} 

var myStopWatch = StopWatch() 
myStopWatch.timer() 

를 사용하여 코드 메신저입니다 기본 타이머을 시도하고있다. 이것은 내가 믿는 이유는 import xcPlaygrounds가 모든 기능과 함께 제공되는 ipad의 신속한 놀이터에서 사용할 수 없기 때문입니다.이 모듈을 대체하거나 더 좋은 방법이 있는지 궁금합니다.

감사

답변

1

당신이 swift3과 놀이터를 사용하는 경우, 아래의 코드를 사용할 수 있습니다.

'XCPSetExecutionShouldContinueIndefinitely는'그렇게되지 나는 참으로

PlaygroundSupport 모듈과 설정 needsIndefiniteExecution 값이 추가됩니다.

import PlaygroundSupport 

PlaygroundPage.current.needsIndefiniteExecution = true 

class StopWatch { 
    var myCounter = 0 


    func timer() { 
     let _ = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(incrementCounter(mytimer:)), userInfo: nil, repeats: true) 
    } 

    @objc func incrementCounter(mytimer:Timer) { 
     myCounter = myCounter + 1 
     print(myCounter) 
    } 
} 

var myStopWatch = StopWatch() 
myStopWatch.timer() 
+0

완벽하게 작동합니다. – Yellow

관련 문제