plist和sqlite3之间无法确定

时间:2013-05-08 18:09:21

标签: iphone ios xcode core-data sqlite

我一直在讨论.plist和sqlite3数据库,以保存我需要访问的一些数据,但不能在.plist /数据库中操作。这是我的问题。让我们说我想存储树木,花朵和灌木的高度和颜色,我希望每条信息都可以访问。下面与我想要的类似:树木 Palm 6英尺绿色柳树 8英尺棕色灌木丛 常绿 5英尺绿色 Cinquefoil 2英尺黄色鲜花 玫瑰 1英尺红色郁金香 2英尺黄色< / p>

因此,如果我想单独访问Tulips下的2英尺高度并将其显示在我的应用程序的文本框中......什么是使用的最佳数据存储/资源形式。 .plist或sqlite?我怎么把这个列为.plist?
一如既往,我感谢您的时间和精力! 谢谢!

2 个答案:

答案 0 :(得分:1)

由于您没有太多数据,只需使用.plist即可更轻松地管理

让每个父树木,鲜花,灌木和数组使每个子项目成为一个整体,所以当你检查一个孩子是否满足你的要求时,如2 feet height under Tulips使用它。

像这样创建一些plist:

enter image description here

代码示例: 注意:我没有测试你需要改进这个

你可以在这里使用某种逻辑来检查颜色,种类或高度。

我从我的项目中给出了一个示例,让您了解如何过滤plist,因此可以根据需要更改函数的名称。

我不会更改功能名称导致“没有人得到时间”:) 创建一个名为ParseSchedulePlist

的nsobject类

在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);

答案 1 :(得分:0)

您可以通过核心数据实现相同的目标。核心数据本身有点难以使用。我一直用它和Magical Record,一个很棒的图书馆。

使用关系映射对象非常容易。在您的情况下,父母和孩子之间存在一对多和一对一的关系。

enter image description here

使用MagicalRecord,如果你想找到一个高度等于2的物品。你可以通过两个简单的步骤来完成这个步骤

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"height =%@",@2];
NSArray *items = [Item findAllWithPredicate:predicate];

好的是,由于项目之间存在一对一的关系,因此每个项目都会有关于组的信息。所以一切都很合适,而且搜索你喜欢的方式更容易。

您可以找到source code了解详情。