iOS 存储对象数组
iOS store Array of Objects
在我的应用程序中,我实现了几个 tableviewcontroller,它们为每个单元格显示一个 NSObject。每个 tableviewcontroller 都有一个不同的 "filter" 应用于数组,因此每个 table 显示这些对象的特定组。
在每个 tableviewcontroller 的 viewdidload 中,我从 NSUserdefaults 加载我的数组,过滤它并显示它。这工作得很好,我正在使用 NSCoder 来获取对象 encoded/decoded 进行存储。
仅出于开发目的,我以编程方式创建每个对象(initWithName:(NSString*)name 等),将所有对象添加到数组并存储它。
但我相信这不是最终的解决方案。
题:
我正在寻找一种方法来实现这个对象数组,而无需手动创建每个对象 code.The 对象的数量是固定的,它们的内容不能改变。他们只需要加载。添加:也许在未来的更新中,我会在列表中添加新项目。
我该怎么做?
所以我按照@Luke Smith 所说的那样实现了它,而且效果很好!谢谢!
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"MyPlist" ofType:@"plist"];
NSArray *allElements = [[NSArray alloc] initWithContentsOfFile:plistPath];
for (NSDictionary *newObject in allElements){
Object *tempObject = [[Object alloc] init];
tempObject.Type = [newObject objectForKey:@"Type"];
tempObject.isFavorite = [newObject objectForKey:@"isFavorite"];
[Elements addObject:tempObject];//MutableArray storing all Elements
}
但经过一些头脑风暴后,我得出的结论是我需要编辑一些条目。不幸的是,由于 .plist 在 MainBundle 中,它不是 editable...
是否可以从一开始就将 .plist 存储在文档目录中(可能在 xcode 中)?
这是我的 .plist :
<plist version="1.0">
<array>
<dict>
<key>isFavorite</key>
<false/>
<key>Name</key>
<string>Obj1</string>
</dict><dict>
<key>isFavorite</key>
<true/>
<key>Name</key>
<string>Obj2</string>
</dict>
</array>
</plist>
因此,当用户按下特定按钮时,我想更改此项目的 .plist 文件中的 isFavorite
值。
查看从 plist 加载您的值。
您需要在您的项目中创建一个新的 plist 文件并向其中添加您的属性(看起来有点像 JSON 编辑器 - 相当容易理解)
在代码中,您可以使用如下一行代码将 plist 加载到内存中:
NSString *pathToMyPlist = [[NSBundle mainBundle] pathForResource:@"MyPlist" ofType:@"plist"];
NSDictionary *myPlistContents = [[NSDictionary alloc] initWithContentsOfFile:pathToMyPlist];
然后您有一个 NSDictionary
,其中包含您在 plist 中指定的键和值。您可以遍历这些键和值来初始化您的对象。
如果我没有正确理解你的问题,这是我在处理具有不同结构的数组时经常做的事情。
在 NSArray 或 MSMutableArray 上使用类别实现
头文件
static const int DIARIES_title = 0;
@interface NSArray (DIARIES_)
+ (id)arrayNEWDiary;
+ (id)arrayNEWDiaryWithTitle:(NSString *)stringTitleValue;
- (NSString *)objectDiary_title;
@end
植入文件
@implementation NSArray (DIARY_)
+ (id)arrayNEWDiary {
return [NSArray arrayNEWDiaryWithTitle: @"Untitled"];
}
+ (id)arrayNEWDiaryWithTitle:(NSString *)stringTitleValue {
return [NSArray arrayWithObjects: stringTitleValue, nil];
}
- (NSString *)objectDiary_title {
return [self objectAtIndex: DIARIES_title];
}
@end
这将使您免于试图记住所有这些索引的内容 objectAtIndex: 0
此外,向此数组结构中添加一个新项目也很简单快捷:
添加新项目
首先添加和更新存储索引值的int
static const int DIARIES_id = 0;
static const int DIARIES_title = 1;
static const int DIARIES_dateCreated = 2;
您将在使用 - objectAtIndex:
或 - replaceObjectAtIndex: withObject:
时使用这些变量
更新 arrayNEW 原型和函数定义
那么您必须为原型输入任何新参数:
+ (id)arrayNEWDiaryWithId:(NSInteger)idValue title:(NSString *)stringTitleValue dateCreated:(NSDate *)dateCreatedValue;
您必须在代码中更新使用此函数的位置,因为您将得到一个 未定义的函数 类似的东西。然后还将参数列表中的变量添加到实现文件中的return语句中:
+ (id)arrayNEWDiary {
return [NSArray arrayNEWDiaryWithId: 0 title: @"Untitled" dateCreated: [NSDate date]];
}
+ (id)arrayNEWDiaryWithId:(NSInteger)idValue title:(NSString *)stringTitleValue dateCreated:(NSDate *)dateCreatedValue {
return [NSArray arrayWithObjects: idValue, stringTitleValue, dateCreated, nil];
}
访问对象
最后,您必须添加函数来访问数组中的每个值:
- (NSInteger)objectDiary_id;
- (NSString *)objectDiary_title;
- (NSDate *)objectDiary_dateCreated;
这将使您不必更新所有代码。此外,这还会告诉您每个函数的数据类型 return 超级有用 。更新索引号。节省时间,因为它全部在一个地方完成。
我希望这就是您的问题所指的:)
在我的应用程序中,我实现了几个 tableviewcontroller,它们为每个单元格显示一个 NSObject。每个 tableviewcontroller 都有一个不同的 "filter" 应用于数组,因此每个 table 显示这些对象的特定组。 在每个 tableviewcontroller 的 viewdidload 中,我从 NSUserdefaults 加载我的数组,过滤它并显示它。这工作得很好,我正在使用 NSCoder 来获取对象 encoded/decoded 进行存储。 仅出于开发目的,我以编程方式创建每个对象(initWithName:(NSString*)name 等),将所有对象添加到数组并存储它。 但我相信这不是最终的解决方案。 题: 我正在寻找一种方法来实现这个对象数组,而无需手动创建每个对象 code.The 对象的数量是固定的,它们的内容不能改变。他们只需要加载。添加:也许在未来的更新中,我会在列表中添加新项目。 我该怎么做?
所以我按照@Luke Smith 所说的那样实现了它,而且效果很好!谢谢!
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"MyPlist" ofType:@"plist"];
NSArray *allElements = [[NSArray alloc] initWithContentsOfFile:plistPath];
for (NSDictionary *newObject in allElements){
Object *tempObject = [[Object alloc] init];
tempObject.Type = [newObject objectForKey:@"Type"];
tempObject.isFavorite = [newObject objectForKey:@"isFavorite"];
[Elements addObject:tempObject];//MutableArray storing all Elements
}
但经过一些头脑风暴后,我得出的结论是我需要编辑一些条目。不幸的是,由于 .plist 在 MainBundle 中,它不是 editable... 是否可以从一开始就将 .plist 存储在文档目录中(可能在 xcode 中)? 这是我的 .plist :
<plist version="1.0">
<array>
<dict>
<key>isFavorite</key>
<false/>
<key>Name</key>
<string>Obj1</string>
</dict><dict>
<key>isFavorite</key>
<true/>
<key>Name</key>
<string>Obj2</string>
</dict>
</array>
</plist>
因此,当用户按下特定按钮时,我想更改此项目的 .plist 文件中的 isFavorite
值。
查看从 plist 加载您的值。
您需要在您的项目中创建一个新的 plist 文件并向其中添加您的属性(看起来有点像 JSON 编辑器 - 相当容易理解)
在代码中,您可以使用如下一行代码将 plist 加载到内存中:
NSString *pathToMyPlist = [[NSBundle mainBundle] pathForResource:@"MyPlist" ofType:@"plist"];
NSDictionary *myPlistContents = [[NSDictionary alloc] initWithContentsOfFile:pathToMyPlist];
然后您有一个 NSDictionary
,其中包含您在 plist 中指定的键和值。您可以遍历这些键和值来初始化您的对象。
如果我没有正确理解你的问题,这是我在处理具有不同结构的数组时经常做的事情。
在 NSArray 或 MSMutableArray 上使用类别实现
头文件
static const int DIARIES_title = 0;
@interface NSArray (DIARIES_)
+ (id)arrayNEWDiary;
+ (id)arrayNEWDiaryWithTitle:(NSString *)stringTitleValue;
- (NSString *)objectDiary_title;
@end
植入文件
@implementation NSArray (DIARY_)
+ (id)arrayNEWDiary {
return [NSArray arrayNEWDiaryWithTitle: @"Untitled"];
}
+ (id)arrayNEWDiaryWithTitle:(NSString *)stringTitleValue {
return [NSArray arrayWithObjects: stringTitleValue, nil];
}
- (NSString *)objectDiary_title {
return [self objectAtIndex: DIARIES_title];
}
@end
这将使您免于试图记住所有这些索引的内容 objectAtIndex: 0
此外,向此数组结构中添加一个新项目也很简单快捷:
添加新项目
首先添加和更新存储索引值的int
static const int DIARIES_id = 0;
static const int DIARIES_title = 1;
static const int DIARIES_dateCreated = 2;
您将在使用 - objectAtIndex:
或 - replaceObjectAtIndex: withObject:
更新 arrayNEW 原型和函数定义
那么您必须为原型输入任何新参数:
+ (id)arrayNEWDiaryWithId:(NSInteger)idValue title:(NSString *)stringTitleValue dateCreated:(NSDate *)dateCreatedValue;
您必须在代码中更新使用此函数的位置,因为您将得到一个 未定义的函数 类似的东西。然后还将参数列表中的变量添加到实现文件中的return语句中:
+ (id)arrayNEWDiary {
return [NSArray arrayNEWDiaryWithId: 0 title: @"Untitled" dateCreated: [NSDate date]];
}
+ (id)arrayNEWDiaryWithId:(NSInteger)idValue title:(NSString *)stringTitleValue dateCreated:(NSDate *)dateCreatedValue {
return [NSArray arrayWithObjects: idValue, stringTitleValue, dateCreated, nil];
}
访问对象
最后,您必须添加函数来访问数组中的每个值:
- (NSInteger)objectDiary_id;
- (NSString *)objectDiary_title;
- (NSDate *)objectDiary_dateCreated;
这将使您不必更新所有代码。此外,这还会告诉您每个函数的数据类型 return 超级有用 。更新索引号。节省时间,因为它全部在一个地方完成。
我希望这就是您的问题所指的:)