标签: swift-playground

为什么Xcode Playground通过类似iPad的模拟器执行liveView?

在此输入图像描述

我正在使用Swift 3在Xcode 8.2.1 Playground中尝试一些代码.
自从PlaygroundPage.current.liveView执行类似iPad的模拟器以来,我一直很困惑.
我想通过较小设备的模拟器测试键盘输入.我能更好地处理这件事吗?

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setupTextView()
    }

    private func setupTextView() {
        let textView = UITextView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height))
        textView.backgroundColor = .gray
        textView.isSelectable = true
        textView.font = textView.font?.withSize(20)
        view.addSubview(textView)
    }

}

let viewController = ViewController()
let window = UIWindow(frame: CGRect(x: 0, y: 0, width: 640, height: 600))
window.rootViewController = viewController
window.makeKeyAndVisible()

import PlaygroundSupport

PlaygroundPage.current.liveView = window
PlaygroundPage.current.needsIndefiniteExecution = true
Run Code Online (Sandbox Code Playgroud)

xcode ipad ios swift swift-playground

19
推荐指数
1
解决办法
1280
查看次数

获取操场以显示所有循环结果

我正在使用xcode 7游乐场,我想知道如何让游乐场显示循环打印出的所有结果.

例如:

在此输入图像描述

这将告诉我循环运行4次,但它只显示迭代的最后一个值.有没有办法让它显示所有价值观?

swift swift-playground xcode7 swift2

18
推荐指数
1
解决办法
4945
查看次数

在Playground中使用实时照片

我已经做了大量的网络搜索,但我现在正试图在Playground中使用"Live Photos".我知道这个框架(PHLivePhoto),我不知道如果在Playground中与他们合作是可能的,因为没有太多的"导入",因为似乎没有任何"实时照片"可用在线下载.有任何想法吗?

uikit ios swift swift-playground phlivephoto

18
推荐指数
1
解决办法
3835
查看次数

通过Swift中的下标访问属性

我在Swift中有一个自定义类,我想使用下标来访问它的属性,这可能吗?

我想要的是这样的:

class User {
    var name: String
    var title: String

    subscript(key: String) -> String {
        // Something here
        return // Return the property that matches the key…
    }

    init(name: String, title: String) {
        self.name = name
        self.title = title
    }
}

myUser = User(name: "Bob", title: "Superboss")
myUser["name"] // "Bob"
Run Code Online (Sandbox Code Playgroud)

更新:我正在寻找这个的原因是我正在使用GRMustache从HTML模板进行渲染.我希望能够将我的模型对象传递给GRMustache渲染器......

GRMustache使用键控订阅objectForKeyedSubscript:方法和键值编码valueForKey:方法获取值.任何兼容对象都可以为模板提供值.

https://github.com/groue/GRMustache/blob/master/Guides/view_model.md#viewmodel-objects

swift swift-playground

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

如何在 Playground 中导入 Swift 包?

我可以将 Swift Package 导入到我工作区中的 Playground 吗?我已经阅读了答案,但我认为它已经过时了,因为现在我们可以在 iOS 项目中使用 Swift Packages。

swift swift-playground swift-package-manager

16
推荐指数
3
解决办法
3717
查看次数

Xcode 6 Beta/Swift - Playground不更新

我正在玩Xcode 6的第一个测试版的Playground功能 - 我注意到Playground没有更新的一半时间(根本不显示结果计算或发生了多少循环迭代)简单的代码/循环/函数在那里.甚至是Swift Tour https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/GuidedTour.html

有几行代码没有显示在Playground中.如果你搞乱代码,它有时会显示,通过移动代码或将其放在其他地方.还有谁?任何修复?这只是一个测试版问题吗?

swift xcode6 swift-playground

15
推荐指数
1
解决办法
1万
查看次数

游乐场执行终止,因为游乐场进程意外退出

我想在Xcode 6.0.1中使用一个游乐场.所以文件 - >新 - >游乐场

我立刻得到了错误.游乐场执行终止,因为游乐场进程意外退出.见图.重启Xcode没有帮助.

Xcode有什么问题?Xcode错误

xcode ios swift swift-playground

15
推荐指数
2
解决办法
7336
查看次数

Swift Playground - 文件不可读

Swift Playground中无法读取文件.

如何使文件可读?

相同的代码在Xcode终端应用程序上运行良好,但在Swift Playground上失败.

下面的演示代码.

import Foundation

println("Hello, World!")

var fname:String = "/Users/holyfield/Desktop/com.apple.IconComposer.plist"
var fm:NSFileManager = NSFileManager.defaultManager()

if(fm.fileExistsAtPath(fname)){
    println("File Exists")
    if(fm.isReadableFileAtPath(fname)){
        println("File is readable")
        var fd:NSData? = NSData(contentsOfFile: fname)
        println(fd?.length)
        let pl = NSDictionary(contentsOfFile: fname)
        println(pl?.count)
        println(pl?.allKeys)
    }else{
        println("File is not readable")
    }
}
else{
    println("File does not exists")
}
Run Code Online (Sandbox Code Playgroud)

示例图片:

XCode终端应用程序 斯威夫特游乐场

xcode foundation swift xcode6 swift-playground

15
推荐指数
2
解决办法
8501
查看次数

Xcode游乐场中的自定义字体

我在代码中编写接口布局.因为每次我逐个像素地测试字体大小或视图布局时重新加载应用程序很烦人,所以我开始在操场上进行.这确实有很大帮助.但我确实想念那里的自定义字体.

有没有办法在游乐场添加自定义字体?

xcode swift-playground

14
推荐指数
1
解决办法
2493
查看次数

斯威夫特三个双引号

我是Swift的新手.docuentation说:对于占用多行的字符串使用三个双引号(""").每个引用行开头的缩进被删除,只要它与结束引号的缩进匹配.例如:

let quotation = """
Even though there's whitespace to the left,
the actual lines aren't indented.
Except for this line.
Double quotes (") can appear without being escaped.
I still have \(apples + oranges) pieces of fruit.
"""
Run Code Online (Sandbox Code Playgroud)

但是,我复制了这个示例并粘贴在我的xcode playground中,它显示错误:

Playground execution failed: error: SwiftBasics.playground:9:19: error: 
unterminated string literal
let quotation = """
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么 ?

swift swift-playground

14
推荐指数
1
解决办法
4800
查看次数