Swift iOS NSDictionary setValue crash - but why? -
i have code (porting language, hence bit different naming conventions, please bear 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 setvalue variation use, call setvalue throws error (not meaningful far can tell) , exits app (xcode editor taken class appdelegate: uiresponder, uiapplicationdelegate)
i guess using nsdictionary wrong? trying read in text file each line key=value strings
you should declare actual nsmutabledictionary instead of casting nsdictionary.
and can use subscript bit simpler use setvalue
(which should setobject
):
var fdefaultslist = nsmutabledictionary() let tmpkey: string = "a" let tmpvalue: string = "b" if tmpkey != "" && tmpvalue != "" { fdefaultslist[tmpvalue] = tmpkey }
a more "swifty" version be:
var defaultslist = [string:string]() let tmpkey = "a" let tmpvalue = "b" if !tmpkey.isempty && !tmpvalue.isempty { defaultslist[tmpvalue] = tmpkey }
Comments
Post a Comment