Last active
August 29, 2015 14:18
-
-
Save barnybug/cc7b717387474bea18ac to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Bug demonstrating Release crash in swift code (XCode 6.2 / swift 1.1, Release builds only) | |
import UIKit | |
import XCTest | |
func dictSetValue(value: AnyObject, forKey key: String, inout #dictionary: [String : AnyObject]) { | |
dictSetValue(value, forKeyPathComponents: key.componentsSeparatedByString("."), dictionary: &dictionary) | |
} | |
func dictSetValue(value: AnyObject, forKeyPathComponents components: [String], inout #dictionary: [String : AnyObject]) { | |
if components.isEmpty { | |
return | |
} | |
let head = components.first! | |
if components.count == 1 { | |
dictionary[head] = value | |
} else { | |
var child = (dictionary[head] as? [String : AnyObject]) ?? [:] | |
let tail = Array(components[1..<components.count]) | |
dictSetValue(value, forKeyPathComponents: tail, dictionary: &child) | |
return dictionary[head] = child // <- crash here | |
} | |
} | |
class SwiftObjectMapperBugTests: XCTestCase { | |
func testCrash() { | |
var dict: [String: AnyObject] = [:] | |
dictSetValue(1, forKey: "a.b", dictionary: &dict) | |
dictSetValue(2, forKey: "a.c", dictionary: &dict) // <- crash | |
XCTAssert(true, "crashes before here") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment