if ([dictInfoForThisBusiness[@"LikeNumber"] isNotEmpty]) {
BusinessToSave.numberOfLike =dictInfoForThisBusiness[@"LikeNumber"];
}
else
{
BusinessToSave.numberOfLike = @(0);
}
if (dictInfoForThisBusiness[@"DislikeNumber"])
{
BusinessToSave.numberOfDislike =dictInfoForThisBusiness[@"DislikeNumber"];
}
else
{
BusinessToSave.numberOfDislike =@(0);
}
//BusinessToSave.buildingName =dictInfoForThisBusiness[@"Building"];
if([dictInfoForThisBusiness[@"Building"] isNotEmpty]){
BusinessToSave.buildingName=dictInfoForThisBusiness[@"Building"];
}
else
{
BusinessToSave.buildingName =nil;
}
if([dictInfoForThisBusiness[@"Street"] isNotEmpty]){
BusinessToSave.Street=dictInfoForThisBusiness[@"Street"];
}
else
{
BusinessToSave.Street =nil;
}
Obviously this is quite messy for one simple reason.
BusinessToSave
is a managedObject.
Often it should simply go to nil. However, dictionary cannot contain nil value and would return NULL instead.
So I can't just do BusinessToSave.buildingName = dictInfoForThisBusiness[@"Building"]
What should I do then?
One thing I tried is:
BusinessToSave.buildingName =(NSString*) [dictInfoForThisBusiness[@"Building"] nilIfNull];
Another thing I tried is
BusinessToSave.buildingName =((NSString*) dictInfoForThisBusiness[@"Building"]).nilIfNull;
I have no idea why the dot notation could work given that nilIfNull returns id
-(id) nilIfNull
{
if ([self isKindOfClass:[NSNull class]])
{
return self;
}
else
{
return nil;
}
}
I finally settle for
[dictInfoForThisBusiness[@"Building"] nilIfNull]
which works if I expect NSString. Are there better or more standard way?