Serializing objects in Objective C - iphone

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 :-)

Related

What is Dynamic Creation?

I recently read about Dynamic Creation as one of the design pattern in Cocoa. However, I don't really understand how it works. So I need clarification from you who have implemented in your design.
What is it? Why and when would you use this design pattern?
I have read that you use NSClassFromString() to access the class. I assume that I use this when I want to use class that doesn't exist within the project I'm working on. Usually when I want to use certain class, I imported them in header. Does using this approach skip the #import process?
Class JavaArrayList = NSClassFromString(#"java.util.ArrayList");
I quote the code above as example. If do according to the code above, that means I can create a new JavaArrayList class and use the methods in it right?
JavaArrayList *foo = [[JavaArrayList alloc] init];
[foo useMethodBelongJava:doWhateverTask];
What are the benefits of using this design pattern? Especially in iPhone Development.
Your example appears to be using that pattern to instantiate a Java class. In the old days (up to about MacOS 10.4 I think), Apple had some technology called the Cocoa-Java Bridge, which let you use Java classes within Objective-C code. You had to instantiate them in the manner specified, because they didn't have Objective-C header files to import.
However, as of Snow Leopard, the Java Bridge no longer exists, so the code in your question won't work any more.
The recommended solution for calling a Java class from Objective-C is now JNI. Take a look at this question if that is what you're trying to do.
What is it? Why and when would you use this design pattern?
Coming back to NSClassFromString, it has other uses besides instantiating Java classes (which, as I mentioned, it doesn't do any more!). For an example, recently I wrote a library for parsing the response from a web service. In order to make it work with different web services, I had it read in a configuration file that described the data format it was expecting. For each field in the web service, my configuration file specified which Cocoa class to instantiate. Thus, in my code, I had a Cocoa class name as a string. To instantiate the object I wanted, I used NSClassFromString to turn it into a Class object.
Usually when I want to use certain class, I imported them in header. Does using this approach skip the #import process?
It can do. NSClassFromString will instantiate any class that is present at run time, so you don't need the header to be able to use it. If you don't have the header, you'll get a bunch of warnings of "may not respond to selector" whenever you try and use your newly instantiated class, as the compiler doesn't have enough information to be helpful. However, in many circumstances where NSClassFromString is useful, the header files aren't available.
See this link:
need advise about NSClassFromString
The only real benefit for iPhone was being able to reference classes from newer APIs and still target the old APIs. Since 4.0 you can do this anyway by setting the deployment target of your project. I can't really see any reason you would use it for iPhone programming any more.
This would only work for objective-C classes. You can't import java objects into your iphone app.

Calling custom Objective-C from a pyobjc application?

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.

How do I create a global variable using Objectvie-C in iPhone?

I'm the beginner in iPhone software development, and I'm creating a HTTP request using the SOAP XML parsing method.
I was creating a variable soapResult for storing the received http data. I want to use the soapResult value into another class.
How can I access that value. I was going to make it a global variable but i don't know how to create and use it?
Also if that method is wrong then how should I implement it?
Generally, singletons are preferred over global variables.
I found this article useful on a similar matter: Cocoa with Love: Singletons, AppDelegates and top-level data
I suggest you to use something like Registry pattern. Just create class Registry with static methods and static properties. It is better way to controll all shared stuff in your app
Global vars are not preferred in objective-c, but if you want to use, this
approach works well.

Object sharing in my iPhone App

I'm currently developping my first iPhone application and I would like to know what is the best way to share an object which is gonna be used in every controller.
I was thinking of referencing the controller containing the needed data in other controllers and access this data thanks to properties, is that a good idea or is there a better way to do it?
Or can we declare any global variable (not const) accessible anywhere in the application?
Thank you
It's not clear from your question what kind of data you are dealing with, but one approach I have used in iphone applications is a singleton object that contains application state and state management functions.
There is a good discussion of the use of singletons versus app delegates for this type of data found at Cocoa With Love and I agree with what he has to say here.
For instance the application in this case had a user account tied to a web application so on load the application would initialize the singleton object and when the user signed in it would keep a reference to the user object in the singleton so that user info could be referenced for api calls.
This approach has worked fairly well for my purposes you can see how to create a singleton class in Objective-C here, and also in the apple docs here there are several ways to do it which provide the same functional result.
Once you have defined your singleton class you can initialize it in applicationDidFinishLaunching or in the viewDidLoad of your main controller and init the globally accessible data you are needing to manage. I highly recommend you read the Cocoa With Love article I linked above for some consideration of managing this whole process.
Hope that helps.
If you're stuck I can post some example code, but the singleton class examples available are pretty straightforward to work with.
If you really need it to be global, you can put it in the AppDelegate or create a data management object. But I think it's frowned upon to do it like that.

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!