Xcode 7 beta swift应用程序在运行时崩溃,出现EXC_坏_访问错误
我有以下代码崩溃Xcode 7 beta swift应用程序在运行时崩溃,出现EXC_坏_访问错误,xcode,swift,Xcode,Swift,我有以下代码崩溃 class PersonFactory { class var build: PersonInterface { get { return Person() } } } class Person: PersonInterface { var age: Int = 0 func say(message: String) { print(message) } } protocol PersonInterface: cla
class PersonFactory {
class var build: PersonInterface {
get {
return Person()
}
}
}
class Person: PersonInterface {
var age: Int = 0
func say(message: String) {
print(message)
}
}
protocol PersonInterface: class {
var age: Int { get set }
func say(message: String)
}
当我从中运行以下行时,它崩溃:
在下一行var age:Int=0
上使用EXC\u BAD\u访问时崩溃。问题出现在Xcode 7 beta 4(7A165t)中
请注意,只有在从应用程序调用代码时,代码才会在操场上崩溃。以下是演示应用程序:
注意:定义PersonFactory、Person和PersonInterface的代码应位于一个单独的文件中,以使其崩溃
看起来像一个Swift 2.0 bug?我向苹果公司提交了一份缺陷报告
更新
这个问题已经在Xcode 7.0 beta 5中修复。很可能是Bug,因为我使用的是Xcode 6.3.2,它看起来很好 您可以通过以下操作避免此崩溃:
import Foundation
class PersonFactory<T: PersonInterface> {
class var build: T {
return T()
}
}
class Person: PersonInterface {
var age: Int
required init() {
age = 0
}
func say(message: String) {
print(message)
}
}
protocol PersonInterface {
init()
var age: Int { get set }
func say(message: String)
}
let person: Person = PersonFactory.build
person.say("hello")
<代码>导入基础
阶级人格工厂{
类var构建:T{
返回T()
}
}
类人:PersonInterface{
变量年龄:Int
必需的init(){
年龄=0
}
func say(消息:字符串){
打印(信息)
}
}
协议人界面{
init()
变量年龄:Int{get set}
func say(消息:字符串)
}
让person:person=PersonFactory.build
人。说(“你好”)
谢谢,我的代码在操场上运行时没有问题,在Xcode 7中也没有问题,但在iOS应用程序中崩溃。在模拟器中运行时,有什么东西没有初始化吗?如果有任何iboutlet,有时剩余链接会抛出一个exc\u bad\u访问
。它们是所有错误中最烦人的。很好,我试着用这些代码创建一个新的应用程序来隔离这种情况,这里是视图控制器,顺便问一下,我知道数组的长形是数组
,但是在Swift中,类声明之后是一个泛型,将代码放在与视图控制器相同的源文件中。
import Foundation
class PersonFactory<T: PersonInterface> {
class var build: T {
return T()
}
}
class Person: PersonInterface {
var age: Int
required init() {
age = 0
}
func say(message: String) {
print(message)
}
}
protocol PersonInterface {
init()
var age: Int { get set }
func say(message: String)
}
let person: Person = PersonFactory.build
person.say("hello")