2014-10-21 3 views
1

놀이터에서이 코드가 작동 대 운동장은빠른 빈 사전 사전 버그? 프로젝트

var detaildata:Dictionary=[:] 
detaildata = ["apple":"hello"] 
detaildata["orange"]="byebye" 

이 코드는

class ViewController: UIViewController{ 
    var detaildata:Dictionary=[:] 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     detaildata = ["apple":"hello"] 
     detaildata["orange"]="byebye" // Error -> 'Dictionary' is not identical to 'Dictionary<key,Value>' 
    } 
} 

그 이유를 알고 프로젝트

에서 작동하지 않습니다?

동일한 코드라고 생각합니다.

답변

0

그 코드는 내 놀이터에서 작동하지 않으며 귀하의 컴퓨터에서 작동하지 않아야합니다. Dictionary을 선언 할 때는 명시 적으로나 유형 유추를 통해 키 유형과 값 유형을 모두 제공해야합니다. 이 모든 작동합니다

var dict1 = ["apple": "hello"]  // inferred 
var dict2: [String: String] = [:] // explicit 
var dict3: Dictionary<String, String> = ["apple": "hello"] // longest version 

이 중 어느 것도 작동합니다

var dict3: Dictionary = [:]   // type inference impossible 
var dict4 = [:]      // same 
+0

을 좀 더 키/베일 dact1 + = 추가하려면 [ "오렌지"를 "바이 바이"] // 오류 내가 할 수있는 일을 잘 모르겠다 두 개의 사전을 병합하고 싶습니다 dict4 = dict1 + dict2 // 오류 PLZ를 도와 줄 수 있습니까? – user1272854

+0

Apple의 [Swift 사전에 대한 설명서] (https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html#//apple_ref/doc/uid/TP40014097-CH8)를 읽어야합니다. -XID_179) - 귀하의 모든 질문을 다루어야합니다. –