McCadi
McCadi

Reputation: 229

NSUserDefaults returning nil for some strings in array

I have a custom Object "Woman". I am trying to store the following values in it and save it in a mutable array using NSUserDefaults. The code I use for such is below. I also am using NSCoding in the object.

       if (women ==nil) {

                     women =[[NSMutableArray alloc] init];
                 }
                 NSString *string =[NSString stringWithFormat:@"%f", interval];
                 //store to woman object
                 Woman* woman = [[Woman alloc] initWithFull:nameOfGirl withdate2:perfectdate withintervalLength:string  withperiodLength:[NSString stringWithFormat:@"432000"] withpmsLength:[NSString stringWithFormat:@"432000"]];

                 [women addObject:woman];


               [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:women] forKey:@"women"];

I use this code to retrieve it:

//pull women from archive
NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
NSData *dataRepresentingSavedArray = [currentDefaults valueForKey:@"women"];
if (dataRepresentingSavedArray != nil)
{
    NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
    if (oldSavedArray != nil)
        women = [[NSMutableArray alloc] initWithArray:oldSavedArray];
    else
        women = [[NSMutableArray alloc] init];
}

The result is in the screenshot. What is interesting to me is that the first string makes it but the other ones don't. :The first string is correctly retrieved, and the NSDate, however the last three arents?

EDIT: Here is my custom class.

.h:

#import <Foundation/Foundation.h>

@interface Woman : NSObject <NSCoding>

@property (nonatomic,strong) NSString *girlname;
@property (nonatomic,strong) NSDate *date2;
@property (nonatomic,strong) NSString *intervalLength;
@property (nonatomic,strong) NSString *periodLength;
@property (nonatomic, strong) NSString *pmsLength;

- (id)initWithFull:(NSString *)girlname withdate2:(NSDate *)date2 withintervalLength:(NSString *)intervalLength withperiodLength:(NSString *)periodLength withpmsLength:(NSString *)pmsLength;

- (id)initWithNoInterval:(NSString *)girlname withdate2:(NSDate *)date2 withperiodLength:(NSString *)periodLength withpmsLength:(NSString *)pmsLength;

- (id)initWithIntervalnoPMSPeriod:(NSString *)girlname withdate2:(NSDate *)date2 withintervalLength:(NSString *)intervalLength;

- (void) encodeWithCoder:(NSCoder*)encode;
- (id) initWithCoder:(NSCoder*)decode;


@end

And the .m:

 #import "Woman.h"

    @implementation Woman

    -(id)initWithFull:(NSString *)girlname withdate2:(NSDate *)date2 withintervalLength:(NSString *)intervalLength withperiodLength:(NSString *)periodLength withpmsLength:(NSString *)pmsLength {


        self = [super init];

        self.girlname = girlname;
        self.date2 = date2;
        self.intervalLength = intervalLength;
        self.pmsLength = pmsLength;
        self.periodLength = periodLength;
        return self;



    }

    -(id)initWithIntervalnoPMSPeriod:(NSString *)girlname withdate2:(NSDate *)date2 withintervalLength:(NSString *)intervalLength {

        self = [super init];

        self.girlname = girlname;
        self.date2 = date2;
        self.intervalLength = intervalLength;
        return self;



    }


    -(id)initWithNoInterval:(NSString *)girlname withdate2:(NSDate *)date2 withperiodLength:(NSString *)periodLength withpmsLength:(NSString *)pmsLength {

        self = [super init];

        self.girlname = girlname;
        self.date2 = date2;
        self.pmsLength = pmsLength;
        self.periodLength = periodLength;
        return self;



    }

    - (id)initWithCoder:(NSCoder *)coder {
        if (self = [super init]) {
            self.girlname = [coder decodeObjectForKey:@"girlname"];
            self.date2 = [coder decodeObjectForKey:@"date2"];
            self.intervalLength = [coder decodeObjectForKey:@"intervalLength"];
            self.pmsLength = [coder decodeObjectForKey:@"pmsLength"];
            self.periodLength = [coder decodeObjectForKey:@"periodLength"];

        }


        return self;
    }

    - (void)encodeWithCoder:(NSCoder *)coder {
        [coder encodeObject:_girlname forKey:@"girlname"];
        [coder encodeObject:_date2 forKey:@"date2"];
        [coder encodeBool:_intervalLength forKey:@"intervalLength"];
        [coder encodeBool:_pmsLength forKey:@"pmsLength"];
        [coder encodeBool:_periodLength forKey:@"periodLength"];

    }


    @end

Here is also a breakpoint screenshot which shows that the newest object (index 2) has values before it is stores in NSDefaults.

enter image description here

UPDATE: After switching "nameofgirl" and the interval string, the interval string worked, but nameofgirl returned nil. So it's only the first two values working for some reason.

Upvotes: 0

Views: 207

Answers (1)

Willeke
Willeke

Reputation: 15598

intervalLength, pmsLength and periodLength are NSString objects. Use encodeObject: to encode them.

Upvotes: 1

Related Questions