SoFunction
Updated on 2025-04-13

Restrict native instantiation methods in custom classes in IOS

Restrict native instantiation methods in custom classes in IOS

In custom classes, in addition to the instantiation methods that come with the system, there may also be instantiation methods customized by developers. This allows you to do this when you do not want to use system custom methods and only use custom instantiation methods.

As shown in the following example:

#import <UIKit/> 
 
@interface MYView : UIView 
 
// Restrict the use of system methods for instantiation// Method 1- (instancetype)init UNAVAILABLE_ATTRIBUTE; 
// Method 2- (instancetype)init __attribute__((unavailable("The init method is not available, please use initWithName:"))); 
- (instancetype)initWithFrame:(CGRect)frame __attribute__((unavailable("The initWithFrame method is not available, please use initWithName:view:"))); 
 
// Custom instantiation method- (instancetype)initWithFrame:(CGRect)frame view:(UIView *)view; 
 
@end 

If you have any questions, please leave a message or go to the community of this site to exchange and discuss. Thank you for reading. I hope it can help you. Thank you for your support for this site!