简体   繁体   中英

Storing an events calendar using NSUserDefaults or CoreData

So I have an app which upon launch imports a user's specific calendar and displays all events related to it. I use NSPredicate to sort them and store the events in an array.

I would like to be able to store this array upon each launch of the app, then in another method i hope to recall it and subtract it from the same calendar events at a later time. The idea being that only the changed items will remain.

I have tried using NSUserDefaults to store the array which is about 70 items long but i get an error : Property list invalid for format: 200 (property lists cannot contain objects of type 'CFType')

I have tried converting the array to NSData using NSKeyedArchiver but that causes a crash aswell: exception 'NSInvalidArgumentException', reason: '-[EKEvent encodeWithCoder:]

Any suggestions on how I can store and recall my events array. The reason i mention coreData in the title is i fear some of you will tell me to use that which is new to me.

Your approach is generally ok, but you're falling over at actually saving because you can't directly save an EKEvent (it isn't permissible in user defaults and it doesn't implement NSCoding ).

You need to extract the parts, such as the eventIdentifier , from each EKEvent and store them in another object that you can add to user defaults (like a dictionary) or that you can archive (so it and its contents need to conform to NSCoding ).

When you reload the data you would then compare each element of data you saved instead of comparing the whole EKEvent s (not clear how you were doing the comparison anyway).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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