I'm in the process of the building a simple iphone application and I had a few questions.
I need to parse through results from an API key search, and manipulate them in my program. The API is from rotten tomatoes, and I can't find a parser that works with ARC. I know the JSON kit works well for previous versions of XCode, but I really like ARC and have done my application to date using it.
1) Is there a solid parser for such results or is it something I'm going to have to do manually?
The basic structure of my app includes a search page and personal list of things, using a mutable array of objects to populate a table view.
2) whats the best way to design the classes and implementation? I know this is a vague questions so let me be more specific. I have one object with several attributes, and I want to both access a remote server and rotten tomatoes API, and store local data internally. So I have my storyboad with a controller for each tab view (there are two). Then I have my object class. Do i need to create a controller specifically for it or can I manipulate (create and delete) in other controllers?
I can give some more specifics about the application, I'm just feeling a little overwhelmed as it is my first time working with Xcode. Any help would be appreciated.
May i suggest you the NSJSONSerialization Class., it should work well with ARC as it is compatible with iOS 5 and above.
Or you can set the ARC flag to no in .m files that you would like it to be switched off. For that purpose you can refer a great answer here.
You can set a compiler flag on the implementations that aren't going to be used in ARC.
Use: -fno-objc-arc
About the first question, you can turn ARC off on specific files, this allows you to mix ARC enabled with non-ARC enabled classes in the same project,
Please refer to the answer of this question
How can I disable ARC for a single file in a project?
About the second question
Do you have two different classes that access the same api services?
If yes i suggest that you create a common remote-request class, this class will do the request for you.
For example:
Let say we have class1 (for tab1) and class2 (for tab2)
And we have classCommonRequest(for both the requests)
App delegate will hold a reference to the classCommonRequest
class1 and class2 can both access this class by using
YourAppDelegate *delegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate];
//get your refrence of request handler class
MyRequestHandler *request = [delegate myRequestHandler];
//then use functions in it
[request fetchSth];
If your question was more specific i could give you better answers :)
I hope this helped anyway
Related
I'm currently writing a Twitter client for iPhone and need a way to cache the public timeline. The data fetched is stored as 'Status' objects in an NSMutableArray.
The NSMutableArray stores 20 objects at a time.
What is the best way to do this?
You will probably want to use Core Data. You'll need to create an entity for each tweet, and then store that in your database. There is quite a lot of documentation and sample code on Core Data out there; start at Apple's Samples page.
If you don't want to play with one of the frameworks already mentioned (SQLitePersistentObjects and CoreData), you could implement your own using the NSCoding protocol on your status objects. The array should already implement NSCoding for archiving/unarchiving.
If you want to start quickly try SQLitePersistentObjects.
If you never plan to go over 20 objects you could (slightly) violate some rules from the gods of apple, and use NSUserDefaults - likely the easiest one to implement, but as the name implies, made for things like user settings, so don't use for big piles of info.
Disclaimer: If you know how to use the others, use them, this one is kinda the lazy way if you don't want to learn something better but more difficult.
I'm new in iPhone development, can you advice me how to serialize AdressBook records?
I have read stackoverflow - How do I serialize a simple object in iPhone sdk, but I still have questions:
should I extend ABRecordRef? Is it possible? Or should I implement own class NSObject?
(similar question was on Mac forums and was left without answer)
Thank you!
ABRecord is an opaque C type. It is not an object in the sense of Objective-C. That means you can not extend it, you can not add a category on it, you can not message it. The only thing you can do is call functions described in ABRecord Reference with the ABRecord as a parameter.
You could do two things to be able to keep the information referenced by the ABRecord arround:
Get the ABRecords id by ABRecordGetRecordID(). The ABRecordID is defined as int32_t so you can cast it to an NSInteger and store it wherever you like. You can later get the record back from ABAddressBookGetPersonWithRecordID () or ABAddressBookGetGroupWithRecordID(). But aware, the record could be changed or even deleted by the user or another app meanwhile.
Copy all values inside the record to a standard NSObject subclass and use NSCoding or other techniques to store it. You will then of cause not benefit from changes or additions to the record the user could have made.
Of cause you can combine both approaches.
I have an application that displays some graphics based on the properties of an object. I want that the state of that object to persist when the application is started next. I know that the app delegate has a "applicationwillterminate" function which I can use, but the problem is I have no idea how to get a hold of a pointer to the object.
I am trying to make this iphone app follow MVC principles, and I have the proper view and controllers. How does app delegates fit into the picture, and how can I use them? I read everywhere of advice saying to save application state during "applicationWillTerminate" but no concrete examples!
Can anyone point me to some literature or give me a hand?
Thanks!
See my answer to this question. If you need more help just let me know.
I'd like to have some settings that I can access from anywhere in my app. Is there a best way to implement this? Right now I'm just sticking properties in my app delegate, then access them with:
ClientAppDelegate *appDelegate = (ClientAppDelegate *)[[UIApplication sharedApplication] delegate];
settingValue = appDelegate.setting;
If they are persistent, use NSUserDefaults. If they are not, wrap them in a class and give every class that needs them a pointer. In every case you should probably make it possible to change the connection to the configuration object so that (1) the dependency gets obvious (“aha, this code’s behaviour depends on the configuration”) and (2) you can supply a custom configuration object for testing purposes. There is a great series of articles about singletons, coupling and testing by Miško Hevery. You can start by the post called Singletons are Pathological Liars and follow up from there, it will do good to your design.
Use NSUserDefaults—they're a reliable, simple way of storing application settings, and even persist between app launches.
I have an app that allows the user to view, manipulate and manage a collection of Schedule objects. Different parts of the app need access to this data. I've experimented with making the collection a singleton, but I didn't find that very clean.
I recently changed to storing the global data in the AppDelegate class and using:
MyAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.myGlobalData doSomething];
Which eliminates the need for my global data to be a singleton, but is really just taking advantage of the UIApplication singleton.
There really isn't just one primary view controller where it makes sense to store it in my case. So I was wondering what different strategies people use to address this.
If you've got a central data model that needs to be accessible to multiple controllers, there's nothing wrong with using a singleton to manage the data-- a "sharedManager" is exactly what the scenario demands. This is essentially what Apple does with their address book API for manipulating contacts. They use a C-style API but it involves getting a reference to the shared address book database and then using that in future calls.
Using the app delegate works too, but as you note it's really the same thing. You're designating the app delegate singleton as the owner of the data model, which is OK too.
NSUserDefaults can be abused in this way but that's really not its purpose. Aside from being kind of ugly, it also puts restrictions on the type of data you can store. Your own model can store whatever data you like, but NSUserDefaults is not quite so flexible.
Using SQLite or a property list or whatever to store the data to a file is really orthogonal to the question of how to manage it. From your description a singleton approach sounds reasonable, and that singleton can write to files in whatever way makes sense for the data you're working with.
Many people store everything in the AppDelegate and access the data through [[UIApplication sharedApplication] delegate].
If the data are supposed to be persisted, you could also use the NSUserDefaults singleton.
If the data are big, maybe you need SQLite.
This is for "global" data as you asked.
If your view controllers are organized in tree, a better way would be passing the required data from parent UIViewController to child UIViewController.