NSUserDefaults为数组中的某些字符串返回nil

时间:2017-06-28 03:49:55

标签: objective-c xcode nsmutablearray nsuserdefaults

我有一个自定义对象"女人"。我试图在其中存储以下值,并使用NSUserDefaults将其保存在可变数组中。我用的代码如下。我也在对象中使用NSCoding。

       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"];

我使用此代码来检索它:

//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 first string is correctly retrieved, and the NSDate, however the last three arents?

编辑:这是我的自定义课程。

·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

而.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

这里还有一个断点截图,显示最新对象(索引2)在NSDefaults中存储之前的值。

enter image description here

更新:切换&#34; nameofgirl&#34;和间隔字符串,间隔字符串工作,但nameofgirl返回零。因此,只有前两个值因某种原因而起作用。

1 个答案:

答案 0 :(得分:1)

intervalLengthpmsLengthperiodLengthNSString个对象。使用encodeObject:对它们进行编码。