我正在研究Swift项目(XCode 6.3.1)。在运行时我获得EXC_BAD_ACCESS
(我在模拟器iPhone 6上测试,但是当我使用单元测试类时,我也得到了这个错误):
这显示了我的代码结构:
// File IPerson.swift
public protocol IPerson {
var firstName : String? { get set }
var secondName : String? { get set }
}
// File PersonHelper.swift
public class PersonHelper<T:IPerson> {
public init(){
}
public func doSomething(person : T) -> Int {
var secondName = person.secondName;
//...
return 0;
}
}
// File SamplePerson.swift
public class SamplePerson: IPerson {
public init() {
}
public var firstName : String?;
public var secondName : String?;
}
// In some viewController (or in test class)
…
var helper = PersonHelper<SamplePerson>();
var person = SamplePerson();
var result = helper.doSomething(person)
…
当我使用此版本的IPerson
协议(更改属性的顺序!)时,evrything就可以了。
// File IPerson.swift
public protocol IPerson {
var secondName : String? { get set }
var firstName : String? { get set }
}
这种行为对我来说很奇怪 - 协议中的属性顺序很重要?
当我使用非泛型PersonHelper
时,一切都还可以,但我需要泛型类。
当我将所有协议和类放在一个文件中时(从我执行doSomething
方法 - viewController或testclass)也一切正常。
我的代码中有什么问题,或者这是Swift语言中的错误?
堆栈追踪:
#0 0x00007f8d4840e350 in 0x7f8d4840e350 ()
#1 0x000000010449e253 in protocol witness for SampleProject.IPerson.firstName.materializeForSet : Swift.Optional<Swift.String> in conformance SampleProject.SamplePerson : SampleProject.IPerson in SampleProject at /Users/bjarko/Documents/XCode Projects/SampleProject/SampleProject/SamplePerson.swift:20
#2 0x000000010449e53e in SampleProject.PersonHelper.doSomething <A : SampleProject.IPerson>(SampleProject.PersonHelper<A>)(A) -> Swift.Int at /Users/bjarko/Documents/XCode Projects/SampleProject/SampleProject/Helper.swift:16
#3 0x0000000104499d35 in SampleProject.ViewController.viewDidLoad (SampleProject.ViewController)() -> () at /Users/bjarko/Documents/XCode Projects/SampleProject/SampleProject/ViewController.swift:18
#4 0x0000000104499dc2 in @objc SampleProject.ViewController.viewDidLoad (SampleProject.ViewController)() -> () ()
#5 0x0000000105058210 in -[UIViewController loadViewIfRequired] ()
#6 0x000000010505840e in -[UIViewController view] ()
#7 0x0000000104f732c9 in -[UIWindow addRootViewControllerViewIfPossible] ()
#8 0x0000000104f7368f in -[UIWindow _setHidden:forced:] ()
#9 0x0000000104f7fe21 in -[UIWindow makeKeyAndVisible] ()
#10 0x0000000104f23457 in -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] ()
#11 0x0000000104f261de in -[UIApplication _runWithMainScene:transitionContext:completion:] ()
#12 0x0000000104f250d5 in -[UIApplication workspaceDidEndTransaction:] ()
#13 0x0000000107d035e5 in __31-[FBSSerialQueue performAsync:]_block_invoke_2 ()
#14 0x00000001045bc41c in __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ ()
#15 0x00000001045b2165 in __CFRunLoopDoBlocks ()
#16 0x00000001045b1f25 in __CFRunLoopRun ()
#17 0x00000001045b1366 in CFRunLoopRunSpecific ()
#18 0x0000000104f24b42 in -[UIApplication _run] ()
#19 0x0000000104f27900 in UIApplicationMain ()
#20 0x000000010449d777 in main at /Users/bjarko/Documents/XCode Projects/SampleProject/SampleProject/AppDelegate.swift:12
#21 0x000000010694b145 in start ()
#22 0x000000010694b145 in start ()
答案 0 :(得分:5)
最后我找到了解决方案!在项目设置中,应在是(默认为否)上设置整个模块优化,并且它可以正常工作。 我找到了关于优化的文章https://developer.apple.com/swift/blog/?id=27,这提供了以下信息:
“(...)如果启用了整个模块优化,则编译所有模块 同时在一起(...)“
这就是为什么当我把所有文件放在一个文件中时没有出现问题。