Objective-c使用BOOL有条件地添加到NSDictionary

Objective-c使用BOOL有条件地添加到NSDictionary,objective-c,boolean,nsdictionary,Objective C,Boolean,Nsdictionary,我正在尝试将对象(医院)添加到NSDictionary中,但医院的“pack ID”与当前匹配,并且标记为选中 所选属性是BOOL,因此我在if语句中尝试了以下各项,但均未成功: hospital.selected==TRUE hospital.selected==YES hospital.selected==1 所有这些都会产生相同的结果,如果选择的值等于1或0,则会将其相加。他们只过滤掉零的结果 NSString * NAME = @"name"; NSStri

我正在尝试将对象(医院)添加到NSDictionary中,但医院的“pack ID”与当前匹配,并且标记为选中

所选属性是BOOL,因此我在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
为零,我在日志中得到0,如果是0或1,我得到随机数(目前分别为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;
  }
)
数据:({
地址=零;
代码1=零;
代码2=零;
dataObject=“0x1587d330”;
距离=0;
“hasA_E”=零;
hospitalDirectionsArray=nil;
医院图像位置=零;
iD=0;
纬度=零;
经度=零;
name=“约翰·拉德克利夫医院-急症室”;
邮政编码=“OX3 9DU”;
所选=零;
电话=“01865 741166”;
网站=无;
},
{
地址=“牛津牛津海德顿海德利路”;
代码1=RTH08;
代码2=零;
dataObject=“0x17741940”;
距离=14;
“hasA_E”=1;
hospitalDirectionsArray=nil;
医院图像位置=零;
iD=645;
纬度=“51.7638761156”;
经度=“-1.2198152232602”;
name=“约翰·拉德克利夫医院-急症室”;
邮政编码=“OX3 9DU”;
所选=1;
电话=“01865 741166”;
网站=无;
},
{
地址=“牛津郡班伯里牛津路”;
代码1=RTH05;
代码2=零;
dataObject=“0x17741940”;
距离=19;
“hasA_E”=1;
hospitalDirectionsArray=nil;
医院图像位置=零;
iD=623;
纬度=“52.053503457165”;
经度=“-1.3366590559482”;
name=“霍顿综合医院-急症室”;
邮政编码=“OX16 9AL”;
所选=0;
电话=“01295 275500”;
网站=无;
},
{
地址=“白金汉郡米尔顿·凯恩斯伊格尔斯通站道”;
代码1=RD816;
代码2=零;
dataObject=“0x17741940”;
距离=41;
“hasA_E”=1;
hospitalDirectionsArray=nil;
医院图像位置=零;
iD=789;
纬度=“52.026356870309”;
经度=“-0.73577247002525”;
name=“米尔顿凯恩斯医院-急症室”;
邮政编码=“MK6 5LD”;
所选=0;
电话=“01908 660033”;
网站=无;
},
{
地址=“白金汉郡艾尔斯伯里曼德维尔路”;
代码1=RXQ02;
代码2=零;
dataObject=“0x17741940”;
距离=34;
“hasA_E”=1;
hospitalDirectionsArray=nil;
医院图像位置=零;
iD=1333;
纬度=“51.800759137118”;
经度=“-0.80484777392717”;
name=“斯托克曼德维尔医院-急症室”;
邮政编码=“HP21 8AL”;
所选=0;
电话=“01296315000”;
网站=无;
}
)
停在那里。切勿将比较测试用作条件测试。你的代码实际上说:

if (hospital.selected == 1)
这是错误的。如果(hospital.selected==YES)和
if(hospital.selected==TRUE)
也是如此。他们都错了。他们注定要失败和胡作非为

如果设置正确,
hospital.selected
是一个布尔值。这是一个条件。所以你只是说:

if (hospital.selected)
这是获得正确答案的唯一途径

编辑如果
hospital.selected
是一个包装布尔值的NSNumber,则要将其用作条件,请使用
boolValue
-即

if (hospital.selected.boolValue)
如果NSNumber为
nil
,则此操作将正常失败,否则将正常工作。但不要假设它只能是0和1;这是一个重大错误

hospitalData.selected = [[object objectForKey:@"selected"]boolValue];
您可以直接将中的0和1转换为布尔是或否

如果要在1或0中向服务器添加布尔值“是”或“否”

你可以像这样添加它

selected : @(hospitalData.selected)

您发布的代码中没有一个是“医院”
hospital.selected
。如果
selected
可以为零,则它是一个对象而不是布尔值。@rmaddy谢谢,已编辑。@NimitParekh isEqual:给出了“坏接收器类型”BOOL(也称为“签名字符”)使用填充该值的相关代码更新您的问题。谢谢您的回答,但问题是
医院。所选的
可以为零0或1,我只需要在它等于1时访问它。这给了我0和1。如果它可以是零,那么我假设在这种情况下它是一个包裹布尔的NSNumber。所以使用
boolValue
并按照我说的做,因此:
if(hospital.selected.boolValue)
。不要假设它只能是0和1。不要与0或1进行比较。这就是我警告你不要犯的错误。
selected : @(hospitalData.selected)