How To Add Data To App To Test Core Data Model
I have some discussion in this question regarding my core data model objects, etc : How To Achieve This Using Core Data
I want to know if and how I can add data to my App Delegate to mock the data that users will input into the app. I can then setup all the tableviews and views that use core data and make sure everything 开发者_运维知识库is working and hooking up properly.
Then once everything is all set, I can remove this data and switch it to input data from the user.
Can anyone help with this? Thanks!
You need to create a Data class where you can set the properties of variables or in your case arrays (for displaying data in UITableView). Implement a class method in data class which checks that object has been instantiated or not. If not, it does that. It is something like this :
//DataClass.h
@interface DataClass : NSObject {
NSMutableArray *nameArray;
NSMutableArray *placeArray;
}
@property(nonatomic,retain)NSMutableArray *nameArray;
@property(nonatomic,retain)NSMutableArray *placeArray;
+(DataClass*)getInstance;
@end
//DataClass.m
@implementation DataClass
@synthesize nameArray;
@synthesize placeArray;
static DataClass *instance =nil;
+(DataClass *)getInstance
{
@synchronized(self)
{
if(instance==nil)
{
instance= [DataClass new];
}
}
return instance;
}
Now in your view controller you need to call this method as :
DataClass *obj=[DataClass getInstance];
And use the arrays.
This way you can assign data without disturbing AppDelegate, which is a good practice.
精彩评论