EXC_I386_GPFLT 在 Swift 代码中使用 Xcode 游乐场
EXC_I386_GPFLT in Swift code using Xcode Playground
我 运行 在 Xcode 7beta/rc Playground 项目中使用相同的代码,但出现错误:
Execution was interrupted, reason: EXC_BAD_ACCESS(code=EXC_I386_GPFLT)
在
let n: Int = Int(Process.arguments[1])!
Playground项目中其他解决方案似乎不相关,如何解决?
二叉树:http://benchmarksgame.alioth.debian.org/u64q/program.php?test=binarytrees&lang=swift&id=1
class TreeNode {
var left, right : TreeNode?
var item : Int
init(_ left: TreeNode?, _ right: TreeNode?, _ item: Int) {
self.left = left
self.right = right
self.item = item
}
func check() -> Int {
guard let left = left, let right = right else {
return item
}
return item + left.check() - right.check()
}
}
func bottomUpTree(item: Int, _ depth: Int) -> TreeNode {
if depth > 0 {
return
TreeNode(
bottomUpTree(2*item-1, depth-1),
bottomUpTree(2*item, depth-1),
item
)
}
else {
return
TreeNode(nil,nil,item)
}
}
let n: Int = Int(Process.arguments[1])!
let minDepth = 4
let maxDepth = n
let stretchDepth = n + 1
let check = bottomUpTree(0,stretchDepth).check()
print("stretch tree of depth \(stretchDepth)\t check: \(check)")
let longLivedTree = bottomUpTree(0,maxDepth)
var depth = minDepth
while depth <= maxDepth {
let iterations = 1 << (maxDepth - depth + minDepth)
var check = 0
for i in 0..<iterations {
check += bottomUpTree(i,depth).check()
check += bottomUpTree(-i,depth).check()
}
print("\(iterations*2)\t trees of depth \(depth)\t check: \(check)")
depth += 2
}
print("long lived tree of depth \(maxDepth)\t check: \(longLivedTree.check())")
Process.arguments
保存作为参数传递给命令行应用程序的值。
但是您是在 Playground 中使用它:无法从 Playground 访问命令行输入(它们是沙盒化的),因此 Process.arguments
为 nil 并且您的应用程序在您执行 Process.arguments[1]
.
解决方案是在实际应用程序中使用它,而不是在 Playground 中。
您可以使用自定义 "readLine()" 函数和全局输入变量,输入数组中的每个元素都显示一行:
import Foundation
var currentLine = 0
let input = ["5", "5 6 3"]
func readLine() -> String? {
if currentLine < input.endIndex {
let line = input[currentLine]
currentLine += 1
return line
} else {
return nil
}
}
let firstLine = readLine() // 5
let secondLine = readLine() // 5 6 3
let thirdLine = readLine() // nil
我 运行 在 Xcode 7beta/rc Playground 项目中使用相同的代码,但出现错误:
Execution was interrupted, reason: EXC_BAD_ACCESS(code=EXC_I386_GPFLT)
在
let n: Int = Int(Process.arguments[1])!
Playground项目中其他解决方案似乎不相关,如何解决?
二叉树:http://benchmarksgame.alioth.debian.org/u64q/program.php?test=binarytrees&lang=swift&id=1
class TreeNode {
var left, right : TreeNode?
var item : Int
init(_ left: TreeNode?, _ right: TreeNode?, _ item: Int) {
self.left = left
self.right = right
self.item = item
}
func check() -> Int {
guard let left = left, let right = right else {
return item
}
return item + left.check() - right.check()
}
}
func bottomUpTree(item: Int, _ depth: Int) -> TreeNode {
if depth > 0 {
return
TreeNode(
bottomUpTree(2*item-1, depth-1),
bottomUpTree(2*item, depth-1),
item
)
}
else {
return
TreeNode(nil,nil,item)
}
}
let n: Int = Int(Process.arguments[1])!
let minDepth = 4
let maxDepth = n
let stretchDepth = n + 1
let check = bottomUpTree(0,stretchDepth).check()
print("stretch tree of depth \(stretchDepth)\t check: \(check)")
let longLivedTree = bottomUpTree(0,maxDepth)
var depth = minDepth
while depth <= maxDepth {
let iterations = 1 << (maxDepth - depth + minDepth)
var check = 0
for i in 0..<iterations {
check += bottomUpTree(i,depth).check()
check += bottomUpTree(-i,depth).check()
}
print("\(iterations*2)\t trees of depth \(depth)\t check: \(check)")
depth += 2
}
print("long lived tree of depth \(maxDepth)\t check: \(longLivedTree.check())")
Process.arguments
保存作为参数传递给命令行应用程序的值。
但是您是在 Playground 中使用它:无法从 Playground 访问命令行输入(它们是沙盒化的),因此 Process.arguments
为 nil 并且您的应用程序在您执行 Process.arguments[1]
.
解决方案是在实际应用程序中使用它,而不是在 Playground 中。
您可以使用自定义 "readLine()" 函数和全局输入变量,输入数组中的每个元素都显示一行:
import Foundation
var currentLine = 0
let input = ["5", "5 6 3"]
func readLine() -> String? {
if currentLine < input.endIndex {
let line = input[currentLine]
currentLine += 1
return line
} else {
return nil
}
}
let firstLine = readLine() // 5
let secondLine = readLine() // 5 6 3
let thirdLine = readLine() // nil