Issue with Swift 2.2 generics (Xcode 7.3)
I've got a frustrating situation with Swift 2.2 (Xcode 7.3). To simulate it, just create a variable in a user defined, generic class, and reference that class from someplace else. For example:
class A<T> {
let genVar = 1
}
class MyViewController: UIViewController {
let myVar = A<Int>() // crash is here
}
If you will run this code on a device running iOS 7 (iPhone 4, in my case), it will crash on the attempt of creating a variable of generic type. Here are the first lines of the device crash log:
Exception Type: EXC_BAD_ACCESS (SIGBUS)
Exception Subtype: KERN_PROTECTION_FAILURE at 0x00298910
Triggered by Thread: 0
Thread 0 Crashed:
0 libswiftCore.dylib 0x006b1d64 0x4bd000 + 2051428
1 Phone 0x001c76ec 0xab000 + 1165036
2 libswiftCore.dylib 0x006b307c 0x4bd000 + 2056316
3 libswiftCore.dylib 0x006b2f70 0x4bd000 + 2056048
4 libswiftCore.dylib 0x006b0f24 0x4bd000 + 2047780
5 libswiftCore.dylib 0x006b107c 0x4bd000 + 2048124
6 Phone 0x0014e730 0xab000 + 669488
7 Phone 0x00129390 0xab000 + 517008
8 UIKit 0x31e9d9c4 -[UIClassSwapper initWithCoder:] + 188
On iOS 8 and 9 simulators/devices, the code above works okay.
Is Swift support for iOS 7 going to be dropped in the near future?
I've been tripped up by what appears to be two bugs with Swift generics in iOS 7. Here's how to fix them.
Bug #1 - you must define a generic as the first property in your class before any others.
Example - this code fails:
public class TestIOS7<T> {
private var x: Int?
}
let x = TestIOS7<String>()
But here's the workaround:
public class TestIOS7<T> {
private var kludge: T?
private var x: Int?
}
let x = TestIOS7<String>()
Bug #2: Class constraints seem completely broken.
Example - this code fails:
class ClassA<B: ClassB> { }
class ClassB { }
let x = ClassA <String>()
I have not found any workaround aside from removing the "ClassB" constraint and rewriting all the code to deal with the fact that basically, this language feature no longer exists. This is particular painful when you need to call an initializer of ClassB from ClassA - I had to rewrite that block with hardwired if/then/else for all my ClassB subclasses until Apple fixes this.
If someone does find a workaround to Bug #2 please let me know!
As this bug suggests, you have following possible workarounds:
value
property object
and value
properties declarations init
Probably the best workaround is to initialize object in init
:
class A<T> {
let genVar: Int
init() {
genVar = 1
}
}
class MyViewController: UIViewController {
let myVar = A<Int>() // no crash
}
We faced the same issue.
A work-around was discovered though that would at least let us support iOS7 without a significant rewrite.
It looks like all subclasses must also be generic so that the parent generic works correctly in iOS7.
The subclass's generic type does not matter and it does not need to be the parent's generic type.
I also posted this answer on the bug discussion simpleBob linked.
Example:
// This heavily pollutes the codebase, so let's keep track of it but using a common meaningless generic value so we can find and destroy later when we no longer support iOS7
protocol iOS7SwiftGenericFixProtocol {}
struct iOS7SwiftGenericFixType: iOS7SwiftGenericFixProtocol {}
class GenericClass<T> {
var value: T?
let attribute = 0
}
class GenericSubclass<Element: iOS7SwiftGenericFixProtocol>: GenericClass<Int> {
let otherAttribute = 0
}
let _ = GenericSubclass<iOS7SwiftGenericFixType>()
链接地址: http://www.djcxy.com/p/90990.html
上一篇: 在pip安装软件包时禁用警告