简体   繁体   English

如何将plist数据读入数据模型?

[英]How to read in plist data into a data model?

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. 我试图弄清楚如何通过使用plist来重新创建硬编码的数据。 Seems straight forward but can't seem to figure it out. 似乎直截了当,但似乎无法解决。

My "hotel" object header: 我的“酒店”对象标头:

@interface Hotel : NSObject {}

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

@end

My "hotel" object implementation: 我的“酒店”对象实现:

@implementation Hotel

@synthesize HotelID, Name, Capacity;

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

The "Hotel" object is managed by my DataModel. “酒店”对象由我的DataModel管理。 The header for DataModel: DataModel的标头:

@class Hotel;

@interface DataModel : NSObject {}

-(int)hotelCount;

The DataModel implementation: DataModel实现:

#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

This setup works fine. 此设置工作正常。 However, I can't figure out how to implement the loadHotel section where the data is hard coded. 但是,我无法弄清楚如何实现对数据进行硬编码的loadHotel部分。 I would like to replace this with a plist with the same information. 我想将其替换为具有相同信息的plist。 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: 创建plist后,可以将其内容加载到字典中,如下所示:

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

Then you can query whatever data you need using the keys from the plist: 然后,您可以使用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];
}

Hope this helps. 希望这可以帮助。

edited for code correctness 编辑以确保代码正确性

You need to make your object to conforms to NSCoding protocol... That means, you need to implement two methods(don't forget to declare your object as 您需要使您的对象符合NSCoding协议...这意味着,您需要实现两种方法(不要忘记将您的对象声明为

@interface Hotel : NSObject<NSCoding>{
   //your declarations here...
}

and the implementation 和实施

@implementation Hotel
////
-(void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:Name forKey:someKeyRepresentingProperty];
    //and so one...
}

-(id)initWithCoder:(NSCoder *)aDecoder{
    self = [self init];
    if(self){
        self.Name = [aDecoder decodeObjectForKey:someKeyRepresentingProperty];
        //and so one...
    }
    return self;
}

Then you'll be able to store and read your objects pretty easy. 然后,您将能够轻松存储和读取对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM