How do I create a global variable using Objectvie-C in iPhone? - 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.

Related

What is meant by singleton object?and what is it usage?

Can anyone explain to me what a singleton object is? In many tutorials I found something like "create a sigleton object". Now I am working in iphone game development using cocos2d. I read "Create a singleton object to keep all the Game Center code in one spot" in raywenderlich multiplayer tutorial, and also he says:
+ (GCHelper *)sharedInstance;
This static method to retrive the singleton instance of this class. But I don't know what a singleton is, or how to use it.
A Singleton object is an object for which there is only a single instance for a given class. The Singleton pattern is used when you want to guarantee that a single instance of a class will be shared across clients in a system.
Check out the C2 wiki page for more information.
Here are some posts with examples of the use of singletons in objective-c:
Using the Singleton Pattern in Objective-C
Singletons: You're doing them wrong
A singleton is an instance of a class that is meant to be the only one of its kind. You usually have a class method that returns a pointer to this single instance or creates it if it didnot exist yet.
Some examples of singletons are:
NSFileManager defaultManager
NSUserDefaults standardUserdefaults
NSCharacterSet whitespaceCharacterset
Singletons are the generally accepted method to hold model data and code in a central place that you can access from everwhere in your other code. All you need to do is to add the header and get the singleton pointer via class method.
See here for a comparison of singleton versus global variables: http://www.cocoanetics.com/2009/05/the-death-of-global-variables/
Singleton object means there is only one object throughout your application. If you used any properties in a singleton class and you access this property using object of this class, which is also called singleton object, you will get the same value in you whole application until you change it manually. This concept is generally used to maintain some fix values that your application required like if your application has login process than may be you required logged in username to be carried in your application for use at any point of application.

How to adapt Apple singleton pattern for accessing SQLite database?

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/

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.

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.

Serializing objects in Objective C

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