0

我为这个冗长的问题道歉,我试着用例子来证明这个问题。

我有三个对象

  1. 疾病
  2. 化合物
  3. 康迪斯

我将疾病和化合物列表存储在各个类别中。

化合物和疾病之间的关系将存储在名为 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 )

我的 disease.h 看起来像这样。

@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

我的compound.m 与疾病对象非常相似。

现在在 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它总是打印。似乎循环中的值被覆盖了,它总是取循环的最后一个值,我是objective-c的初学者,如果有人能对我的代码有什么问题有所了解,我将不胜感激。2objectAtIndex

再次抱歉冗长的解释和代码。

4

1 回答 1

1

[com setIdisease:&iDiseaseIndex];因为所有 comdis 对象的属性 disease 和 Compound 都指向同一个地址,所以它们都等于最后添加的对象的值。为了克服这个问题,您可以在 comdis 中使用int属性而不是*int 。

于 2013-03-05T13:14:13.503 回答