[英]Generics and EXC_BAD_ACCESS in Swift
我正在使用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
協議(屬性順序已更改!)時,一切正常。
// 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 ()
最后我找到了解決方案! 在項目設置中,應將“ 整個模塊優化 ”設置為“ 是” (默認為“ 否” ),並且可以正常工作。 我發現有關優化的文章https://developer.apple.com/swift/blog/?id=27 ,它提供了以下信息:
“((...)如果啟用了“整體模塊優化”,則所有模塊都將同時編譯在一起(...)”
這就是為什么當我將所有文件放在一個文件中時不會出現問題。
如果沒有實際的崩潰堆棧和崩潰中涉及的代碼,則很難調試,但是您所描述的症狀強烈暗示您正在創建未定義的行為。 未定義的行為並不意味着您的程序崩潰。 這意味着任何事情都可能發生。 它可以工作。 它可以隨機覆蓋數據。 它可以跳到程序中的任意點。 可能會崩潰。 未定義是未定義。 我將“ Swift語言中的錯誤”放在可能的原因列表中。 Swift編譯器中當前存在許多錯誤; 但是你比它更有可能。
您應該審核程序崩潰時正在訪問的變量的使用情況。 您應該首先檢查要在GCD隊列上除main以外的其他任何位置進行數據突變。 如果您使用的是dispatch_async
或NSOperation
(尤其是如果您嘗試使用NSThread
或任何其他不應使用的純線程接口),則最可能出現這樣的錯誤。
(我注意到您給協議添加了I...
前綴,這通常是Javaism而不是很好的Swift風格。Swift在這種情況下使用...Type
,因此應為PersonType
。也可以稱為Nameable
或PersonNameable
專注於其提供的方法。您使用過Javaisms的事實表明您可能不熟悉GCD,因此尤其可疑。)
您應該仔細檢查是否使用了Unsafe...
類型(尤其是UnsafeMutablePointer
)。 這些肯定會以棘手的方式on入內存。 您應該仔細檢查任何使用!
(並避免使用)。 根據您的描述,這些問題不太可能成為問題,但是肯定會導致崩潰。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.