Whats the most effective way to use Jastor to create object and managedobject to store in iOS? - objective-c

given objective c doesn't do multiple inheritance, what would be the best way to implement class(es) using Jastor to convert and json data source into object and also use managedobject (core data) as storage?
The whole reason for choosing Jastor was that I want to avoid laboriously code each property/field and then to have to copy the whole lot property by property into a managedobject seems dire.
But whats the most elegant way of achieving this?
Thanks

Any NSObject subclass including NSManagedObject does just what you want out of box by means of NSKeyValueCoding protocol. See example here.

Related

How can I modify my my RestKit managed object before it is saved via Core Data?

I have RestKit setup nicely with a Core Data managed object backing but I have some fields which are not present on the server, only in the local model class.
How can I set these fields before the object is persisted. Is there something like a 'willSave' delegate method I can implement?
Thanks
I don't quite understand what you want to accomplish, but you can override willSave in NSManagedObject. The docs give a good explanation of what it does.
If you want to modify the incoming data before you save it, you should consider willMapData

Buliding a user profile with NSDictionary or NSMutableArray

I'm trying to create an application that stores profile information..
Name: first Last
Email:
Department:
Title:
What would be the best approach?...
I was thinking of using an NSDictionary for the info.
But I'm unclear on how to combine all the attributes of the profile, into the NSDictionary...
Would I create an NSDictionary for NAMES, then another for EMAILS, ect...?
Any insight is greatly appreciated, Thanks
This is directed at those who suggest a struct: I'm sorry but why even bother using Objective-C if you're not going to use Objective-C? I'm not sure where this "use a struct" trend is coming from but it's absurd when considering primary, first-class app objects that are to be heavily manipulated with the Cocoa API.
To the OP: Use an Objective-C class and be done with it.
An NSDictionary is great as an indistinct container or map and, provided everything in it is compliant, the whole thing (container and contents) can be archived and unarchived with one line of code.
In your case, you already know a predefined set of attributes (and maybe even methods) of this object you want to describe (a Profile), so create an Objective-C class and make it NSCoding compliant so you can stash it in any standard Cocoa container and have it easily archived/unarchived, etc. You can also take advantage of automagic behavior such as having a -fullName property that returns a concatenation of the first and last names while participating rather effortlessly in Key Value Observing, using NSPredicate filters, sorting by key etc. You can also implement -copyWithZone: so a Profile instance is easily copyable, etc.
"Why use a hammer on that nail? It's wasteful. Plenty of perfectly good sticks out there you can put rocks on."
Please ... just use a class. It makes functionality so much simpler to add in the long run.
I know there is a NSUserDefaults object. I haven't worked with it.
There is a tutorial that explains NSUserDefaults

Reproduce writeToURL/initWithContentsOfURL behavior with array of custom objects instead of dictionaries

I'm replacing an array of NSMutableDictionary objects with an array of custom objects. Each has 15 or so instance variables which need to persist and a couple more which are transient.
I used to read and write these using -initWithContentsOfURL: and -writeToURL:.
What's the best way to produce the same plist file with the new data structure?
It seems like one approach for saving is to recreate an array of equivalent dictionaries and call -writeToURL:, and vice versa for loading. I wonder if there's a simple way to do that, or a simpler more direct approach.
I often find that the most expedient way to do this is to create a tree of NSMutableDictionaries from the tree of custom objects and archive it. Obviously this has memory and CPU overhead, and is not strictly as nice as implementing NSCoding as suggested by skidr0w, but as I said, it's sometimes quicker and easier.
You can implement the NSCoding protocol in your class, to archive a instance of your class on disk.

Share code between classes in Objective-C without inheritance?

Since Objective-C does not support multiple inheritance, is there some other mechanism to share code between classes?
I'm writing a Cocoa library on top of an existing xml schema. Many elements in the schema (for example, ) are used in multiple types. I was hoping to centralize this somehow, so that I didn't have to add #property NSString *name and the associated code to generate the xml for the name property in every single class that has a name attribute. That would be fairly straightforward if multiple inheritance were supported, but I don't know how to do it otherwise.
The best thing I can think of is to create a common superclass that has a superset of methods to encode each optional property, then call the appropriate methods (i.e. encodeName) in the encoding method for each class. Does that seem like the best way to go?
I would recommend you create a new Category with your properties/functions etc and add said category to NSObject. That would make all properties and functions available to all subclasses of NSObject. It's not exactly multiple inheritance but provides a great amount of flexibility.
It is actually possible to add data to a class at run time using the runtime's Associative Reference functions. It's a little deeper than normal SDK work, but works quite well. It's not the same as a full property with getters and setters, but it does associate objects with an instance.
If the methods are only required on certain objects then maybe a sub class from that class is the way to go or a category on that specific class would be better. The category on NSObject is quite a wide net to cast.
If you want to add additional state then sub classing is probably your safest bet.
If you do add a category make sure you prefix your methods with something unique so to avoid any conflicts. E.g. PSMySpecialMethod as opposed to mySpecialMethod

access a variable of another class via a property

I have two classes,In classA I create a variable that I need to use in classB ,
should i use property ?
is there anyone to explain me easier ,how to set StringValue of variable in one class to the textfield of another class?
thanks
Yes, and yes:
http://www.cocoacast.com/?q=node/103
The simple answer is Yes, use properties, that is what they are for: a simple way of exposing the state of an object to other objects.
The longer answer is that Objective-C 2.0 properties are just a wrapper around the concept of Key-Value-Coding and Key-Value-Observing (KVC/KVO).
It is well worth reading the documentation for these as the concept is fundamental to the way that Cocoa works and understanding them early on in your learning process will save you a lot of trouble in the future.
And, since you will be passing object references around I might as well add a link to the Memory Management Programming Guide which will help you correctly apply the proper memory management attributes to your #property declarations.