我試圖添加對象(醫院)到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;
}
)
您發佈的任何代碼都沒有'hospital.selected'。 – rmaddy
如果'selected'可以爲零,那麼它是一個對象而不是布爾值。 –
@rmaddy謝謝,編輯。 – jampez77