In a game I have a bunch of objects and collections of objects, some of them include references to others. Is there a straightforward way to archive them - to save a game state - while maintaining those references? If I understood well, NSArchiver can do that, but it wasn't available on SDK 2, is it on SDK 3?
(I have sort of asked this question before (best way to save/load iphone game data) but SDK 3 has been released in the meantime, and I'd like to know if this topic has progressed.)
You need to make all of the classes you want to serialize implement the NSCoding protocol. After that, you need to encode them with the NSKeyedArchiver class. That class will recursively serialize your objects, and will deal with cycles. NSKeyedArchiver is available in all releases of the iPhone SDK. To decode your objects, you'll uses NSKeyedUnarchiver.
Check out Object serialization here:
Encoding/Decoding Objects (apple.com)
Basically, you have to make your classes Key-Value Coding compliant, to properly save all of your data; It archives them into binary property lists. After you write your initWithCoder: and encodeWithCoder: functions it's pretty much automatic!
Related
In my game I have a NSCoding-conforming Combat class, which also uses GameplayKit's GKGridGraph and GKGridGraphNode objects, which do not conform to NSCoding. Is there a way to serialize/unserialize these objects?
As you've noted, there's no NSCoding support in these classes. (That'd make a good feature request to send to Apple, though.)
It should be possible, however, to create your own serialized representation of a graph when encoding your classes, then read that in and initialize new GKGraph and GKGraphNode objects accordingly in your init(coder:) implementation. Note that when doing so you'll have to carefully walk the graph yourself to capture all the node connections.
Has anyone developed a singleton for accessing SQLite db? I know that there are other options like Core Data but, in my case, I need SQLite. I have looked at Apple provided singleton creation code (here) but the thing is SQLite database "stuff" is not an object, it is "typedef struct sqlite3". So currently, I'm doubting how should I adapt this code for being singleton. Any suggestions, please :)
UPDATE: I have looked at FMDB framework for SQLite that does all stuff but it doesn't implement singleton. I mean, access methods are instance not class methods. If I need to call the SQL statements from different my code places I need to pass a pointer around instead of calling some shared instance class method :( So, the question remains open.
I think you should use FMDB, it is one of good wrapper libraries around for SQLite3.
See details about FMDB here http://www.ioslib.com/library/data/fmdb/
I'd like to send serialized objects between a C++ application (running on a linux machine) and an iPhone application. Is this possible?
Is there a way to encode/decode data using the NSCoding protocol on the C++ side? Has someone reverse engineered this protocol?
I couldn't find any techniques to decode objects in C++ that had been serialized using the NSCoder protocol.
I ended up building a JSON interface on both sides and simply sending my objects back and forth as serialized text.
Thanks for the responses and ideas!
I'm no expert, but it seems like your best bet is to use property lists, which allow you to serialize objects into a device-independent format. You may still need to write some custom deserialization code on the C++ side, however. But this seems easier than trying to decode archived objects.
Check this out for more details: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Archiving/Articles/serializations.html#//apple_ref/doc/uid/20000947-BCIEBEGI
This question is basically the inverse of this other question: Calling Python from Objective-C
I have implemented my iPhone application logic in Objective-C (obviously), and am now trying to re-use as much as possible from my XCode project in the server component to save on double-implementation. I have successfully loaded the CoreData data model from Python, however, can't see a way to actually call into the Objective-C logic from Python.
Basically I'm trying to access the Objective-C classes and methods in my iPhone project from Python to save myself duping out all the implementations.
Is this even vaguely possible, or is dupe-implementation the only solution here? Seems like the kind of thing Boost::Python might be used for, but I'm not really sure.
edit: Boost::Python won't work because it is C++ based and I need Objective-C. I knew there was a reason why that didn't work.
If your Objective-C code is in a framework and you would like to essentially write a Python application that uses your framework, then you can use objc.loadBundle, and then use objc.lookUpClass or NSClassFromString to get access to your classes. From there, you can use your classes like any other bridged Objective-C class.
If you're running your Python code within a process that already has the Objective-C runtime up, and your classes are already registered with it, then you can skip the loadBundle step.
When a user uploads something via my app, I create an ASIFormDataRequest object to make the POST.
When the user is offline, I would like to write a ASIFormDataRequest object to file and send it later.
Is there a built in way to serialize an object like this in Objective C, or do I have to write something from scratch?
Yep! There's a really great thing called the NSCoding protocol. A writeup on how to implement and use it is available on our local CocoaHeads site: http://cocoaheads.byu.edu/wiki/nscoding In a nutshell, you implement two methods to define what you want to save and how to restore it, and then it's a one-liner to actually archive your object.
In the Objective-C programming
language, serialization (more commonly
known as archiving) is achieved by
overriding the write: and read:
methods in the Object root class.
http://en.wikipedia.org/wiki/Serialization#Objective-C
There's a code example there too :-)