Swift iOS NSDictionary setValue crash - but why?

I have this code (porting from another language, therefore, several different naming conventions, but please do not forget about it now)

var FDefaultsList: NSDictionary = [String:String]();
let TmpKey: String = TmpKeyValue[0];
let TmpValue: String = TmpKeyValue[1];    
if (TmpKey != "") && (TmpValue != "") {
  //let TmpAnyObjectValue: AnyObject? = TmpValue;
  //FDefaultsList.setValue(TmpAnyObjectValue, forKey: TmpKey);
  FDefaultsList.setValue(TmpValue, forKey: TmpKey);
}

However, no matter which setValue variable I use, the setValue call throws an error (it makes no sense, as far as I can tell) and leaves the application (the Xcode editor is transferred to the AppDelegate class : UIResponder, UIApplicationDelegate )

Am I assuming I'm using NSDictionary incorrectly? I am trying to read in a text file where each line is key = value lines

+4
source share
1 answer

NSMutableDictionary, NSDictionary.

, , setValue ( setObject):

var FDefaultsList = NSMutableDictionary()
let TmpKey: String = "a"
let TmpValue: String = "b"
if TmpKey != "" && TmpValue != "" {
    FDefaultsList[TmpValue] = TmpKey
}

"Swifty" :

var defaultsList = [String:String]()
let tmpKey = "a"
let tmpValue = "b"
if !tmpKey.isEmpty && !tmpValue.isEmpty {
    defaultsList[tmpValue] = tmpKey
}
+5

Source: https://habr.com/ru/post/1626373/


All Articles