What is different from @compatibility_alias and using typedef to @class in Objective-C

Is there any difference between:

@compatibility_alias AliasClassName ExistingClassName



typedef AliasClassName ExistingClassName;



source to share

1 answer

They have a different way to use it. @compatibility_alias is an Objective-C reserved word, @compatible_alias allows existing classes to be used aliased by a different name.

For example, PSTCollectionView uses @compatibility_alias to greatly improve the experience of using backward compatibility to replace UICollectionView:

// Allows code to just use UICollectionView as if it would be available on iOS SDK 5.
// http://developer.apple.    com/legacy/mac/library/#documentation/DeveloperTools/gcc-3.   3/gcc/compatibility_005falias.html
@compatibility_alias UICollectionViewController PSTCollectionViewController;
@compatibility_alias UICollectionView PSTCollectionView;
@compatibility_alias UICollectionReusableView PSTCollectionReusableView;
@compatibility_alias UICollectionViewCell PSTCollectionViewCell;
@compatibility_alias UICollectionViewLayout PSTCollectionViewLayout;
@compatibility_alias UICollectionViewFlowLayout PSTCollectionViewFlowLayout;
@compatibility_alias UICollectionViewLayoutAttributes     PSTCollectionViewLayoutAttributes;
@protocol UICollectionViewDataSource <PSTCollectionViewDataSource> @end
@protocol UICollectionViewDelegate <PSTCollectionViewDelegate> @end


Using this clever combination of macros, a developer can develop with a UICollectionView, including a PSTCollectionView, without worrying about the deployment target of the final project. As a drop-in replacement, the same code works more or less the same on iOS 6 as it does on iOS 4.3.

Then typedef is a reserved keyword in C, like a programming language. It is used to create an alias for another data type, not only for a class, but also for an underlying data type like struct, pointers, even a function. Here are some examples:

typedef struct Node Node;
struct Node {
    int data;
    Node *nextptr;

typedef int *intptr;   // type name: intptr
                       // new type: int*

intptr ptr;            // same as: int *ptr




All Articles