加载和保存NSMutableArray将不起作用

时间:2012-08-23 20:49:26

标签: objective-c ios file

首先,我有一个类会对NSMutableArray造成影响。 在实例化类时,它应该看看是否已经存储了已保存的数组并加载它,或者以其他方式创建一个新数组。

每次添加项目时都应保存。但是:没有任何反应。

#import "NEList.h"

@interface NEList()



@end

@implementation NEList
@synthesize theInternArray;



-(id) initWithArray {
    self = [super init];

    if(self != nil) {
             NSLog(@"build");

        theInternArray = [[NSMutableArray alloc] initWithCapacity:20];

        return self;
    }
    return nil;
}

-(id) initWithContent {
    self = [super init];

    if(self != nil) {
        NSLog(@"load");

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"theFiles"];

        theInternArray = [NSMutableArray arrayWithContentsOfFile:filePath];

        if(theInternArray == nil) {
            theInternArray = [[NSMutableArray alloc] initWithCapacity:200];
        }
               NSLog(@"second %@", theInternArray);
        return self;
    }
    return nil;
}

-(BOOL) save {

   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
   NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"theFiles"];


    return [self.theInternArray writeToFile:filePath atomically:NO];
}

-(BOOL) addObject:(NEListItem *)theItem {

    [theInternArray addObject:theItem];    [self save];





    UILocalNotification *localNotif = [[UILocalNotification alloc] init];

    localNotif.fireDate = theItem.theBestBeforeDate;
    localNotif.timeZone = [NSTimeZone defaultTimeZone];

    localNotif.alertBody = [theItem.theName stringByAppendingString:@" expires!"];
    localNotif.alertAction = NSLocalizedString(@"View Details", nil);

    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 1;



    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];


    return true;
}


-(NEListItem *) getElementAtIndex:(NSUInteger)theIndex {
    return [theInternArray objectAtIndex:theIndex];
}

-(BOOL)removeObjectByName:(NSString *)theName {

    for (NEListItem *tempItem in theInternArray) {

        if([tempItem.theName isEqualToString:theName]) {
            [theInternArray removeObject:tempItem];
            break;
            return true;
            }
    }

            NSLog(@"Nicht gefunden");
            return false;
        }






@end

在另一个拥有NEList

实例的类中调用save方法

2 个答案:

答案 0 :(得分:1)

好的,我改变的是“initWithContent”方法和save方法。我已经注释掉了你的代码,并在其下进行了更改以使用NSUserDefaults。 NSUserDefaults应该保存您的Array的二进制文件,然后将其取回并将其转换回NSArray。如果这不起作用,请告诉我你得到的错误。

-(id) initWithContent {
    self = [super init];

    if(self != nil) {
        NSLog(@"load");
        /*
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"theFiles"];

        theInternArray = [NSMutableArray arrayWithContentsOfFile:filePath];

        if(theInternArray == nil) {
            theInternArray = [[NSMutableArray alloc] initWithCapacity:200];
        }
        NSLog(@"second %@", theInternArray);*/
        NSUserDefaults *uDefaults = [NSUserDefaults standardUserDefaults];
        theInternArray = [uDefaults objectForKey:@"internArray"];
        if (!theInternArray)
        {
            theInternArray = [[NSMutableArray alloc] initWithCapacity:200];
        }
        NSLog(@"%@", theInternArray);

        return self;
    }
    return nil;
}

-(BOOL) save {

    /*NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"theFiles"];*/

    NSUserDefaults *uDefaults = [NSUserDefaults standardUserDefaults];
    [uDefaults setObject:theInternArray forKey:@"internArray"];


    //return [self.theInternArray writeToFile:filePath atomically:NO];
    return [uDefaults objectForKey:@"internArray"];
}

哦,你的方式不起作用的原因是因为保存文件然后取回它并不是那么简单。您需要将数组中的数据(例如“item1,item2,item3等”)打印到文件中,然后解析它并稍后创建一个包含该数据的NSArray。

答案 1 :(得分:1)

所以,这是我的解决方案。 首先,这是我的自定义类,它存储在数组中:

#import <Foundation/Foundation.h>

@interface NEListItem : NSObject <NSCoding>

@property (nonatomic, strong) NSString *theName;
@property (nonatomic, strong) NSString *theBestBeforeDate;
@property (nonatomic, strong) NSDate *theInternDate;


-(id) initWithName:(NSString *) theInitName;
 @end

和实施

#import "NEListItem.h"

@implementation NEListItem
@synthesize theName = _theName;
@synthesize theBestBeforeDate = _theBestBeforeDate;
@synthesize theInternDate = _theInternDate;

-(id)initWithName:(NSString *)theInitName {
    self = [super init];
    if(self != nil) {
        self.theName = theInitName;

        return self;
    }
    return nil;
}

- (void)encodeWithCoder:(NSCoder *)coder;
{
    [coder encodeObject:self.theName forKey:@"theName"];
    [coder encodeObject:self.theBestBeforeDate forKey:@"theBBDate"];
    [coder encodeObject:self.theInternDate forKey:@"theInternDate"];
}

- (id)initWithCoder:(NSCoder *)coder;
{
    self = [[NEListItem alloc] init];
    if (self != nil)
    {
        self.theName = [coder decodeObjectForKey:@"theName"];
        self.theInternDate = [coder decodeObjectForKey:@"theInternDate"];
        self.theBestBeforeDate = [coder decodeObjectForKey:@"theBBDate"];
    }
    return self;
}

@end

因此,这是保存自定义值所必需的。

保存方法:

-(BOOL) save {

    NSLog(@"save called");
    [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:theInternArray] forKey:@"savedArray"];
    return true;
    }

和作为init方法一部分的加载

 NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
        NSData *dataRepresentingSavedArray = [currentDefaults objectForKey:@"savedArray"];


            NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];

        if (oldSavedArray != nil) {
                theInternArray = [[NSMutableArray alloc] initWithArray:oldSavedArray];
            return self;
        }
            else
                          theInternArray = [[NSMutableArray alloc] initWithCapacity:200];
        return self;
        }
非常感谢ManOx给了我很多帮助