由于您没有太多数据,只需使用 a.plist
它会更容易管理
使每个父母 Trees,Flowers,Bushes 和 array 使每个子项成为一个 dictinary ,所以当您检查一个孩子是否满足您的要求时,就像2 feet height under Tulips
使用它一样。
创建一些像这样的plist:
代码示例:注意:我没有测试这个你需要改进这个
您可以在这里使用某种逻辑来检查颜色、种类或高度。
我从我的项目中给出一个示例,让您了解如何过滤 plist,因此可以根据需要更改函数的名称。
我不会更改函数名称,因为“没有人有时间”:) 创建一个名为 nsobject 的类ParseSchedulePlist
在 ParseSchedulePlist .h
#import <Foundation/Foundation.h>
@interface ParseSchedulePlist : NSObject
@property (nonatomic,strong) NSMutableDictionary * agendaDic;
- (void)passStringToAgendaDic:(NSString *)string;
//trees
-(NSArray*)getTrees;
-(NSDictionary*)getItems:(NSInteger ) section ;
-(NSString*)getItemKind :(NSInteger ) section;
-(NSString*)getItemColor :(NSInteger ) section;
-(NSNumber*)getItemheight :(NSInteger ) section;
//flowers
//do the same of above for flowers an bushes took
@end
在 ParseSchedulePlist .m
#import "ParseSchedulePlist.h"
@implementation ParseSchedulePlist
@synthesize agendaDic = _agendaDic;
- (void)passStringToAgendaDic:(NSString *)string {
//get plist from bundle
NSString * path = [[NSBundle mainBundle] pathForResource:string ofType:@".plist"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:path];
NSLog(fileExists ? @"Yes" : @"No");
NSLog(@"Path is %@",path);
NSLog(@"agendaDic is %u",[self.agendaDic count]);
self.agendaDic = [NSMutableDictionary dictionaryWithContentsOfFile:path];
}
-(NSArray*)getTrees
{
NSArray * value = [[self agendaDic] objectForKey:@"trees"];
return value;
}
-(NSDictionary*)getItems:(NSInteger ) section
{
NSDictionary * value =[[self getTrees] objectAtIndex:section];
return value;
}
-(NSString*)getItemKind :(NSInteger ) section;
{
NSString * value =[[[self getItems] objectAtIndex:section] objectForKey:@"kind"];
return value;
}
-(NSString*)getItemColor :(NSInteger ) section
{
NSString * value =[[[self getItems] objectAtIndex:section] objectForKey:@"color"];
return value;
}
-(NSNumber *)getItemheight :(NSInteger ) section;
{
NSNumber * value =[[[self getItems] objectAtIndex:section] objectForKey:@"height"];
return value;
}
//write the same functions for flowers and bushes
@end
在您的常规视图控制器 .h 中:
#import "ParseSchedulePlist .h"
@property (nonatomic, strong) ParseSchedulePlist *agenda;
在您的常规视图控制器 .m 中:
#import "ParseSchedulePlist .h"
@synthesize agenda;
//here calls for the special class
self.agenda=[[ParseSchedulePlist alloc] init];
[self.agenda passStringToAgendaDic:@"name of the plist"];
NSMutableArray *newArray=[ NSMutableArray alloc] init];
//here for example get all the palm trees under 6 feet
for(int i=0; i<[self.agenda getTrees] count] i++)
{
if([self.agenda getItemKind :i] isquealtostring @"palm"){
if([self.agenda getItemheight :i] <= 6)
[newArray add object:[self.agenda getItems:i];
}
}
Nslog(@"print your array %@",newArray);