Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Reasoning behind the macro

#define KINDOF_CAST(arg) ((__kindof typeof(arg))(arg))

UICollectionViewFlowLayout* flowLayout = KINDOF_CAST(self.collectionView.collectionViewLayout);

The macro works at compile time. It relies on the collectionViewLayout property declaration type. So the expression results in

UICollectionViewFlowLayout* flowLayout = (__kindof UICollectionViewLayout)(self.collectionView.collectionViewLayout);

The __kindof UICollectionViewLayout type silences the compiler's "type mismatch" warnings because UICollectionViewFlowLayout is a child of UICollectionViewLayout.

The templates above do the runtime checks so these casts might co-exist depending on the "safety" level chosen by the programmer.

#define KINDOF_CAST(arg) ((__kindof typeof(arg))(arg))
// By Scherbinin Anatoly from the cocoa chat ("Какао-чат: Cocoa, Xcode, objective C")
UICollectionViewFlowLayout* flowLayout = KINDOF_CAST(self.collectionView.collectionViewLayout);
@dodikk
Copy link
Author

dodikk commented Oct 4, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment