我创建了一个对象,其中包含最终将管理核心数据中的数据的属性。
我想在此对象中创建方法来设置将保存到核心数据中的数据,以及从核心数据获取所有数据并在对象中设置属性的方法,这样我就可以将所有内容包含在一个对象中。 / p>
我用来设置对象属性的对象没有正确设置它。它将它们设置在方法的一部分内,但在方法的另一部分中,在方法之外,属性为null。
这是一个范围问题,我显然没有理解,所以我转向专家。请告知我误入歧途的地方。再次感谢您的帮助! 编
对象定义: .m文件
@implementation GenObject
@synthesize name = _name;
@synthesize address = _address;
- (void)getGenData
{
MyAppDelegate *genDataAppDelegate = [[UIApplication sharedApplication] delegate]; // Create a variable to the app delegate
NSManagedObjectContext *genDataContext = [genDataAppDelegate managedObjectContext]; // Create a context
NSEntityDescription *getDataEntityDesc = [NSEntityDescription entityForName:@"GeneralData" inManagedObjectContext:genDataContext]; // Create a description for the core data
NSFetchRequest *genDataRequest = [[NSFetchRequest alloc] init]; // Create a Fetch request for the core data
[genDataRequest setEntity:getDataEntityDesc]; // Set the fetch requests entity description
NSManagedObject *genDataMatches = nil; // Create a managed object
NSError *genDataCoreError; // Create an error object
NSArray *genDataCoreObjectsArray = [genDataContext executeFetchRequest:genDataRequest error:&genDataCoreError]; // Fetch the core data and load the objects array
int genDataCoreObjectsCount = [genDataCoreObjectsArray count]; // Get the count of the objects fetched
if ([genDataCoreObjectsArray count] == 0) // Check to see if there were any objects fetched
{
//NSLog(@"No matches");
}
else // If there were objects fetched then loop over them
{
for(int y = 0; y < genDataCoreObjectsCount; y++) // Loop over the objects fetched
{
genDataMatches = [genDataCoreObjectsArray objectAtIndex:y]; // Get the current object
self.name = [[genDataMatches valueForKey:@"name"] retain];
self.address = [[genDataMatches valueForKey:@"address"] retain];
NSLog(@"BASE URL %@",self.baseurl); // WORKS HERE!!!
}
}
NSLog(@"BASE URL %@",self.baseurl); // DOES NOT WORK HERE!!!
}
@end
.h文件
@interface GenObject : NSObject
{
NSString *_name;
NSString *_address;
}
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *address;
- (void)setGenIntitalData;
@end
如果我尝试从一个不同的类中调用它,就像说ViewController一样...... 注意:在viewController中,genDataObj是viewController的属性
- (void)viewDidLoad
{
self.genDataObj = [GenData new];
[self.genDataObj setGenIntitalData]; // Setup the gendata core data
[self.genDataObj getGenData]; // Set up the gen data from the core data
NSLog(@"NAME: %@", self.genDataObj.name); // NAME IS nil HERE!!!!
[super viewDidLoad];
}
答案 0 :(得分:0)
您应该确认您的NSManagedObject实际上是在返回数据。试试这个:
- (void)getGenData
{
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"GeneralData"];
NSManagedObject *genData = nil;
NSError *fetchError = nil;
NSArray *genDataObjects = [context executeFetchRequest:request error:&fetchError];
[request release];
if( fetchError )
NSLog(@"%s - ERROR fetching genData: %@,%@",__FUNCTION__,fetchError,[fetchError userInfo]);
else
{
if( [genDataObjects count] == 0 )
NSLog(@"%s - No matches for genData",__FUNCTION__);
else
{
genData = [genDataObjects objectAtIndex:0];
NSLog(@"%s - genData.name = %@",__FUNCTION__,[genData valueForKey:@"name"]);
self.name = [genData valueForKey:@"name"];
self.address = [genData valueForKey:@"address"];
}
}
}
在setGenIntitalData
中,您是否将更改保存到持久性存储中?我假设在setGenIntitalData
你正在创建初始设置;但如果未保存NSManagedObject
,则后续提取将产生无效数据。