我有一个表如下所示
我有一个XML,其中 CLUB 的详细信息可用。每个标签都有一个值,但Images标签包含动态图像。
在此之后,我的对象如下所示
ClubDetails.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import "ClubDetailsImages.h"
@interface ClubDetails : NSManagedObject
@property (nonatomic, retain) NSString * clubarea;
@property (nonatomic, retain) NSString * clubdealhere;
@property (nonatomic, retain) NSString * clubdescription;
@property (nonatomic, retain) NSString * clubdistance;
@property (nonatomic, retain) NSString * clubemail;
@property (nonatomic, retain) NSString * clubfacility;
@property (nonatomic, retain) NSString * clubfav;
@property (nonatomic, retain) NSString * clubid;
@property (nonatomic, retain) NSString * clublat;
@property (nonatomic, retain) NSString * clublogopath;
@property (nonatomic, retain) NSString * clubname;
@property (nonatomic, retain) NSString * clubphone;
@property (nonatomic, retain) NSString * cluburl;
@property (nonatomic, retain) NSString * clubvenutype;
@property (nonatomic, retain) NSString * clublong;
@property (nonatomic, retain) NSSet *clubdetailsimages;
@end
@interface ClubDetails (CoreDataGeneratedAccessors)
- (void)addClubdetailsimagesObject:(ClubDetailsImages *)value;
- (void)removeClubdetailsimagesObject:(ClubDetailsImages *)value;
- (void)addClubdetailsimages:(NSSet *)value;
- (void)removeClubdetailsimages:(NSSet *)value;
.m 文件如下所示
@implementation ClubDetails
@dynamic clubarea;
@dynamic clubdealhere;
@dynamic clubdescription;
@dynamic clubdistance;
@dynamic clubemail;
@dynamic clubfacility;
@dynamic clubfav;
@dynamic clubid;
@dynamic clublat;
@dynamic clublogopath;
@dynamic clubname;
@dynamic clubphone;
@dynamic cluburl;
@dynamic clubvenutype;
@dynamic clublong;
@dynamic clubdetailsimages;
- (void)addClubdetailsimagesObject:(ClubDetailsImages *)value {
NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
[self willChangeValueForKey:@"ClubDetailsImages" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
[[self primitiveValueForKey:@"ClubDetailsImages"] addObject:value];
[self didChangeValueForKey:@"ClubDetailsImages" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
}
- (void)removeClubdetailsimagesObject:(ClubDetailsImages *)value {
NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
[self willChangeValueForKey:@"ClubDetailsImages" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
[[self primitiveValueForKey:@"ClubDetailsImages"] removeObject:value];
[self didChangeValueForKey:@"ClubDetailsImages" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
}
- (void)addClubdetailsimages:(NSSet *)value {
[self willChangeValueForKey:@"ClubDetailsImages" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
[[self primitiveValueForKey:@"ClubDetailsImages"] unionSet:value];
[self didChangeValueForKey:@"ClubDetailsImages" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
}
- (void)removeClubdetailsimages:(NSSet *)value {
[self willChangeValueForKey:@"ClubDetailsImages" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
[[self primitiveValueForKey:@"ClubDetailsImages"] minusSet:value];
[self didChangeValueForKey:@"ClubDetailsImages" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
}
***ClubDetailsImages.h*** looks like
@class ClubDetails;
@interface ClubDetailsImages : NSManagedObject
@property (nonatomic, retain) NSString * images;
@property (nonatomic, retain) ClubDetails *clubdetailed;
***ClubDetailsImages.m*** looks like
@implementation ClubDetailsImages
@dynamic images;
@dynamic clubdetailed;
For Saving, I wrote code like this
-(void)saveClubDetails:(NSMutableArray*)allClubs{
NSError *error;
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"ClubDetails" inManagedObjectContext:context]];
[fetchRequest setIncludesPropertyValues:NO]; //only fetch the managedObjectID
NSArray *allObject = [context executeFetchRequest:fetchRequest error:&error];
for (NSManagedObject * obj in allObject) {
[context deleteObject:obj];
}
NSError *saveError = nil;
[context save:&saveError]; // NO MORE VALUE IS DB
for (int x = 0; x<[allClubs count]; x++) {
ClubDetails *club = [NSEntityDescription insertNewObjectForEntityForName:@"ClubDetails"
inManagedObjectContext:context];
ClubDetails2 *ob = (ClubDetails2*)[allClubs objectAtIndex:x];
club.clubarea = [NSString stringWithFormat:@"%@", ob.clubarea];
club.clubdealhere = [NSString stringWithFormat:@"%@", ob.clubdealhere];
club.clubdescription = [NSString stringWithFormat:@"%@", ob.clubdescription];
club.clubdistance = [NSString stringWithFormat:@"%@", ob.clubdistance];
club.clubemail = [NSString stringWithFormat:@"%@", ob.clubemail];
club.clubfacility = [NSString stringWithFormat:@"%@", ob.clubfacility];
club.clubfav = [NSString stringWithFormat:@"%@", ob.clubfav];
club.clubid = [NSString stringWithFormat:@"%@", ob.clubid];
club.clublat = [NSString stringWithFormat:@"%@", ob.clublat];
club.clublogopath = [NSString stringWithFormat:@"%@", ob.clublogopath];
club.clubname = [NSString stringWithFormat:@"%@", ob.clubname];
club.clubphone = [NSString stringWithFormat:@"%@", ob.clubphone];
club.cluburl = [NSString stringWithFormat:@"%@", ob.cluburl];
club.clubvenutype = [NSString stringWithFormat:@"%@", ob.clubvenutype];
club.clublong = [NSString stringWithFormat:@"%@", ob.clublong];
ClubDetailsImages *clubImages = [NSEntityDescription insertNewObjectForEntityForName:@"ClubDetailsImages"
inManagedObjectContext:context];
clubImages.images = [NSString stringWithFormat:@"veer url image"];
[club addClubdetailsimagesObject:clubImages];
}
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
// NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Clubs"
inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedArray = [context executeFetchRequest:fetchRequest error:&error];
NSLog(@"COUNT of arary is %d", [fetchedArray count]);
for (Clubs *info in fetchedArray) {
NSLog(@" Duaan Name ~~~~ : %@", info.clubname);
}
}
我对以下几点感到不满
1。如何在CoreData中保存这样的对象,其中一个俱乐部可能是3,4,5个存储在数组中的图像?
2。如何从CoreData获取一对多的关系?
答案 0 :(得分:1)
只需继续添加对象并将它们链接到ClubDetails,例如,您可以从某处获得一系列图像字符串....
for (NSString *image in images)
{
ClubDetailsImages *clubImages = [NSEntityDescription insertNewObjectForEntityForName:@"ClubDetailsImages" inManagedObjectContext:context];
clubDetailsImages.image = image;
clubDetailsImages.clubdetailed = club;
}
不确定你在这里问什么,但给出了ClubDetails实例:
NSSet *images = club.clubdetailsimages;
另外,为什么要实现addClubdetailsimagesObject等。这不是必需的。使用XCode生成实体类,您将看到未创建这些方法。