ios - Any way to store struct in NSCache -


is there way store struct values in nscache ? when read apple documentation can store anyobject it. there couple of work around 1 convert sturct class, second convert sturct values dictionary expensive operation if dataset big. suggestion?

i'm going chance arm on 'no' (without workarounds). nscache resoundingly on over objective-c side of runtime, written work nsobjects, bridged via anyobject. unlike nsdictionary , nsarray there no equivalent swift collection compiler can bridge between.

implementation points aside: nscache doesn't live in world understands value semantics more sophisticated c atoms.

that being said, easiest workaround create object container struct, making bridging explicit owned whomever wants use cache:

class yourstructholder: nsobject {     let thing: yourstruct     init(thing: yourstruct) {         self.thing = thing     } }  cache.setobject(yourstructholder(thing: thing), forkey:"whatever") (cache.objectforkey("whatever") as? yourstructholder)?.thing 

... or skip init , use var ...: yourstruct? if you're happy mutability. you're going have deal optionality when talking cache anyway.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -