Re: Good idea/bad idea?
Re: Good idea/bad idea?
- Subject: Re: Good idea/bad idea?
- From: Lee Ann Rucker <email@hidden>
- Date: Thu, 24 Apr 2014 15:03:03 -0700
That's the flip side of what I was thinking about, but also useful. I was thinking about the code that receives the dictionary:
- (void)processDictionary:(NSDictionary *)d
{
foo = [[d valueForKey:@"foo"] orIfNull:fooDefault];
...
On Apr 24, 2014, at 2:56 PM, Andy Lee wrote:
> On Apr 24, 2014, at 5:39 PM, Lee Ann Rucker <email@hidden> wrote:
>> An orIfNull: for NSObject and NSNull might be nice for those times when you put placeholders in dictionaries.
>
> For that you really only need a method in one place. I'd be inclined to put it in NSNull:
>
> + (id)nullIfNil:(id)obj
> {
> return obj ?: [self null];
> }
>
> And then in constructing your dictionary you'd have code like
>
> NSDictionary *myLookup = @{ key1, [NSNull nullIfNil:value1],
> key2, [NSNull nullIfNil:value2],
> ... };
>
> For brevity I might not use a method at all; since this is a special case where there is only one variable, a function or macro is much less clunky:
>
> NSDictionary *myLookup = @{ key1, ObjOrNull(value1),
> key2, ObjOrNull(value2),
> ... };
>
> --Andy
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden