标签: swift-playground

如何自动禁用 Swift Playground 的启用结果

我正在为 Swift Student Challenge 开发一个使用 ARKit 和 Vision 的 Swift Playground。在应用程序形式中,代码可以完美运行并使用大约 350mb 的 RAM。当我将代码带到 Playground 时,只要我尝试添加单个 SCNNode,Playground 就会崩溃并提示运行代码时出错。

不过,一旦我关闭启用结果,它就会以应用程序形式运行。

有没有办法自动关闭它,而不是告诉用户关闭启用结果,这感觉有点不完整和hacky?

ipad swift swift-playground

5
推荐指数
1
解决办法
781
查看次数

带有 async/await 的 Swift Playground“无法在范围内找到‘async’”

我正在尝试在 Xcode Playground 上运行这个异步函数:

import Foundation
import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true

enum NetworkingError: Error {
    case invalidServerResponse
    case invalidCharacterSet
}

func getJson() async throws -> String {
        let url = URL(string:"https://jsonplaceholder.typicode.com/todos/1")!
        let (data, response) = try await URLSession.shared.data(from: url)
        
        guard let httpResponse = response as? HTTPURLResponse,
              httpResponse.statusCode == 200 else {
                  throw NetworkingError.invalidServerResponse
              }
        
        guard let result = String(data: data, encoding: .utf8) else {
            throw NetworkingError.invalidCharacterSet
        }
        
        return result
}
    
let result = try! await getJson()
print(result)
Run Code Online (Sandbox Code Playgroud)

我收到此错误消息:

error: ForecastPlayground.playground:27:25: …
Run Code Online (Sandbox Code Playgroud)

async-await swift swift-playground swift5 xcode13

5
推荐指数
1
解决办法
3317
查看次数

以快速奇怪的行为为变量赋值

我有以下简单的代码段:

func swapper(var arr: [Int]) {
    let first: Int = arr[0]
    let last: Int  = arr[arr.count - 1]
    arr[0] = last
    arr[arr.count - 1] = first
    arr
}
var myFunctionPointer : ([Int]) -> () = swapper
Run Code Online (Sandbox Code Playgroud)

它运行良好但是当我尝试将inout添加 到方法的参数的签名时,我无法将其分配给变量外部,如下所示.

func swapper(inout  arr: [Int]){
    let first: Int = arr[0]
    let last: Int  = arr[arr.count - 1]
    arr[0] = last
    arr[arr.count - 1] = first
    arr
}
var myFunctionPointer: ([Int]) -> () = swapper // This failed [int] is not subtype …
Run Code Online (Sandbox Code Playgroud)

swift swift-playground

4
推荐指数
1
解决办法
6159
查看次数

NSDateFormatter总是返回nil.

我试图将日期时间字符串从Django Rest Framework转换为如此:"2014-10-28T18:14:32.457"到Swift字符串中.我在操场上运行以下代码并始终收到错误:

致命错误:在展开Optional值时意外发现nil

import Foundation

var mydate =  "2014-10-28T18:14:32.457"
let formatter = NSDateFormatter()
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"
formatter.timeStyle = .ShortStyle
formatter.dateStyle = .ShortStyle
var parsedDateTimeString = formatter.dateFromString(mydate)
formatter.stringFromDate(parsedDateTimeString!)
Run Code Online (Sandbox Code Playgroud)

我认为这是因为dateFromString总是返回nil.但是我无法弄清楚为什么它的回归为零.我很确定我的dateFormat是正确的.我哪里错了?

swift swift-playground

4
推荐指数
1
解决办法
4933
查看次数

在快速操场上使用NSTimer

我想知道如何使用NSTimerSwift游乐场内部.之前已经问过这个问题,但没有一个答案真正回答了这个问题.

这是我的Playground代码:

import Foundation

class MyClass {

    func startTimer() {
        NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "onTimer:", userInfo: nil, repeats: true)
    }

    func onTimer(timer:NSTimer!) {
        println("Timer here")
    }
}

var anInstance = MyClass()

anInstance.startTimer()
Run Code Online (Sandbox Code Playgroud)

实例化NSTimerscheduledTimerWithTimeInterval创建计时器,并在当前运行的循环时间表吧.

