2016-12-16 22 views
2

我已經OBJ-C代碼的setObject相當於斯威夫特

[dictionary setObject:[[notification object] objectForKey:@"key"] forKey:@"anotherKey"]; 

我怎麼能翻譯這迅速?通知NSNotification對象

我試圖

dictionary.setValue(notification.valueForKey("key"), forKey: "anotherKey") 

,但應用程序崩潰,錯誤(對不起,圖片,粘貼不工作)

enter image description here

+0

'[通知對象]'沒有被翻譯:'notification.object'。所以'... notification.object.valueForKey(「key」)'? – Larme

回答

3

純夫特當量是

dictionary["anotherKey"] = notification.object?["key"] 

如果notification.object或鍵key不存在沒有對象將被分配。

注意

切勿使用valueForKey/setValue:forKey:,除非你有你爲什麼使用KVC一個獨特的想法。

1

嘗試,這可能是它可以幫助你:

dictionary .setValue(notification.object["key"], forKey: "anotherKey") 
+0

除非你可以解釋你明確使用KVC的原因,否則不要**使用'setValue:forKey'。 – vadian

1

如何使你的字典var(未let)和這樣做:

dictionary["anotherKey"] = [[notification object] objectForKey:@"key"] 
0
dictionary["anotherKey"] = (notification.object!["key"] as! String)