Swift 操场上有替代品吗?
我正在试验ipad的Swift Playgrounds,我正在尝试制作一个基本的计时器这是我使用的代码Swift 操场上有替代品吗?,swift,swift-playground,Swift,Swift Playground,我正在试验ipad的Swift Playgrounds,我正在尝试制作一个基本的计时器这是我使用的代码 import UIKit import ObjectiveC import CoreFoundation import XCPlayground XCPSetExecutionShouldContinueIndefinitely() class StopWatch { var myCounter = 0 func timer() { var timer =
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()
然而,每次我运行它时,它都会反复出现错误。我认为这是因为ipad的swift playgrounds中没有import xcPlaygrounds以及它附带的所有功能和命令。我想知道是否有此模块的替代品或更好的方法
谢谢如果您正在与swift3一起使用游乐场,您可以使用下面的代码 “XCPSetExecutionShouldContinueIndefinitely”已被弃用,因此我添加了 PlaygroundSupport模块,并将NeedsDefiniteExecution值设置为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()
如果您正在使用操场与swift3,您可以使用下面的代码 “XCPSetExecutionShouldContinueIndefinitely”已被弃用,因此我添加了 PlaygroundSupport模块,并将NeedsDefiniteExecution值设置为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()