2016-10-05 54 views
0

我與斯威夫特遊樂場經歷過了ipad和我試圖做一個基本的計時器,這是使用是否有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() 

代碼IM然而,它與反覆出現和錯誤每次運行它的時候。我相信這是因爲導入xcPlaygrounds在ipad的快速操場上不可用,以及隨附的所有功能和命令,我想知道是否有替代此模塊或更好的方法。

謝謝

回答

1

如果您使用swift3操場,您可以使用下面的代碼。

'XCPSetExecutionShouldContinueIndefinitely' 被棄用,所以我說

PlaygroundSupport模塊並設置needsIndefiniteExecution值設置爲true。

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

相關問題