Skip to content

Instantly share code, notes, and snippets.

@crimsonwoods
Created February 3, 2016 03:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crimsonwoods/ea204eb3cd04560849fc to your computer and use it in GitHub Desktop.
Save crimsonwoods/ea204eb3cd04560849fc to your computer and use it in GitHub Desktop.
Xcode 7.2で謎の現象が起きるコード
//
// このコードをXcode 7.2(7.2.1ではない)でDeployment Targetを7でビルドして、
// iOS7.1.2のデバイスで実行するとfatalErrorが発生する。
//
import UIKit
class Logger {
func put(message: String) {
print(message)
}
}
class Broken<T> {
private var logger: Logger! // この行とこの下の行を入れ替えるとfatalErrorが発生しない
private var data: T!
func set(data: T!) {
dispatch_async(dispatch_get_main_queue()) {
self.data = data
self.logger?.put("set: \(data)")
self.logger = nil // この行を削除してもfatalErrorが発生しない
}
}
func get(completion: (T!) -> Void) {
dispatch_async(dispatch_get_main_queue()) {
self.logger?.put("get: \(self.data)")
completion(self.data)
}
}
}
class ViewController: UIViewController {
private var x: Broken<[Int]>!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.x = Broken<[Int]>()
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) {
self.x.set([0, 1, 2])
}
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(100 * NSEC_PER_MSEC))
dispatch_after(time, dispatch_get_main_queue()) {
self.x.get({
if $0 == nil {
fatalError("supplied value must not be a nil value.")
}
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment