0

I am using an NSData to store an object for later use. It has quite a few NSStrings and I need to pull it out of an object.
For some reason, only some of the NSStrings are stored and some others get zeroed out! I was thinking it must be something with my code, that I must have forgotten to initialize some string, but for a very weird reason some of the strings lose the data! I can't get theImportantString to get it's relevant value because it first seems like the variable got it's value, but after coming back from Unarchive, it's equal to @""!

// CMyData.h
/////////////////////

@interface CMyData : NSObject <NSCoding>
{
    NSString *ID;
    NSString *DIST;
    .  
    .  
}
@property (nonatomic,retain) NSString *ID;
@property (nonatomic,retain) NSString *DIST;

@end

// CMyData.m
//////////////////////

#import "CMyData.h"
@implementation CMyData

@synthesize ID;
@synthesize DIST;

- (id)initWithCoder:(NSCoder *)decoder {
    if (self = [super init]) {
        self.ID = [decoder decodeObjectForKey:@"ID"];
        self.DIST = [decoder decodeObjectForKey:@"DIST"];
    .  
    .  
 }


- (void)encodeWithCoder:(NSCoder *)encoder {
    [encoder encodeObject:ID forKey:@"ID"];
    [encoder encodeObject:DIST forKey:@"DIST"];
    .  
    .  

}

- (void)dealloc {
    [ID release];
    [DIST release];
    [super dealloc];

}

@end

MyController.m

-(void) makeObject: (NSDictionary *)dict
{    
    CMyData* myData=[[CMyData alloc] init];
    myData.ID = [[NSString alloc] initWithString:[dict objectForKey:@"NAME"]];
    myData.DIST = [[NSString alloc] initWithString:[dict objectForKey:@"DISTRIBUTOR"]];
    .  
    .  

    myObject = [[[MYObject alloc] init];

    myObject.data = [NSKeyedArchiver archivedDataWithRootObject:myData];
} 

And then a tap on a button happens:

- (void) tapOnIcon: (MyObject*)theObject 
{
    CMyData *data = [NSKeyedUnarchiver unarchiveObjectWithData:theObject.data];
    [delegate showData:data];
}

in the delegate Controller (Where the value can't be set anymore):

delegateController.m
/////////////////////////////////

-(void) showData:(CMyData*)theData{
     self.theImportantString = [[NSString alloc] initWithString:theData.DIST];
     .
     .
     .


}
Ted
  • 3,805
  • 14
  • 56
  • 98
  • Why are you using `initWithString` when you can just use the (string) object itself? Also the use of identifiers `ID/DIST` and `NAME/DISTRIBUTOR` is confusing. – trojanfoe Oct 16 '12 at 12:08

1 Answers1

1

Seems you have types mismatch:

// in - (id)initWithCoder:(NSCoder *)decoder
self.DIST = [decoder decodeIntForKey:@"DIST"];

but in declaration you have

// in CMyData.h
NSString *DIST;

This should be:

// in - (id)initWithCoder:(NSCoder *)decoder
self.DIST = [NSString stringWithFormat:@"%d", [decoder decodeIntForKey:@"DIST"]];
MANIAK_dobrii
  • 6,014
  • 3
  • 28
  • 55
  • Ok than if you encode nsstring and decode an object this should work fine. – MANIAK_dobrii Oct 16 '12 at 12:13
  • Are you saying self.DIST should be something like: self.DIST = [[NSString alloc] initWithString:[decoder decodeObjectForKey:@"DIST"]]; ? – Ted Oct 16 '12 at 12:15
  • That if you'we encoded DIST as an int in `- (void)encodeWithCoder:(NSCoder *)encoder` with `encodeInt:forKey:`. But in your code here I see that you encode DIST as an object and then decode as an object, so it should work fine. – MANIAK_dobrii Oct 16 '12 at 12:21