0
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 を期待する場合に機能します。より良い、またはより標準的な方法はありますか?

4

1 に答える 1

3

最初のステップは、他のすべてのステートメントを試してみることです。この setup(?) メソッドを次のように変更するだけで、読みやすくなります。

BusinessToSave.numberOfLike = @(0);
if ([dictInfoForThisBusiness[@"LikeNumber"] isNotEmpty]) {
    BusinessToSave.numberOfLike =dictInfoForThisBusiness[@"LikeNumber"];
}

BusinessToSave.numberOfDislike =@(0);
if (dictInfoForThisBusiness[@"DislikeNumber"]){
    BusinessToSave.numberOfDislike =dictInfoForThisBusiness[@"DislikeNumber"];
}

BusinessToSave.buildingName =nil;
if([dictInfoForThisBusiness[@"Building"] isNotEmpty]){
    BusinessToSave.buildingName=dictInfoForThisBusiness[@"Building"];
}

BusinessToSave.Street =nil;
if([dictInfoForThisBusiness[@"Street"] isNotEmpty]){
    BusinessToSave.Street=dictInfoForThisBusiness[@"Street"];
}

正しいオブジェクト (または nil) を提供するメソッドを作成するのはどうですか?

BusinessToSave.buildingName = [self valueForDictKey:@"Building"];

2 つ (またはそれ以上) の異なるメソッドを使用することもできます。(stringForDictKey、integerForDictKey) など。

BusinessToSave.numberOfLike = [self integerForDictKey:@"LikeNumber"];
BusinessToSave.numberOfDislike = [self integerForDictKey: @"DislikeNumber"];
BusinessToSave.buildingName = [self stringForDictKey: @"Building"];
BusinessToSave.street = [self stringForDictKey @"Street"];

また、これらすべてのキーに対していくつかの #define を作成することをお勧めします。これらのキー名のいずれかのスペルを間違えるのはあまりにも簡単です。:)

BusinessToSave.numberOfLike = [self integerForDictKey:KEY_LIKES];
BusinessToSave.numberOfDislike = [self integerForDictKey: KEY_DISLIKES];
BusinessToSave.buildingName = [self stringForDictKey: KEY_BUILDING];
BusinessToSave.street = [self stringForDictKey KEY_STREET];
于 2013-01-11T06:42:24.870 に答える