简体   繁体   中英

Method Swizzle encodeWithCoder/initWithCoder

We're working on an app that saves a lot of stuff in to Core Data. The stuff it saves is mainly NSDate NSNumber & NSString but it also saves many images and videos as NSData . I'm trying to change it so that these files are instead saved to local storage and a file name is saved in core data instead, then we just load it up from the local storage, hopefully saving time when accessing core data and making it a bit more stable.

After discussing it, we thought it would be easiest to override encodeWithCoder and initWithCoder so that we can just intercept the calls to save the data and save them on the file system instead. Then I found out that NSData is actually a part of a class cluster and so overriding the functions is harder then it seems.

So then I read about method swizzling where you can just swap one implementation for another. I Googled around and found this , and also found a post on Stack Overflow saying to use that article, but it didn't really help me and I'm still non the wiser on how I would do what I need.

Could anyone point me in the right direction or give me some of their own pointers on how I would do this?

Thanks,

Don't method swizzle these -- trust me :)

If you want to save out as described, place that implementation in your class' implementation of the archiving methods (not a subclass, not a swizzle, but the class that contains these objects).

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