gpt4 book ai didi

objective-c - 加载和保存 NSMutableArray 将不起作用

转载 作者:行者123 更新时间:2023-11-28 22:50:31 25 4
gpt4 key购买 nike

首先,我有一个管理 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 实例的类中调用

最佳答案

好的,我只更改了“initWithContent”方法和保存方法。我已经注释掉了您的代码并在其下进行了更改以使用 NSUserDefaults。 NSUserDefaults 应该保存您的数组的二进制文件,稍后将其取回并将其转换回 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。

关于objective-c - 加载和保存 NSMutableArray 将不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12099772/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com