有 XCPlayground 的替代品吗?
Is there a replacement for XCPlayground?
我正在为 ipad 试验 Swift 游乐场,我正在尝试制作一个基本计时器,这是我使用的代码
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 在 swift playgrounds for ipad 及其附带的所有功能和命令中不可用 我想知道是否有此模块的替代品或更好的方法这个。
谢谢
如果你使用 playground 和 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()
我正在为 ipad 试验 Swift 游乐场,我正在尝试制作一个基本计时器,这是我使用的代码
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 在 swift playgrounds for ipad 及其附带的所有功能和命令中不可用 我想知道是否有此模块的替代品或更好的方法这个。
谢谢
如果你使用 playground 和 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()