如何将 plist 数据读入数据模型? [英] How to read in plist data into a data model?

查看:29
本文介绍了如何将 plist 数据读入数据模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过在我的数据模型中硬编码一些静态数据(酒店信息)来启动我的应用程序,以便在我的应用程序中的任何地方都可以访问它们.这很好,直到列表开始增长(仍然是静态数据).我试图弄清楚如何通过使用 plist 来重新创建硬编码数据.看起来很直接,但似乎无法弄清楚.

I started my app by hardcoding some static data (hotel info) inside my data model so that they are accessible everywhere in my app. This was fine until the list started to grow (still static data). I'm trying to figure out how to recreate the hardcoded data by using a plist instead. Seems straight forward but can't seem to figure it out.

我的酒店"对象标题:

@interface Hotel : NSObject {}

@property (nonatomic, assign) int HotelID;
@property (nonatomic, copy) NSString* Name;
@property (nonatomic, copy) int Capacity;

@end

我的酒店"对象实现:

@implementation Hotel

@synthesize HotelID, Name, Capacity;

-(void)dealloc {
    [Name release];
    [Capacity release];
}

Hotel"对象由我的 DataModel 管理.DataModel 的标头:

The "Hotel" object is managed by my DataModel. The header for DataModel:

@class Hotel;

@interface DataModel : NSObject {}

-(int)hotelCount;

DataModel 实现:

The DataModel implementation:

#import "DataModel.h"
#import "Hotel.h"

// Private methods
@interface DataModel ()

@property (nonatomic, retain) NSMutableArray *hotels;

-(void)loadHotels;

@end

@implementation DataModel

@synthesize hotels;

- (id)init {
    if ((self = [super init])) {
        [self loadHotels];
    }
    return self;
}

- (void)dealloc {
    [hotels release];
    [super dealloc];
}

- (void)loadHotels

hotels = [[NSMutableArray arrayWithCapacity:30] retain];

Hotel *hotel = [[Hotel alloc] init];
hotel.HotelID = 0;
hotel.Name = @"Solmar";
hotel.Capacity = 186;
// more data to be added eventually
[hotels addObject:hotel];
[hotel release];

Hotel *hotel = [[Hotel alloc] init];
hotel.HotelID = 1;
hotel.Name = @"Belair";
hotel.Capacity = 389;
[hotels addObject:hotel];
[hotel release];

// and so on... I have 30 hotels hard coded here.

- (int)hotelCount {
    return self.hotels.count;
}

@end

此设置工作正常.但是,我无法弄清楚如何实现数据被硬编码的 loadHotel 部分.我想用具有相同信息的 plist 替换它.我如何读取 plist 文件来为每个键(名称、容量等)分配信息?

This setup works fine. However, I can't figure out how to implement the loadHotel section where the data is hard coded. I would like to replace this with a plist with the same information. How do I read in the plist file to assign information for every key (name, capacity, etc.)?

推荐答案

创建 plist 后,您可以将其内容加载到字典中,如下所示:

Once you create the plist, you can load its contents into a dictionary like this:

NSString *plistPath = [[NSBundle mainBundle] pathForResource:plistFileName ofType:nil];
NSDictionary *plistDict = [NSDictionary dictionaryWithContentsOfFile:plistPath];

然后您可以使用 plist 中的键查询您需要的任何数据:

Then you can query whatever data you need using the keys from the plist:

NSArray *hotelsFromPlist = [plistDict objectForKey:"hotels"];

// remember this is autoreleased, so use alloc/initWithCapacity of you need to keep it
NSMutableArray *hotels = [NSMutableArray arrayWithCapacity:[hotelsFromPlist count]];

for (NSDictionary *hotelDict in hotelsFromPlist) {
    Hotel *hotel = [[Hotel alloc] init];
    hotel.name = [hotelDict objectForKey:@"name"];
    hotel.capacity = [hotelDict objectForKey:@"capacity"];
    [hotels addObject:hotel];
}

希望这会有所帮助.

为代码正确性进行了编辑

edited for code correctness

这篇关于如何将 plist 数据读入数据模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