0

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.

5
  • Can you share your custom women file also? Commented Jun 28, 2017 at 4:23
  • 1
    try NSLog your data before saving it to NSUserDefaults I think values are not getting attached to your interval length variables that are coming nil while retrieving. if this doesn't work kindly show your custom class if possible. Commented Jun 28, 2017 at 5:21
  • I got the custom class and another screenshot, thanks. Any more ideas? Commented Jun 28, 2017 at 7:33
  • Use encodeObject: to encode the intervalLength, pmsLength and periodLength NSString objects. Commented Jun 29, 2017 at 22:31
  • It worked. wow, how do I upvote you. Thank you so much !!!! I should have noticed that. Commented Jun 30, 2017 at 0:32

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I would upvote if I was allowed to but this works for me. Thank you !!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.