ios - NSJSONSerialization creates garbage -


i have problems creating nsstring-representation (json-string) of nsdictionary using nsjsonserialization. have used jsonkit before, since kind of deprecated in ios9 (and crashes), switched nsjsonserialization.

this code:

// `usersettings` of type nsmutabledictionary*  nsdata* data= [nsjsonserialization datawithjsonobject:usersettings options:0 error:&error]; nsstring* settingsstring= [nsstring stringwithutf8string:data.bytes];  currentuser.settings= settingsstring;   // nsstring* property 

now, time time, code works, settingsstring nil. , when inspect data object in debugger, bytes property shows json-string followed random garbage, this:

1 = 0x00007ff1ba814000 "{\"residua ... lculateplanned\":\"0\",\"wizarduserid\":\"\"}uitextcolor\x91uisystemcolorname\x94uibaselineadjustment\x8cuisystemfont\x89ns.intval\x8euishadowoffset\x94uiautoresizesubviews\x8duicontentmode\x85nsrgb\x8auifontname\x8buitextlabel\x8ensinlinedvalue\x91uidetailtextlabel\x99uiuserinteractiondisabled\x9duitablecellbackgroundcolorset\x94uinibencoderemptykey\x87nswhite\x8cnscolorspace\x8fuitextalignment\xa3uinibaccessibilityconfigurationskey\x92uiautoresizingmask\x99uiproxiedobjectidentifier\x87uialpha\x87uiwhite\x9auifontdescriptorattributes\x8cuifonttraits\x86nssize\x95uicolorcomponentcount\x91uiminimumfontsize\x86uitext\x96uimultipletouchenabled\x8duidestination\x94uimi..."                                              ^ start of garbage after end of dictionary 

what doing wrong?

do not use + stringwithutf8string:, relies on a null-terminated c array of bytes , there null terminator chance , may after end of charactery expect.

instead use:

- (instancetype)initwithdata:(nsdata *)data encoding:(nsstringencoding)encoding 

ex:

nsstring *settingsstring = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding]; 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

python - malformed header from script index.py Bad header -