Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to save data in a NSObject
    text
    copied!<p>I'm using Restkit to return a bunch of objects when a users presses log-in. I need to store these different objects and properties in a custom NSObject for reference throughout the app. </p> <p>I'm no genius at Objc, which is why I'll need an example and explanation of what I'm doing wrong. SO doesn't have any examples for custom NSObjects which is why I'm asking here. </p> <p>Thanks in advance, I'll be sure to accept the correct answer. </p> <p>In the loginViewController I have this to set the array in the custom NSObject:</p> <pre><code>Crap *crapper = [[Crap alloc] init]; [crapper setCrapArray:array]; </code></pre> <p>This the the Crap.h:</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @interface Crap : NSObject &lt;NSCoding&gt; @property (nonatomic, retain) NSArray *crapArray; - (void)encodeWithCoder:(NSCoder *)enCoder; - (id)initWithCoder:(NSCoder *)aDecoder; @end </code></pre> <p>And the Crap.m:</p> <pre><code>#import "Crap.h" @implementation Crap @synthesize crapArray; - (void)encodeWithCoder:(NSCoder *)encoder { //Encode properties, other class variables, etc [encoder encodeObject:self.crapArray forKey:@"crapArray"]; } - (id)initWithCoder:(NSCoder *)decoder { if((self = [super init])) { //decode properties, other class vars self.crapArray = [decoder decodeObjectForKey:@"crapArray"]; } return self; } @end </code></pre> <p>I'm using this to retrieve the data in the FeedViewController:</p> <pre><code>Crap *crap = [[Crap alloc] init]; NSLog(@"%d", crap.crapArray.count); </code></pre> <p>Which of course is 0 (array count is really 40 something). </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload