简体   繁体   English

使用Xcode Playground的Swift代码中的EXC_I386_GPFLT

[英]EXC_I386_GPFLT in Swift code using Xcode Playground

I ran the same code in Xcode 7beta/rc Playground project and got an error: 我在Xcode 7beta / rc Playground项目中运行了相同的代码,但出现错误:

Execution was interrupted, reason: EXC_BAD_ACCESS(code=EXC_I386_GPFLT) 执行被中断,原因:EXC_BAD_ACCESS(code = EXC_I386_GPFLT)

in

let n: Int = Int(Process.arguments[1])!

How do I solve in Playground project since other solutions don't seem to be related? 由于其他解决方案似乎无关,我该如何在Playground项目中解决问题?

Binary tree: http://benchmarksgame.alioth.debian.org/u64q/program.php?test=binarytrees&lang=swift&id=1 二进制树: 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 holds the value that is passed as arguments for a command-line application. Process.arguments保留作为命令行应用程序的参数传递的值。

But you're using it in a Playground: there's no access to command line input from a Playground (they are Sandboxed), so Process.arguments is nil and your app crashes when you're doing Process.arguments[1] . 但是您在操场上使用它:无法从操场上访问命令行输入(它们是沙盒),因此Process.arguments为nil,并且当您执行Process.arguments[1]时,应用程序崩溃。

The solution is to use this in an actual application, not in a Playground. 解决方案是在实际的应用程序中而不是在游乐场中使用它。

You can use a custom "readLine()" function and a global input variable, each element in the input array is presenting a line: 您可以使用自定义“ 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 代码在Playground上运行良好; 同时产生EXC_I386_GPFLT错误 - Code runs fine on Playground; at the same time it yields an EXC_I386_GPFLT error 如何在swift编程中解决这个EXC_BAD_ACCESS(代码= EXC_i386_GPFLT) - How to solve this EXC_BAD_ACCESS(code=EXC_i386_GPFLT )in swift programming 快速崩溃:“线程 1:EXC_BAD_ACCESS(代码=EXC_I386_GPFLT)” - Swift Crashing: "Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)" iOS Swift:EXC_BAD_ACCESS(code = EXC_i386_GPFLT)关于单例 - iOS Swift : EXC_BAD_ACCESS(code=EXC_i386_GPFLT ) regarding a singleton 不了解如何修复线程1:ECX_BAD_ACCESS(代码= EXC_I386_GPFLT)(折线图Swift iOS) - Don't understand how to fix Thread 1: ECX_BAD_ACCESS (code = EXC_I386_GPFLT) (line chart swift iOS) 加载AVPlayer时出现错误线程1:EXC_BAD_ACCESS(code = EXC_I386_GPFLT) - Getting error Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT) when loading AVPlayer 在TableView中删除单元格时,exc_bad_access代码= exc_i386_gpflt - exc_bad_access code=exc_i386_gpflt when deleting cell in TableView 无法以编程方式设置UITableView单元格-线程1:EXC_BAD_ACCESS(代码= EXC_I386_GPFLT) - Unable to setup UITableView cell programatically - thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT) 错误:“线程 1:EXC_BAD_ACCESS(代码=EXC_I386_GPFLT) - error: "Thread 1: EXC_BAD_ACCESS(Code=EXC_I386_GPFLT) 为什么我得到线程 1:EXC_BAD_ACCESS(代码 = EXC_I386_GPFLT) - why do I get a Thread 1: EXC_BAD_ACCESS (code = EXC_I386_GPFLT)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM