Is it possible to encode an Objective-C block with an NSKeyedArchiver?
I don’t think a Block object is NSCoding-compliant, therefore [coder encodeObject:block forKey:@"block"] does not work?
Any ideas?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, it isn’t possible for a variety of reasons. The data contained within a block isn’t represented in any way similar to, say, instance variables. There is no inventory of state and, thus, no way to enumerate the state for archival purposes.
Instead, I would suggest you create a simple class to hold your data, instances of which carry the state used by the blocks during processing and which can be easily archived.
You might find the answer to this question interesting. It is related.
To expand, say you had a class like:
Then you could:
From there, you could implement archiving on MyData, save it away, etc… The key is treat the Block as the trigger for doing the computation and encapsulate said computation and the computation’s necessary state into the instance of the MyData class.