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;
}
明らかに、これは 1 つの単純な理由で非常に面倒です。
BusinessToSave
マネージドオブジェクトです。
多くの場合、単純に nil になる必要があります。ただし、ディクショナリに nil 値を含めることはできず、代わりに NULL を返します。
だから私はただすることはできませんBusinessToSave.buildingName = dictInfoForThisBusiness[@"Building"]
じゃあどうすればいいの?
私が試したことの1つは次のとおりです。
BusinessToSave.buildingName =(NSString*) [dictInfoForThisBusiness[@"Building"] nilIfNull];
私が試したもう一つのことは
BusinessToSave.buildingName =((NSString*) dictInfoForThisBusiness[@"Building"]).nilIfNull;
nilIfNull が id を返すのに、ドット表記が機能する理由がわかりません
-(id) nilIfNull
{
if ([self isKindOfClass:[NSNull class]])
{
return self;
}
else
{
return nil;
}
}
私は最終的に解決します
[dictInfoForThisBusiness[@"Building"] nilIfNull]
これは、NSString を期待する場合に機能します。より良い、またはより標準的な方法はありますか?