2016-08-22 69 views
-1

我試圖添加對象(醫院)到NSDictionary當醫院的'包ID'匹配當前和它被標記爲選定。Objective-c有條件添加到NSDictionary使用布爾

選定的屬性是布爾所以我曾嘗試以下每個與if語句沒有成功:

  • hospital.selected == TRUE
  • hospital.selected == YES
  • hospital.selected == 1

所有這些產生相同的結果,如果選擇等於1或0,則它被添加。他們只會過濾掉零結果。

NSString * NAME  = @"name"; 
    NSString * PHONE = @"phone"; 
    NSString * POSTCODE = @"postcode"; 

    NSMutableArray * mArray = [NSMutableArray array]; 

    NSDictionary * dict; 

    for(ecossHospitalObject *hospital in results){ 

    @try{ 
     NSLog(@"self pack id: %i, hospital pack id: %@", self.fullDataObject.dataPack.pack_id, hospital.dataObject.dataPack.pack_id); 
     NSLog(@"hospital.selected %hhd", hospital.selected); 
     if(self.fullDataObject.dataPack.pack_id == [hospital.dataObject.dataPack.pack_id integerValue] && hospital.selected == 1){ 

       dict = [NSDictionary dictionaryWithObjectsAndKeys: 
         hospital.name, NAME, 
         hospital.telephone, PHONE, 
         hospital.postCode, POSTCODE, 
         nil]; 

       [mArray addObject:dict]; 
     } 

    }@catch(NSException *e){ 
     NSLog(@"NSException %@", e); 
    } 

    } 

哪裏hospital.selected是nil我在日誌中得到0,其中它爲0或1 I得到的隨機數(分別爲當前48和96)。使用下面的方法被創建

醫院對象:

+(NSMutableArray*) parseJsonHospitalArray : (NSMutableArray *) jsonArray 
{ 


NSMutableArray *parsedData = [[NSMutableArray alloc] init]; 


for (id object in jsonArray) { 
    ecossHospitalObject *hospitalData = [[ecossHospitalObject alloc] init]; 

    hospitalData.address = [object objectForKey:@"address"]; 
    hospitalData.code1 = [object objectForKey:@"code1"]; 
    hospitalData.code2 = [object objectForKey:@"code2"]; 
    hospitalData.distance = [self getIntForString:[object objectForKey:@"distance"]]; 
    hospitalData.hasAE = [[object objectForKey:@"has_a_and_e"] boolValue]; 
    hospitalData.ID = [self getIntForString:[object objectForKey:@"id"]]; 
    hospitalData.latitude = [object objectForKey:@"latitude"]; 
    hospitalData.longitude = [object objectForKey:@"longitude"]; 
    hospitalData.name = [object objectForKey:@"name"]; 
    hospitalData.postCode = [object objectForKey:@"postcode"]; 
    hospitalData.selected = [[object objectForKey:@"selected"] boolValue]; 
    hospitalData.telephone = [object objectForKey:@"telephone"]; 
    hospitalData.website = [object objectForKey:@"website"]; 


    [parsedData addObject: hospitalData]; 
} 

return parsedData; 


} 

這裏是打印輸出的數據:

