0

首先,我有一个管理 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 方法

4

2 回答 2

1

好的,我改变的只是“initWithContent”方法和保存方法。我已经注释掉了您的代码,并在它下面进行了更改以使用 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。

于 2012-08-23T21:30:03.110 回答
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,它帮了我很多

于 2012-08-24T01:10:14.057 回答