Encode/decode NSCoder data in C++ - iphone

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

Related

Keeping data models on the client and server in sync?

This is a question about application architecture. I'm working on a iOS client app and (RoR) server backend. It seems like I spend needless mental energy creating data models on the RoR server, serving that model as JSON (that part is easy enough from Ruby), creating objective-C data objects, and writing JSON/NSDictionary serializer/de-serializers for said class.
It seems like I should be able to just, write a data schema (in ruby, yaml, json, whatever), and get the ruby object, obj-c object, and json serialize/deserialize for both for free. Anyone have any thoughts on this issue?
Thanks!
look at the different libraries available. the one that works the best is RestKit. http://restkit.org/
RestKit allows you to easily translate a JSON API into coredata objects that are synced on the client. you can tell it what objects to update and how.

CoreData web service using MySQL

I currently have a MySQL database that I wish to create a web service for.
One of the main purposes of this web service is to be used in an iPhone app. Because of this I would like to used CoreData, as it will make parsing on the iPhone side so much easier. How would I use CoreData to get the data from my MySQL database? Are there any good tutorials around?
To get the data from the server to the iPhone I would recommend JSON.
Then you need to write some code that will turn that JSON into an object that you can put into the CoreData database. You have a couple choices there, but I would recommend providing your own implementations of the NSCoding protocol. The great part about the NSCoding approach is that the object itself defines what it needs to save/restore one time, then you simply do additional implementations one time to support other formats (e.g. XML, JSON, simple serialization).
Here is the tutorial to sbjson, a JSON parser on Objective-C: sbjson project

Communicating with a server, is XML the preferred method?

If a iphone app needs to communicate with a server, is xml the best route in most cases?
how hard is it to parse xml in obj-c?
It really depends on the type of data you wish to exchange, but XML will at the very least be able to handle any complexity of data structure you require. (If you only want to exchange a minimal amount of information, you might want to consider JSON that said.)
There are quite a few XML parsers available for Objective-C, most of which are discussed on this existing question: Navigating XML from Objective-C
Finally, there's a great blog post on Ray Wenderlich's web site that discusses the various XML parsers with a view to speed/memory footprint which might be important if you're parsing a large amount of data.
Depends on what is beeing transmitted.
That said I use JSON for 90% of my server to app communication. Easy to parse as libraries are readily available.
Nope. Not hard. But when it comes to APIs it seems many prefer JSON.
JSON is easier to work with than XML, regardless of parser used. Lots of server side people will understand JSON quite well because of the need to use it to work with Javascript.
The iPhone JSON parser I'd look at using first is YAJL.
I would either go with XML or JSON ( http://www.json.org/ ).
It's very easy to parse XML on the iPhone. There are quite a few XML parsers out there based on your preference. For a DOM parser you can use TBXML, otherwise Apple's built-in NSXMLParser does the job.
I use JSON, which is a great (and popular) solution for your server as well. Try out SBJSON for a good obj c library:
http://code.google.com/p/json-framework/

NSDistantObject Protocol for iphone

I've been reading through the docs on how the NSDistantObject class and I can see that it is more or less a binary protocol for sending objects from one NSPort to another. What I'm curious about is the possibility of using this in an iPhone application to share data. Is it possible to have two applications talking to each other on distinct phones using this protocol if the connection is established by a server, or even if its not? (I'm sort of thinking of torrents) Also, how does this interchange format compare to typical data serialization methods such as JSON?
Not currently possible as NSDistantObject is only available for the OS X Foundation framework.
Source.

Easy way to archive interlinked objects

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!