【问题标题】:How to make Playground execution time is as fast as if we run in iOS application如何让 Playground 的执行时间就像我们在 iOS 应用程序中运行一样快
【发布时间】:2015-06-16 14:47:29
【问题描述】:
我发现游乐场的执行速度并不可靠。以代码为例:
import UIKit
var count = 0;
let startTime = NSDate()
for i in 1...10000 {
count++
}
let endTime = NSDate()
let interval = endTime.timeIntervalSinceDate(startTime)
interval的值在2s左右,不靠谱。
随着 Swift 2.0 和 XCode beta 7 的发布,是否有可能让 Swift Playground 代码像在 iOS 应用程序中一样快速执行?
【问题讨论】:
标签:
ios
swift
swift2
xcode7
【解决方案1】:
感谢 Playground 的 Sources 文件夹,有一个解决方法。
您可以使用菜单添加外部文件:
新建 > 将文件添加到源
或进入菜单:
查看 > 导航器 > 显示项目导航器
并将.swift 文件放到Sources 文件夹中。
为了便于访问,您在此文件夹中的代码必须是公开的:
public class PlayGround {
public class func count() {
var count = 0
for i in 1...10000 {
count++
}
}
}
那么在操场本身就和往常一样:
let startTime = NSDate()
PlayGround.count()
let endTime = NSDate()
let interval = endTime.timeIntervalSinceDate(startTime) // 0.0062