Created
February 3, 2016 03:35
-
-
Save crimsonwoods/ea204eb3cd04560849fc to your computer and use it in GitHub Desktop.
Xcode 7.2で謎の現象が起きるコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// このコードを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