Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your header Crap file should be</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @interface Crap : NSObject &lt;NSCoding&gt; @property (nonatomic, retain) NSArray *crapArray; + (Crap *) crap; - (void)encodeWithCoder:(NSCoder *)enCoder; - (id)initWithCoder:(NSCoder *)aDecoder; @end </code></pre> <p>Your implementation Crap file should be</p> <pre><code>#import "Crap.h" @implementation Crap @synthesize crapArray; + (Crap *) crap { static Crap *crap = nil; if (!crap) { crap = [[super allocWithZone:nil] init]; } return crap; } + (id) allocWithZone:(struct _NSZone *)zone { return [self crap]; } - (id) init { self = [super init]; if (self) { // set default property values crapArray = nil; } return self; } - (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>Then to set the value you got to do something like</p> <pre><code>[Crap crap].crapArray = [NSArray arrayWithObjects:@"Hello", @"Bye", nil]; </code></pre> <p>And to get that value from another view controller you got to do something like</p> <pre><code>NSLog(@"Value: %d", [[Crap crap] crapArray].count); </code></pre> <p>If you do</p> <pre><code>Crap *crap = [[Crap alloc] init]; NSLog(@"%d", crap.crapArray.count); </code></pre> <p>crapArray will be set to nil and you are going to get 0 again, because we are setting crapArray to nil on the init method.</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