Objective c 将关系数据存储在对象中,覆盖值
我为这个冗长的问题道歉,我试着用例子来说明这个问题 我有三个目标Objective c 将关系数据存储在对象中,覆盖值,objective-c,Objective C,我为这个冗长的问题道歉,我试着用例子来说明这个问题 我有三个目标 疾病 复合物 康迪斯 我将疾病和化合物的列表存储在各自的类别中 化合物和疾病之间的关系存储在一个名为Comdis的对象列表中,Comdis存储成对的(化合物,疾病) 要存储在对象中的示例信息包括 DISEASES index acronim fullname 1 AML, Acute Myelogenous Leukemia 2 PV, Polycytemia Vera 3
DISEASES
index acronim fullname
1 AML, Acute Myelogenous Leukemia
2 PV, Polycytemia Vera
3 MF, Mielofibrosis
COMPOUNDS
index acronim fullname
1 LBH589, Panobinostat
2 INC424, Ruxolitinib
3 BKM120, Buparsinib
RELATIONS (COMDIS)
index disease compound
1 ( 0 , 1 )
2 ( 0 , 2 )
3 ( 0 , 3 )
4 ( 1 , 1 )
5 ( 1 , 2 )
6 ( 1 , 3 )
7 ( 2 , 1 )
8 ( 2 , 2 )
9 ( 2 , 3 )
我的病。h看起来像这样。
disease.m有以下代码
这是我的comdis.m
#导入“comdis.h”
#导入“composite.h”
#输入“disease.h”
@实施comdis
@合成idisease,I复合物;
-(id)initWithComdisList{
复合*comp=[[component alloc]initWithCompoundList];
疾病*dis=[[disease alloc]initWithDiseaseList];
NSArray*复合阵列=复合列表;
NSArray*diseaseArray=dis.list;
NSMutableArray*数组=[NSMutableArray];
对于(int i=0;i<[diseaseArray count];i++){
对于(int j=0;j<[compoundArray count];j++){
int ICompondIndex=[compoundArray indexOfObject:compoundArray[j]];
int iDiseaseIndex=[diseaseArray IndexOfoObject:diseaseArray[i]];
comdis*com=[[comdis alloc]init];
[com setIdisease:&iDiseaseIndex];
[com setIcompound:&ICOMPOUNDEX];
[数组addObject:com];
}
}
comdis*comdisbj=[self.list objectAtIndex:1];
int idis=*(comdisObj.idisease);
int-icom=*(comdisObj.icompound);
NSLog(@“%d”,idis);
NSLog(@“%d”,icom);
回归自我;
}
@结束
问题是,如果我尝试打印idis
或icom
的值,它总是打印2
,而不管我给出的objectAtIndex
值如何。似乎循环中的值被覆盖了,它总是取循环的最后一个值,我是objective-c的初学者,如果有人能解释一下我的代码有什么问题,我将不胜感激
再次为冗长的解释和代码感到抱歉。
[com setIdisease:&idiseindex]代码>因为所有comdis对象的属性disease和component都引用相同的地址,所以它们都等于最后添加的对象的值。为了解决这个问题,您可以在comdis中使用int属性而不是*int。我发现使用CoreData可以在5分钟内解决这个问题@AnoopVaidya:有什么提示或任何可能的解决方案吗?你能用类图甚至用文字来分享你的问题吗。请不要输入代码,请输入:)
@interface disease: NSObject
{
NSString __strong *acronim;
NSString __strong *fullname;
int backcolor;
UIImage __strong *background;
}
@property (nonatomic) int backcolor;
@property (nonatomic, strong) UIImage *background;
@property (nonatomic, strong) NSString *acronim;
@property (nonatomic, strong) NSString *fullname;
@property NSMutableArray *list;
- (id)initWithDiseaseList;
- (int)getIndexByAcronim:(NSString *)acronim;
@end
#import "disease.h"
@implementation disease
@synthesize acronim, fullname, backcolor, background;
-(id)initWithDiseaseList {
disease *aml = [[disease alloc] init];
[aml setAcronim:@"AML"];
[aml setFullname:@"Acute Myelogenous Leukemia"];
disease *pv = [[disease alloc] init];
[pv setAcronim:@"PV"];
[pv setFullname:@"Polycytemia Vera"];
disease *mf = [[disease alloc] init];
[mf setAcronim:@"MF"];
[mf setFullname:@"Mielofibrosis"];
NSMutableArray *array = [NSMutableArray array];
[array addObject:aml];
[array addObject:pv];
[array addObject:mf];
self.list = array;
return self;
}
- (int)getIndexByAcronim:(NSString *)accr {
NSArray *array = self.list;
for(int i = 0; i < [array count]; i++) {
disease *disease = [array objectAtIndex:i];
if(disease.acronim == accr) {
return i;
}
}
return -1;
}
@end
@interface comdis: NSObject
{
int *icompound;
int *idisease;
}
@property (nonatomic,) int *icompound;
@property (nonatomic,) int *idisease;
@property NSMutableArray *list;
- (id)initWithComdisList;
@end
#import "comdis.h"
#import "compound.h"
#import "disease.h"
@implementation comdis
@synthesize idisease, icompound;
- (id)initWithComdisList {
compound *comp = [[compound alloc] initWithCompoundList];
disease *dis = [[disease alloc] initWithDiseaseList];
NSArray *compoundArray = comp.list;
NSArray *diseaseArray = dis.list;
NSMutableArray *array = [NSMutableArray array];
for(int i = 0; i < [diseaseArray count]; i++) {
for(int j = 0; j < [compoundArray count]; j++) {
int iCompoundIndex = [compoundArray indexOfObject:compoundArray[j]];
int iDiseaseIndex = [diseaseArray indexOfObject:diseaseArray[i]];
comdis *com = [[comdis alloc] init];
[com setIdisease:&iDiseaseIndex];
[com setIcompound:&iCompoundIndex];
[array addObject:com];
}
}
comdis *comdisObj = [self.list objectAtIndex:1];
int idis = *(comdisObj.idisease);
int icom = *(comdisObj.icompound);
NSLog(@"%d", idis);
NSLog(@"%d", icom);
return self;
}
@end