但这种方法onTimer从未被调用过.为什么?

nstimer uikit foundation swift swift-playground

4
推荐指数
1
解决办法
3583
查看次数

在Swift游乐场的"自动完成框"中出现的"S","T"和"Pr"图标是什么意思?

在Swift游乐场的"自动完成框"中出现的"S","T"和"Pr"图标是什么意思?还有其他种类吗?

谢谢!

截图

答案(搜索引擎索引)

C  : Classes
Pr : Protocols
f  : Functions
S  : Structs
U  : Unions
E  : Enums
T  : Types
V  : Globals
Run Code Online (Sandbox Code Playgroud)

swift swift-playground

4
推荐指数
1
解决办法
81
查看次数

初始化期间的Swift可选值可防止默认初始化程序继承

在Swift中:

1)如果为类中的所有存储属性提供默认值,则继承默认初始值设定项,即 - init().

- 和 -

2)任何可选类型的属性默认值为nil,即 - var shouldBeNill: String? //should initially be nill

- 因此 -

我希望这段代码能够正常工作:

class Product {
    let name: String?
}

let product = Product()
Run Code Online (Sandbox Code Playgroud)

但是当我把它作为游乐场输入时,我得到错误:"类产品没有初始化器".

为什么Product不继承默认初始值设定项init()?我知道我可以通过明确设置let name: String? = nil来完成这项工作,但我不确定为什么我必须这样做.这是Swift方面的错误,还是有些东西我不太理解?

initialization constants optional swift swift-playground

4
推荐指数
1
解决办法
707
查看次数

Swift playground:获取ViewController来调用presentViewController

我想在Playground玩UIAlertController.是否有可能从XCPlaygroundPage获取ViewController - 能够调用presentViewController?

xcode ios uialertcontroller swift-playground xcode7

4
推荐指数
2
解决办法
2731
查看次数

动画不在ios游乐场工作

这是我的动画方法(PreloaderView.swift)

public func performAction(action: PreloaderAction)
{
    let highOpacity = CABasicAnimation.init(keyPath: "opacity")
    let middleOpacity = CABasicAnimation.init(keyPath: "opacity")
    let lowOpacity = CABasicAnimation.init(keyPath: "opacity")

    if action == .PreloaderActionNone {
        self.thirdShape.removeAllAnimations()
        self.firstShape.opacity = 0.3;
        self.secondShape.opacity = 0.5
        self.thirdShape.opacity = 1.0
    } else if action == .PreloaderActionFailure {
        UIView.animateWithDuration(2, animations: {
            self.thirdShape.strokeColor = UIColor.redColor().CGColor
        }) {(completed : Bool) in
            self.thirdShape.strokeColor = self.ringColor.CGColor
        }

    }

    if action == .PreloaderActionStart {

        highOpacity.fromValue = 0.0
        highOpacity.toValue = 1.0
        highOpacity.duration = animationDuration
        highOpacity.autoreverses = true
        highOpacity.repeatCount = .infinity
        self.secondShape.addAnimation(highOpacity, …
Run Code Online (Sandbox Code Playgroud)

uianimation ios swift swift-playground

4
推荐指数
1
解决办法
1537
查看次数

更改Xcode 10 Playgrounds变量初始化更改?Xcode 10 Playgrounds是翻译吗?

我注意到Xcode 10中的Playgrounds不再允许使用已声明但未初始化的变量.例如:虽然这段代码可以在Xcode 9游乐场中运行,但在Xcode 10游乐场(至少在Beta 1中),它会崩溃:

var myValue: Int
//...
myValue = 100

print (myValue)

// Xcode 9 prints 100
// Xcode 10 reports an error: variables currently must have an initial value when entered at the top level of the REPL
Run Code Online (Sandbox Code Playgroud)
  1. 这是新行为,还是当前Xcode 10 beta中的一个错误?

  2. 我曾经将早期的Xcode Playgrounds称为翻译,但是仍然会考虑将Xcode 10游乐场作为翻译(并且总是正确的)?Apple将装订线中的"运行"按钮称为"编译"代码.

谢谢!

xcode read-eval-print-loop swift-playground

4
推荐指数
1
解决办法
2284
查看次数