Wednesday, June 14, 2017

Even Swiftier Objective-C

Peter Steinberger and Matej Bukovinski:

In our original “Swifty Objective-C” blog post we talked about the C++ auto keyword and how it’s great for preserving type information while also making the code more readable and easier to write. This is especially true when dealing with generics or block types. Since then, Objective-C learned the same trick via the new __auto_type keyword. Since nobody wants to type __auto_type all day long, we decided to make things nicer by defining Swift-like macros for it.

[…]

There’s only one copy selector, and it’s not generic. It’s also not a case where returning instancetype would be the right fix, as there’s no general way to understand what a mutable counterpart of a class pair is called and if one even exists. […] However, we can just add that ourselves! Again, a header-only declaration that simply redefines copy on our collections. This won’t magically add types to every single object, but it will solve the common case of (mutable) copying collections

[…]

We define a block and add __attribute__((cleanup)) to it that tells the compiler to execute the function defined in that attribute. We pass along the block as a parameter and thus execute the block when the scope is exited.

Previously: Swifty Objective-C, foreach Using Objective-C Generics.

1 Comment RSS · Twitter

The only thing that came to mind: https://www.youtube.com/watch?v=I1188GO4p1E (Get Schwifty!)

Leave a Comment