我为这个冗长的问题道歉,我试着用例子说明问题。
我有三个对象
- 疾病
- 化合物
- Comdis
醇>
我将疾病和化合物列表存储在各自的类中。
化合物和疾病之间的关系应存储在名为Comdis的对象列表中,Comdis存储对(COMPOUND,DISEASE)。
要存储在对象中的示例信息是。
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 )
我的疾病。就像这样。
@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
disease.m具有以下代码
#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
我的化合物与疾病对象非常相似。
现在在comdis我想存储关系。
我的comdis.h看起来像这样
@interface comdis: NSObject
{
int *icompound;
int *idisease;
}
@property (nonatomic,) int *icompound;
@property (nonatomic,) int *idisease;
@property NSMutableArray *list;
- (id)initWithComdisList;
@end
这是我的comdis.m
#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
问题是,如果我尝试打印idis
或icom
的值,它始终会打印2
,而不管我给出的objectAtIndex
值。似乎循环中的值被覆盖了,它总是占用循环的最后一个值,我是objective-c的初学者,并且如果有人可以对我的代码的错误有所了解,那将会很感激。
对于冗长的解释和代码再次感到抱歉。
答案 0 :(得分:1)
[com setIdisease:&iDiseaseIndex];
因为所有comdis对象的属性疾病和复合引用相同的地址,它们都等于最后添加的对象的值。要解决此问题,您可以在comdis中使用 int 属性而不是 * int 。