In Smalltalk, where nil is an object like everything else, and we were working with calls out to C APIs that had a lot of required parameters, we added an "orIfNil:" that was very useful:
foo := bar orIfNil:10. Nil orIfNil:other ^other Object orIfNil:other ^self It would be useful in ObjC if there were an elegant way to do it. Macros are just clunky. On Apr 24, 2014, at 8:12 AM, Luther Baker wrote: > Not native and I've no idea when or if this is a good idea ... nor am I sure > how much typing you want to do ... but you _could_ create a class convenience > method for this > > x = [Thing defaultIfNil:x]; > > With shorter or longer names as you see fit ... down to possibly: > > x = [Thing :x] > > I've really no idea if that syntax would work but it stands out and might be > easy to use and identify in the code! > > -Luther > > >> On Apr 24, 2014, at 9:33 AM, Alex Zavatone <z...@mac.com> wrote: >> >> I was just asked yesterday if there is any shorthand in Objective-C for "if >> this thing = nil, then instantiate a new instance from the class" >> >> Something like this: >> >> NSString x; >> >> if ([x isEqualtoString:nil]) { >> x = @"yo"; >> } >> >> Feel free to replace NSString with any class. >> >> And we messed around a bit looking for any shorthand and though it looked >> like a terrible idea since the comparison is done against integers using the >> ternary operator, I'd like to know exactly why it's a terrible idea. >> >> NSString x; >> >> x = (x) ?: @"yo"; >> >> Thanks in advance. >> - Alex >> >> _______________________________________________ >> >> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) >> >> 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: >> https://urldefense.proofpoint.com/v1/url?u=https://lists.apple.com/mailman/options/cocoa-dev/lutherbaker%2540gmail.com&k=oIvRg1%2BdGAgOoM1BIlLLqw%3D%3D%0A&r=yJFJhaNnTZDfFSSz1U9TSNMmxGyib3KjZGuKfIhHLxA%3D%0A&m=6T3UPQaZo%2FpOzrcZ5K8HUhNh7uHOsfGoVY1bHCvudI4%3D%0A&s=35234c047f9691472c3b6bb62075ae02d99cab0034ff682df0b44aee655ac27e >> >> This email sent to lutherba...@gmail.com > > _______________________________________________ > > Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) > > 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: > https://urldefense.proofpoint.com/v1/url?u=https://lists.apple.com/mailman/options/cocoa-dev/lrucker%2540vmware.com&k=oIvRg1%2BdGAgOoM1BIlLLqw%3D%3D%0A&r=yJFJhaNnTZDfFSSz1U9TSNMmxGyib3KjZGuKfIhHLxA%3D%0A&m=6T3UPQaZo%2FpOzrcZ5K8HUhNh7uHOsfGoVY1bHCvudI4%3D%0A&s=7334f62adc31bd2a1e8d5e29e538694c8e2ee44b0b125b0d6f8b087cef0dbca7 > > This email sent to lruc...@vmware.com _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com