سؤال

I'm interested in persistent storage for a custom annotation object

CustomAnnotation : NSObject <MKAnnotation>

which has the following properties

@property (nonatomic, assign) CGFloat val1;
@property (nonatomic, retain) NSString *val2;
@property (nonatomic, retain) NSDate *val3;
@property (nonatomic, retain) NSURL *val4;
@property (nonatomic, assign) double val5;
@property (nonatomic, assign) double val6;

What is the best approach to storing the data? I was thinking of using NSUserDefaults, but would I need to archive my CustomAnnotation into NSData? How do you do that?

Thanks!

هل كانت مفيدة؟

المحلول

If you want to persist a custom object to NSUserDefaults you have two options.

  1. Encode the object to NSData using an NSKeyedArchiver
  2. Create an NSDictionary with all of the values you want to persist.

Whether using NSUserDefaults is the proper approach depends on a few factors. If you just have a one or a few of the objects then that would be fine. If you have a lot, then you may want to look at other options. Perhaps a plist or some other file. Either way, you have the same issue of needed to encode the object using one of the two approaches.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top