Reputation: 39253
In the Model portion, I have some data types whose properties are all property list data types. Eventually, users will be sending each other these data items over the network by posting to and reading from a server. The question is, should I be using NSCoding and NSKeyedArchiver (as in the second code block below, which makes ugly XML IMHO) or should I do something like subclass NSDict so I can use dictionaryWithContentsOfFile:
and writeToFile:atomically:
to get pretty XML?
@interface Word : NSObject <NSCoding>
@property (strong, nonatomic) NSString *language;
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSDictionary *detail;
@property (strong, nonatomic) NSString *userName;
@property (strong, nonatomic) NSString *userId;
@property (strong, nonatomic) NSArray *files;
- (void)copyFromTemporaryFiles:(NSArray *)temporaryFiles; // populates self.files
@end
and
@interface Category : NSObject <NSCoding>
@property (strong, nonatomic) NSString *name;
@property (strong, nonatomic) NSDictionary *detail;
@property (strong, nonatomic) NSDate *updated;
@property (strong, nonatomic) NSArray *words;
+ (Category *)categoryWithName:(NSString *)name;
- (void)saveWithName:(NSString *)name;
@end
Upvotes: 0
Views: 646
Reputation: 5820
In my opinion, neither of these options is a great practice for sending data over a network since the file sizes will be relatively large compared to other formats. Instead, I would probably use a JSON library to transmit this sort of structured data. If you're building for iOS 5+, you can look into NSJSONSerialization
(reference here) to do some simple, straightforward conversion (just pack up all your ivars in a dictionary and pass it to +dataWithJSONObject:options:error
, send the data over the network, and unpack with +JSONObjectWithData:options:error
). Otherwise, there's a bunch of 3rd party and/or open source solutions out there. SBJSON comes immediately to mind.
P.S. I would really be wary of subclassing NSDictionary
. The class actually represents a class-cluster and has a bunch of optimization going on the background to handle best use of memory, etc. You can read the Subclassing Notes on the reference page to find some other means of adding functionality to your dictionaries.
Upvotes: 2