如何向应用程序添加数据以测试核心数据模型 [英] How To Add Data To App To Test Core Data Model

查看:67
本文介绍了如何向应用程序添加数据以测试核心数据模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个问题上有一些关于我的核心数据模型对象的讨论,例如:

I have some discussion in this question regarding my core data model objects, etc : How To Achieve This Using Core Data

我想知道是否以及如何将数据添加到我的App Delegate中,以模拟用户将输入到应用程序中的数据.然后,我可以设置所有使用核心数据的表视图和视图,并确保一切正常工作并正确连接.

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!

推荐答案

您需要创建一个Data类,您可以在其中设置变量或案例数组的属性(用于在UITableView中显示数据).在数据类中实现一个类方法,该方法检查对象是否已实例化.如果没有,它将执行此操作.就像这样:

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];  

并使用数组.
这样,您可以分配数据而不会打扰AppDelegate,这是一个好习惯.

And use the arrays.
This way you can assign data without disturbing AppDelegate, which is a good practice.

这篇关于如何向应用程序添加数据以测试核心数据模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