gpt4 book ai didi

objective-c - 如何使用 NSManagedObjectSubClass 从 CoreData Base 获取所有记录?

转载 作者:技术小花猫 更新时间:2023-10-29 10:19:30 25 4
gpt4 key购买 nike

我正在尝试在 ios 应用程序中实现 CoreData,现在我想从实体 MUSTHAFA 中获取所有记录

  My  NSManagedObjectedSubClass is MUSTAHFA

MUSTHAFA.m

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@interface MUSTHAFA : NSManagedObject {
@private
}
@property (nonatomic, retain) NSString * FirstName;
@property (nonatomic, retain) NSNumber * Age;
@property (nonatomic, retain) NSString * Location;
@property (nonatomic, retain) NSString * LastName;

@end

#import "MUSTHAFA.h"


@implementation MUSTHAFA
@dynamic FirstName;
@dynamic Age;
@dynamic Location;
@dynamic LastName;

@end

向核心数据添加记录

-(void)AddRecordToCoreData{

//NSLog(@"______ ADD Core Data Implementaion");
MUSTHAFA *event = (MUSTHAFA *)[NSEntityDescription insertNewObjectForEntityForName:@"MUSTHAFA" inManagedObjectContext:managedObjectContext];
[event setLastName:@"JOHN"];
[event setFirstName:@"JOSE "];
[event setLocation:@"IDUKKI "];
[event setAge:[NSNumber numberWithInt:25]];
NSError *error;
if (![managedObjectContext save:&error])
{
NSLog(@"Error..%@",error);
}
else
{
NSLog(@"Data added to MUSTHAFA ");
}
}

从 Core Data 中检索所有值,就像 select * from dbTable 一样;

-(void)FetchRecordFromCoreData:(id)data1{

//NSLog(@"______ Fetch Core Data Implementaion");
MUSTHAFA *event = (MUSTHAFA *)[NSEntityDescription insertNewObjectForEntityForName:@"MUSTHAFA" inManagedObjectContext:managedObjectContext];

//USING event how can fetch all record from Data base;


}

USING 事件(MUSTAHFA 的实例)如何从数据库中获取所有记录?

最佳答案

NSManagedObjectContext *context = //Get it from AppDelegate

NSFetchRequest *request = [[NSFetchRequest alloc]initWithEntityName:@"MUSTHAFA"];

NSError *error = nil;

NSArray *results = [context executeFetchRequest:request error:&error];

if (error != nil) {

//Deal with failure
}
else {

//Deal with success
}

[request release];

关于objective-c - 如何使用 NSManagedObjectSubClass 从 CoreData Base 获取所有记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10297542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com