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;
}

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?

user4234
  • 1,523
  • 1
  • 20
  • 37

1 Answers1

3

A first step would be to try and rid yourself of all those else statements. Simply changing this setup(?) method to something like this would make it more readable:

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"];
}

How about creating a method to give you the correct object (or nil):

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

You could even have two (or more) different methods. (stringForDictKey, integerForDictKey) etc.

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

Also: I'd advice you to make some #defines for all those keys, it is way too easy to misspell one of those key-names. :)

BusinessToSave.numberOfLike = [self integerForDictKey:KEY_LIKES];
BusinessToSave.numberOfDislike = [self integerForDictKey: KEY_DISLIKES];
BusinessToSave.buildingName = [self stringForDictKey: KEY_BUILDING];
BusinessToSave.street = [self stringForDictKey KEY_STREET];
T. Benjamin Larsen
  • 6,373
  • 4
  • 22
  • 32