Subclass NSDate, no changing behaviour, just add a tag? - nsdate

I need to sort I need to subclass NSDate to add a tag, I don't need to change its behaviour, do I still need to implement my own:
(NSTimeInterval)timeIntervalSinceReferenceDate
(id)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)seconds;
as stated in apple's documentation? If so, how do I do it?
currently I just created a class extends NSDate with a tag attribute, but how do I initialise it?
Thanks!

NSDate is a class cluster, not a simple class. It is not wise to subclass a class cluster unless you know exactly what you are doing; see How do I subclass NSDate?
I would recommend that you create your own class containing an NSDate and a tag, and extend as needed:
#interface MyClass : NSObject {
NSDate *_date;
NSUInteger _tag;
}
#end
Add the appropriate properties, etc for easy dot-notation access.

Related

Add readonly attributes (getters) to NSManagedObject

I have a ruby class and a match definition in core data.
There is a NSData field 'image_data' to store my image.
In my ruby class, i was thinking to add a getter to return an UIImage.
However, i get an error saying "undefined method `image' for #"
What do i need to do to get around this?
class ActivityImage < NSManagedObject
def image
UIImage.imageWithData(image_data)
end
end
I think what you're looking for is a way of creating a "Transformable Attribute".
If you read through that guide, you'll get to understand what transformable attributes are, hopefully you're using Xcode's model editor so you can just create the attribute in there, but otherwise you'll just have to pull some hackery to get the entity then create the image attribute and set it as transformable.

Getting started with MagicalRecord

I have an iPad app, using Storyboards, XCode 4.5 and iOS 6. I am trying to get started using MagicalRecord. I have everything installed and it builds correctly. I have created an empty entity called ClientInfo with attributes. I have also generated the NSManagedObject (Editor > Create NSManagedObject Subclass… > Create) but don't know what to do with it either.
This is the instruction that is my "brick wall"; I am unable to follow the following instruction:
import the NSManagedObject subclass of the entity you want to save
into you’re class
I have looked at examples, Googled it and looked at SO. I can't find a good example from starty to finish. Anybody know of a real good doc or example on MagicalRecord? Or, show me a real simple example that explains this where the MagicalRecord does not?
In Xcode, select the entities in the Core Data model editor and choose "Editor -> Create NSManagedObject Subclass ..." from the menu. This creates .h and .m for each entity, e.g. ClientInfo.h and ClientInfo.m.
Import the NSManagedObject subclass in your class file, e.g. #import "ClientInfo.h".
Note that you have to redo step 1 after modifiying the entities.
The NSManagedObject subclasses declare all the properties and methods for you entity, so you can (for example) write
clientInfo.name = #"foo";
instead of
[clientInfo setValue:#"foo" forKey:#"name"];

CoreData - Primitive accessors as properties?

Reading through Apple's docs here, the example code declares the primitive accessor and mutator as separate methods in a category. Is it possible to use #property to declare the primitives? Is there a disadvantage to do it this way; it seems like it saves boiler plate code, but are there performance implications in the way the methods are generated when declared this way? What are the optional attributes - (strong, nonatomic)?
Apple's Example Code
#interface Department : NSManagedObject
#property(nonatomic, retain) NSString *name;
#end
#interface Department (PrimitiveAccessors)
- (NSString *)primitiveName;
- (void)setPrimitiveName:(NSString *)newName;
#end
Suggested Change
#interface Department : NSManagedObject
#property(nonatomic, retain) NSString *name;
#end
#interface Department (PrimitiveAccessors)
#property (strong, nonatomic) NSString *primitiveName;
#end
You should generate classes for your Core Data entities automatically. You do this by pressing the Editor menu and selecting "Create NSManagedObject subclass". This will automatically create a class for each of your entities, and those will have the properties you want.
If you want to customize these classes I strongly recommend that you do it in a category. I recommend this because, you may want to go back to your datamodel and add or remove an attribute, and thus you will have to generate new subclasses. In that event, any changes you may've made to the subclasses will be overwritten and that can be painful :)

subclass uitableview already in a UIView

I am trying to implement Leah's "Pull to Refresh" code (https://github.com/leah/PullToRefresh) in to my UITableView. However, I have a UIView, so cannot subclass the tableViewController as is required by this.
My structure is
UIView
- UITableView
So there's a UITableView inside my main UIView. I use a UIViewController obviously - and this cannot really change (I think!)
I have tried to change the class in interface builder to the custom uitableviewcontroller above (the pull to refresh one) but it doesn't let me.
Any ideas on how I can subclass the UITableView - NOT the tableViewController??
Here is how I did it:
Change PullToRefreshTableViewController so that it subclasses UIViewController, NOT UITableViewController. Next, add a UITableView * property called tableView to PullToRefreshTableViewController and synthesize it. Lastly, modify your view controller so that it subclasses PullToRefreshTableViewController instead of UIViewController.
That should give you a working implementation of it.

NSFetchRequest data into a view?

I am just getting started with the CoreData API and am following a few tutorials. I get the basics of storing and retrieving objects, but am having trouble connecting all the pieces in terms of MVC.
I have a CustomView into which I draw some stuff with CoreAnimation, including some text layers that will get their strings from an NSManagedObject. I started with a basic CoreData application template so the managedObjectContext etc are declared in the appDelegate, and I'm just not sure how I should be getting data from the CoreData stack into the view. By the way, this is all in code, not interface builder.
So my question is, if I want to build my app in a pure MVC way, how should I go about getting data from the stack into the view? How should I give my view access to the initialized NSManagedObjectContext, for example?
I have been reading Cocoa Design Patterns but am a bit of a dunce when it comes to MVC. I know its a pretty general question, but if someone can just say, "set the delegate, grab a pointer..." whatever it is, that would be great!
Thanks in advance!
So I did some more poking around and it seems the easiest way to do this is to get a pointer to the AppDelegate and then a pointer to the managedObjectContext. From there, execute a fetch request and put it into your view!
In a good tutorial Björn Sållarp does it this way:
From the app delegate he creates the rootViewController and sends it the context with:
RootViewController *rootViewController = [[RootViewController alloc] initWithStyle:UITableViewStylePlain];
rootViewController.managedObjectContext = context;
rootViewController.entityName = #"Counties";
In the rootViewController's h file you declare:
NSManagedObjectContext *managedObjectContext;
Create its property:
#property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
and in m you
#synthesize managedObjectContext;
Then its there for your use.

Resources