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. 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
@synthe开发者_如何学运维size HotelID, Name, Capacity;
-(void)dealloc {
[Name release];
[Capacity release];
}
The "Hotel" object is managed by my DataModel. The header for DataModel:
@class Hotel;
@interface DataModel : NSObject {}
-(int)hotelCount;
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
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.)?
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];
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];
}
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
@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.
精彩评论