gpt4 book ai didi

iphone - 无法确定 plist 和 sqlite3

转载 作者:行者123 更新时间:2023-11-29 13:13:41 25 4
gpt4 key购买 nike

我一直在争论 .plist 和 sqlite3 数据库来保存一些我需要访问的数据,但不能在 .plist/database 中操作。这是我的问题。假设我想存储树木、花朵、灌木的高度和颜色,并且我希望每条信息都可以访问。以下与我想要的类似:树木 棕榈 6 英尺绿色柳树 8 英尺棕色灌木 常青树 5 英尺绿色五叶草 2 英尺黄色 玫瑰 1 英尺红色郁金香 2 英尺黄色

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

最佳答案

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

使每个父项 Trees,Flowers,Bushes 和数组使每个子项成为一个字典,因此当您检查一个子项是否满足您的要求时,例如 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

在您的常规 View Controller .h 中:

#import "ParseSchedulePlist .h"
@property (nonatomic, strong) ParseSchedulePlist *agenda;

在你的常规 View Controller .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);

关于iphone - 无法确定 plist 和 sqlite3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16447393/

25 4 0
文章推荐: postgresql - 扩展另一个图像标签时 Postgres docker 容器初始化脚本不运行
文章推荐: php - 在 html select 中显示 MySQL 查询
文章推荐: PostgresQL 错误 : relation doesn't exist