data: ({ 
     address = nil; 
     code1 = nil; 
     code2 = nil; 
     dataObject = "0x1587d330 <x-coredata://69494056-6039-4679-9C66-7394E9795700/DataObject/p10>"; 
     distance = 0; 
     "hasA_E" = nil; 
     hospitalDirectionsArray = nil; 
     hospitalImageLocation = nil; 
     iD = 0; 
     latitude = nil; 
     longitude = nil; 
     name = "John Radcliffe Hospital - A & E"; 
     postCode = "OX3 9DU"; 
     selected = nil; 
     telephone = "01865 741166"; 
     website = nil; 
    }, 
    { 
     address = "Headley Way, Headington, Oxford, Oxfordshire"; 
     code1 = RTH08; 
     code2 = nil; 
     dataObject = "0x17741940 <x-coredata://69494056-6039-4679-9C66-7394E9795700/DataObject/p10>"; 
     distance = 14; 
     "hasA_E" = 1; 
     hospitalDirectionsArray = nil; 
     hospitalImageLocation = nil; 
     iD = 645; 
     latitude = "51.7638761156"; 
     longitude = "-1.2198152232602"; 
     name = "John Radcliffe Hospital - A & E"; 
     postCode = "OX3 9DU"; 
     selected = 1; 
     telephone = "01865 741166"; 
     website = nil; 
}, 
{ 
     address = "Oxford Rd, Banbury, Oxfordshire"; 
     code1 = RTH05; 
     code2 = nil; 
     dataObject = "0x17741940 <x-coredata://69494056-6039-4679-9C66-7394E9795700/DataObject/p10>"; 
     distance = 19; 
     "hasA_E" = 1; 
     hospitalDirectionsArray = nil; 
     hospitalImageLocation = nil; 
     iD = 623; 
     latitude = "52.053503457165"; 
     longitude = "-1.3366590559482"; 
     name = "Horton General Hospital - A & E"; 
     postCode = "OX16 9AL"; 
     selected = 0; 
     telephone = "01295 275500"; 
     website = nil; 
}, 
{ 
     address = "Standing Way, Eaglestone, Milton Keynes, Buckinghamshire"; 
     code1 = RD816; 
     code2 = nil; 
     dataObject = "0x17741940 <x-coredata://69494056-6039-4679-9C66-7394E9795700/DataObject/p10>"; 
     distance = 41; 
     "hasA_E" = 1; 
     hospitalDirectionsArray = nil; 
     hospitalImageLocation = nil; 
     iD = 789; 
     latitude = "52.026356870309"; 
     longitude = "-0.73577247002525"; 
     name = "Milton Keynes Hospital - A & E"; 
     postCode = "MK6 5LD"; 
     selected = 0; 
     telephone = "01908 660033"; 
     website = nil; 
    }, 
    { 
     address = "Mandeville Road, Aylesbury, Buckinghamshire"; 
     code1 = RXQ02; 
     code2 = nil; 
     dataObject = "0x17741940 <x-coredata://69494056-6039-4679-9C66-7394E9795700/DataObject/p10>"; 
     distance = 34; 
     "hasA_E" = 1; 
     hospitalDirectionsArray = nil; 
     hospitalImageLocation = nil; 
     iD = 1333; 
     latitude = "51.800759137118"; 
     longitude = "-0.80484777392717"; 
     name = "Stoke Mandeville Hospital - A & E"; 
     postCode = "HP21 8AL"; 
     selected = 0; 
     telephone = "01296 315000"; 
     website = nil; 
    } 
) 
+5

您發佈的任何代碼都沒有'hospital.selected'。 – rmaddy

+0

如果'selected'可以爲零,那麼它是一個對象而不是布爾值。 –

+0

@rmaddy謝謝,編輯。 – jampez77

回答

1
if(self.fullDataObject.dataPack.pack_id == [hospital.dataObject.dataPack.pack_id integerValue] 
    && hospital.selected == 1){ 

停止在那裏。永不永不從不使用對比測試作爲一個條件。您的代碼實際上是說:

if (hospital.selected == 1) 

也就是說錯誤。所以是if (hospital.selected == YES)if (hospital.selected == TRUE)。他們是全部是錯誤。他們註定要失敗和隨機行爲。

如果你設置正確,hospital.selected一個布爾值。它一個條件。所以你只是說:

if (hospital.selected) 

這是唯一的方法來一致地得到正確的答案。

編輯如果hospital.selected是一個NSNumber包裹一個布爾值,然後用它作爲條件,使用boolValue - 也就是說

if (hospital.selected.boolValue) 

如果NSNumber的是nil這將優雅地失敗,並會否則工作正常。但是不是假設它只能是0和1;這是一個重大錯誤。

+0

感謝這個答案,但問題是'hospital.selected'可以是零或1,我只需要訪問它,當它等於1.這給了我0和1. – jampez77

+0

如果它可以爲零,那麼我假定在這種情況下,它是一個包含Bool的NSNumber。因此,使用'boolValue'並按照我所說的做,因此:'if(hospital.selected.boolValue)'。不_不假設它只能是0和1.不_與0或1比較。這是我警告你的錯誤。 – matt

0
hospitalData.selected = [[object objectForKey:@"selected"]boolValue]; 

你可以直接轉換成0和1爲bool yes或no

,如果ü要布爾值是或否1或0

您可以添加像添加到服務器

selected : @(hospitalData.selected)