Domanda

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.

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.

Thanks in advance, I'll be sure to accept the correct answer.

In the loginViewController I have this to set the array in the custom NSObject:

Crap *crapper = [[Crap alloc] init];
        [crapper setCrapArray:array];

This the the Crap.h:

#import <Foundation/Foundation.h>

@interface Crap : NSObject <NSCoding> 

@property (nonatomic, retain) NSArray *crapArray;

- (void)encodeWithCoder:(NSCoder *)enCoder;
- (id)initWithCoder:(NSCoder *)aDecoder;

@end

And the Crap.m:

#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

I'm using this to retrieve the data in the FeedViewController:

Crap *crap = [[Crap alloc] init];
NSLog(@"%d", crap.crapArray.count);

Which of course is 0 (array count is really 40 something).

È stato utile?

Soluzione

Your header Crap file should be

#import <Foundation/Foundation.h>

@interface Crap : NSObject <NSCoding>

@property (nonatomic, retain) NSArray *crapArray;

+ (Crap *) crap;

- (void)encodeWithCoder:(NSCoder *)enCoder;
- (id)initWithCoder:(NSCoder *)aDecoder;

@end

Your implementation Crap file should be

#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

Then to set the value you got to do something like

[Crap crap].crapArray = [NSArray arrayWithObjects:@"Hello", @"Bye", nil];

And to get that value from another view controller you got to do something like

NSLog(@"Value: %d", [[Crap crap] crapArray].count);

If you do

Crap *crap = [[Crap alloc] init];
NSLog(@"%d", crap.crapArray.count);

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.

Altri suggerimenti

startObject.h

#import <Foundation/Foundation.h>

@interface startObject : NSObject

@property (nonatomic, retain) NSArray *crapArray;

@end

startObject.m

import "startObject.h"

@implementation startObject

@synthesize crapArray;


- (id)init {

    self = [super init];

    return self;
}
@end

Use:

startObject* start = [[startObject alloc] init];

start.crapArray = [NSArray arrayWithObjects:@"Alpha", @"Beta", @"Gamma", @"Omega", nil];

NSLog(@"%@",start.crapArray);

Result:

(
Alpha,
Beta,
Gamma,
Omega
)

The above code is used to save data to an NSObject. That's it. To release it's contents, you simply set start = nil.

But if you are looking to save data to disk using Archiving, let me know. Also look here for a detailed tutorial on using NSCoding.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top