2013-03-05 72 views
0

對於冗長的問題,我表示歉意,虐待嘗試用示例演示問題。將關係數據存儲在對象中,覆蓋值

我有三個對象

  1. 疾病
  2. 化合物
  3. 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) 

我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 

的問題是,如果我嘗試打印價值idisicom它總是打印2,無論objectAtIndex價值我給。它看起來循環中的值被覆蓋,並且它總是取循環的最後一個值,我是一個初學者objective-c,並且如果有人能夠說明我的代碼有什麼問題,我會很感激。

對不起再詳細的解釋和代碼。

+0

我看到這可以在5分鐘內使用CoreData解決! – 2013-03-05 09:14:15

+0

@AnoopVaidya任何提示或任何可能的解決方案? – 2013-03-05 09:17:06

+0

可以請你分享你的問題,根據班級圖或甚至用文字。 NO代碼,請:) – 2013-03-05 09:22:15

回答

1

[com setIdisease:&iDiseaseIndex];由於所有comdis對象的屬性疾病和化合物指的是相同的地址,它們都等於最後添加的對象的值。爲了解決這個問題,你可以在comdis中使用int屬性而不是* int