我正在使用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) 我已经做了大量的网络搜索,但我现在正试图在Playground中使用"Live Photos".我知道这个框架(PHLivePhoto),我不知道如果在Playground中与他们合作是可能的,因为没有太多的"导入",因为似乎没有任何"实时照片"可用在线下载.有任何想法吗?
我在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 Package 导入到我工作区中的 Playground 吗?我已经阅读了答案,但我认为它已经过时了,因为现在我们可以在 iOS 项目中使用 Swift Packages。
我正在玩Xcode 6的第一个测试版的Playground功能 - 我注意到Playground没有更新的一半时间(根本不显示结果计算或发生了多少循环迭代)简单的代码/循环/函数在那里.甚至是Swift Tour https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/GuidedTour.html
有几行代码没有显示在Playground中.如果你搞乱代码,它有时会显示,通过移动代码或将其放在其他地方.还有谁?任何修复?这只是一个测试版问题吗?
我想在Xcode 6.0.1中使用一个游乐场.所以文件 - >新 - >游乐场
我立刻得到了错误.游乐场执行终止,因为游乐场进程意外退出.见图.重启Xcode没有帮助.
Xcode有什么问题?
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)
示例图片:

我在代码中编写接口布局.因为每次我逐个像素地测试字体大小或视图布局时重新加载应用程序很烦人,所以我开始在操场上进行.这确实有很大帮助.但我确实想念那里的自定义字体.
有没有办法在游乐场添加自定义字体?
我是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-playground ×10
swift ×9
xcode ×4
ios ×3
xcode6 ×2
foundation ×1
ipad ×1
phlivephoto ×1
swift2 ×1
uikit ×1
xcode7 ×1