From eaa875c7f2e08afa8b597c653be6b7c45cfff66d Mon Sep 17 00:00:00 2001 From: Michael Schneider Date: Fri, 3 Mar 2017 14:49:34 -0800 Subject: [PATCH] Improve designated initializer usage (#3132) * Improve designated initializer usage * Some more changes * Add some whitespace * Fix some warning --- Source/ASCellNode.h | 6 +++--- Source/ASDisplayNode.h | 6 +++--- Source/ASDisplayNode.mm | 6 ++---- Source/ASEditableTextNode.h | 4 ++-- Source/ASMultiplexImageNode.h | 10 ++++++++++ Source/ASMultiplexImageNode.mm | 15 ++++++++++++++- Source/ASNetworkImageNode.h | 9 +++++++++ Source/ASNetworkImageNode.mm | 12 ++++++++++++ Source/ASScrollNode.mm | 2 +- Source/ASTextNode.h | 4 ++-- Source/ASVideoNode.h | 5 ++++- Source/Details/ASAbstractLayoutController.h | 4 ++-- Source/Layout/ASLayout.h | 2 +- Source/Layout/ASLayoutSpec+Subclasses.mm | 2 +- Source/Layout/ASLayoutSpec.h | 2 +- .../Private/ASCollectionViewFlowLayoutInspector.h | 1 + Source/Private/ASLayoutTransition.h | 2 +- Source/Private/ASMutableElementMap.h | 8 ++++++-- Source/Private/ASSection.h | 2 +- 19 files changed, 76 insertions(+), 26 deletions(-) diff --git a/Source/ASCellNode.h b/Source/ASCellNode.h index 4957afdff..36e5c4609 100644 --- a/Source/ASCellNode.h +++ b/Source/ASCellNode.h @@ -191,9 +191,9 @@ typedef NS_ENUM(NSUInteger, ASCellNodeVisibilityEvent) { @interface ASCellNode (Unavailable) -- (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock __unavailable; +- (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock NS_UNAVAILABLE; -- (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock __unavailable; +- (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock NS_UNAVAILABLE; - (void)setLayerBacked:(BOOL)layerBacked AS_UNAVAILABLE("ASCellNode does not support layer-backing"); @@ -208,7 +208,7 @@ typedef NS_ENUM(NSUInteger, ASCellNodeVisibilityEvent) { /** * Initializes a text cell with given text attributes and text insets */ -- (instancetype)initWithAttributes:(NSDictionary *)textAttributes insets:(UIEdgeInsets)textInsets; +- (instancetype)initWithAttributes:(NSDictionary *)textAttributes insets:(UIEdgeInsets)textInsets NS_DESIGNATED_INITIALIZER; /** * Text to display. diff --git a/Source/ASDisplayNode.h b/Source/ASDisplayNode.h index deeb1e1ca..3b9a38a95 100644 --- a/Source/ASDisplayNode.h +++ b/Source/ASDisplayNode.h @@ -125,7 +125,7 @@ extern NSInteger const ASDefaultDrawingPriority; * @return An ASDisplayNode instance whose view will be a subclass that enables asynchronous rendering, and passes * through -layout and touch handling methods. */ -- (instancetype)init; +- (instancetype)init NS_DESIGNATED_INITIALIZER; /** @@ -147,7 +147,7 @@ extern NSInteger const ASDefaultDrawingPriority; * @return An ASDisplayNode instance that loads its view with the given block that is guaranteed to run on the main * queue. The view will render synchronously and -layout and touch handling methods on the node will not be called. */ -- (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock; +- (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock NS_DESIGNATED_INITIALIZER; /** * @abstract Alternative initializer with a block to create the backing layer. @@ -168,7 +168,7 @@ extern NSInteger const ASDefaultDrawingPriority; * @return An ASDisplayNode instance that loads its layer with the given block that is guaranteed to run on the main * queue. The layer will render synchronously and -layout and touch handling methods on the node will not be called. */ -- (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)layerBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock; +- (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)layerBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock NS_DESIGNATED_INITIALIZER; /** * @abstract Add a block of work to be performed on the main thread when the node's view or layer is loaded. Thread safe. diff --git a/Source/ASDisplayNode.mm b/Source/ASDisplayNode.mm index f1e134c80..fd7123b88 100644 --- a/Source/ASDisplayNode.mm +++ b/Source/ASDisplayNode.mm @@ -316,12 +316,11 @@ - (instancetype)init - (instancetype)initWithViewClass:(Class)viewClass { - if (!(self = [super init])) + if (!(self = [self init])) return nil; ASDisplayNodeAssert([viewClass isSubclassOfClass:[UIView class]], @"should initialize with a subclass of UIView"); - [self _initializeInstance]; _viewClass = viewClass; _flags.synchronous = ![viewClass isSubclassOfClass:[_ASDisplayView class]]; @@ -330,12 +329,11 @@ - (instancetype)initWithViewClass:(Class)viewClass - (instancetype)initWithLayerClass:(Class)layerClass { - if (!(self = [super init])) + if (!(self = [self init])) return nil; ASDisplayNodeAssert([layerClass isSubclassOfClass:[CALayer class]], @"should initialize with a subclass of CALayer"); - [self _initializeInstance]; _layerClass = layerClass; _flags.synchronous = ![layerClass isSubclassOfClass:[_ASDisplayLayer class]]; _flags.layerBacked = YES; diff --git a/Source/ASEditableTextNode.h b/Source/ASEditableTextNode.h index 71d4a89da..d395876ac 100644 --- a/Source/ASEditableTextNode.h +++ b/Source/ASEditableTextNode.h @@ -136,9 +136,9 @@ NS_ASSUME_NONNULL_BEGIN @interface ASEditableTextNode (Unavailable) -- (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock __unavailable; +- (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock NS_UNAVAILABLE; -- (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock __unavailable; +- (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock NS_UNAVAILABLE; @end diff --git a/Source/ASMultiplexImageNode.h b/Source/ASMultiplexImageNode.h index 5320a461f..6ea08c2c3 100644 --- a/Source/ASMultiplexImageNode.h +++ b/Source/ASMultiplexImageNode.h @@ -134,6 +134,16 @@ typedef NS_ENUM(NSUInteger, ASMultiplexImageNodeErrorCode) { #endif @end +#pragma mark - + +@interface ASMultiplexImageNode (Unavailable) + +- (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock NS_UNAVAILABLE; + +- (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock NS_UNAVAILABLE; + +@end + #pragma mark - /** diff --git a/Source/ASMultiplexImageNode.mm b/Source/ASMultiplexImageNode.mm index 0b3523c70..c26354f43 100644 --- a/Source/ASMultiplexImageNode.mm +++ b/Source/ASMultiplexImageNode.mm @@ -149,7 +149,8 @@ - (void)_downloadImageWithIdentifier:(id)imageIdentifier URL:(NSURL *)imageURL c @implementation ASMultiplexImageNode -#pragma mark - Getting Started / Tearing Down +#pragma mark - Lifecycle + - (instancetype)initWithCache:(id)cache downloader:(id)downloader { if (!(self = [super init])) @@ -179,6 +180,18 @@ - (instancetype)init #endif } +- (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock +{ + ASDISPLAYNODE_NOT_DESIGNATED_INITIALIZER(); + return [self init]; +} + +- (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock +{ + ASDISPLAYNODE_NOT_DESIGNATED_INITIALIZER(); + return [self init]; +} + - (void)dealloc { [_phImageRequestOperation cancel]; diff --git a/Source/ASNetworkImageNode.h b/Source/ASNetworkImageNode.h index 0f984eea3..4f3803aa7 100644 --- a/Source/ASNetworkImageNode.h +++ b/Source/ASNetworkImageNode.h @@ -112,6 +112,15 @@ NS_ASSUME_NONNULL_BEGIN @end +#pragma mark - + +@interface ASNetworkImageNode (Unavailable) + +- (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock NS_UNAVAILABLE; + +- (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock NS_UNAVAILABLE; + +@end #pragma mark - /** diff --git a/Source/ASNetworkImageNode.mm b/Source/ASNetworkImageNode.mm index 40e4bbf5b..85b88bca7 100755 --- a/Source/ASNetworkImageNode.mm +++ b/Source/ASNetworkImageNode.mm @@ -102,6 +102,18 @@ - (instancetype)init #endif } +- (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock +{ + ASDISPLAYNODE_NOT_DESIGNATED_INITIALIZER(); + return [self init]; +} + +- (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock +{ + ASDISPLAYNODE_NOT_DESIGNATED_INITIALIZER(); + return [self init]; +} + - (void)dealloc { [self _cancelImageDownload]; diff --git a/Source/ASScrollNode.mm b/Source/ASScrollNode.mm index 6e9afc69c..9e110d015 100644 --- a/Source/ASScrollNode.mm +++ b/Source/ASScrollNode.mm @@ -64,7 +64,7 @@ @implementation ASScrollNode - (instancetype)init { - return [super initWithViewBlock:^UIView *{ return [[ASScrollView alloc] init]; }]; + return [super initWithViewBlock:^UIView *{ return [[ASScrollView alloc] init]; } didLoadBlock:nil]; } - (ASLayout *)calculateLayoutThatFits:(ASSizeRange)constrainedSize diff --git a/Source/ASTextNode.h b/Source/ASTextNode.h index 78bdd1200..a5544d5a2 100644 --- a/Source/ASTextNode.h +++ b/Source/ASTextNode.h @@ -282,9 +282,9 @@ typedef NS_ENUM(NSUInteger, ASTextNodeHighlightStyle) { @interface ASTextNode (Unavailable) -- (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock __unavailable; +- (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock NS_UNAVAILABLE; -- (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock __unavailable; +- (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock NS_UNAVAILABLE; @end diff --git a/Source/ASVideoNode.h b/Source/ASVideoNode.h index 63b833433..d4f6bb7a3 100644 --- a/Source/ASVideoNode.h +++ b/Source/ASVideoNode.h @@ -145,8 +145,11 @@ NS_ASSUME_NONNULL_BEGIN @interface ASVideoNode (Unavailable) -- (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock __unavailable; +- (instancetype)initWithLayerBlock:(ASDisplayNodeLayerBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock NS_UNAVAILABLE; + +- (instancetype)initWithViewBlock:(ASDisplayNodeViewBlock)viewBlock didLoadBlock:(nullable ASDisplayNodeDidLoadBlock)didLoadBlock NS_UNAVAILABLE; @end + NS_ASSUME_NONNULL_END diff --git a/Source/Details/ASAbstractLayoutController.h b/Source/Details/ASAbstractLayoutController.h index c79f13e8b..0394fa70e 100644 --- a/Source/Details/ASAbstractLayoutController.h +++ b/Source/Details/ASAbstractLayoutController.h @@ -29,9 +29,9 @@ ASDISPLAYNODE_EXTERN_C_END @interface ASAbstractLayoutController (Unavailable) -- (NSSet *)indexPathsForScrolling:(ASScrollDirection)scrollDirection rangeMode:(ASLayoutRangeMode)rangeMode rangeType:(ASLayoutRangeType)rangeType __unavailable; +- (NSSet *)indexPathsForScrolling:(ASScrollDirection)scrollDirection rangeMode:(ASLayoutRangeMode)rangeMode rangeType:(ASLayoutRangeType)rangeType NS_UNAVAILABLE; -- (void)allIndexPathsForScrolling:(ASScrollDirection)scrollDirection rangeMode:(ASLayoutRangeMode)rangeMode displaySet:(NSSet * _Nullable * _Nullable)displaySet preloadSet:(NSSet * _Nullable * _Nullable)preloadSet __unavailable; +- (void)allIndexPathsForScrolling:(ASScrollDirection)scrollDirection rangeMode:(ASLayoutRangeMode)rangeMode displaySet:(NSSet * _Nullable * _Nullable)displaySet preloadSet:(NSSet * _Nullable * _Nullable)preloadSet NS_UNAVAILABLE; @end diff --git a/Source/Layout/ASLayout.h b/Source/Layout/ASLayout.h index b592ddb13..ca01744d2 100644 --- a/Source/Layout/ASLayout.h +++ b/Source/Layout/ASLayout.h @@ -144,7 +144,7 @@ ASDISPLAYNODE_EXTERN_C_END @interface ASLayout (Unavailable) -- (instancetype)init __unavailable; +- (instancetype)init NS_UNAVAILABLE; @end diff --git a/Source/Layout/ASLayoutSpec+Subclasses.mm b/Source/Layout/ASLayoutSpec+Subclasses.mm index a743364ee..083d1e14c 100644 --- a/Source/Layout/ASLayoutSpec+Subclasses.mm +++ b/Source/Layout/ASLayoutSpec+Subclasses.mm @@ -14,7 +14,7 @@ #pragma mark - ASNullLayoutSpec @interface ASNullLayoutSpec : ASLayoutSpec -- (instancetype)init __unavailable; +- (instancetype)init NS_UNAVAILABLE; + (ASNullLayoutSpec *)null; @end diff --git a/Source/Layout/ASLayoutSpec.h b/Source/Layout/ASLayoutSpec.h index 4a28709d5..fe8bb67a9 100644 --- a/Source/Layout/ASLayoutSpec.h +++ b/Source/Layout/ASLayoutSpec.h @@ -84,7 +84,7 @@ NS_ASSUME_NONNULL_BEGIN /* * Init not available for ASWrapperLayoutSpec */ -- (instancetype)init __unavailable; +- (instancetype)init NS_UNAVAILABLE; @end diff --git a/Source/Private/ASCollectionViewFlowLayoutInspector.h b/Source/Private/ASCollectionViewFlowLayoutInspector.h index 8505d3d7b..edbdedcb6 100644 --- a/Source/Private/ASCollectionViewFlowLayoutInspector.h +++ b/Source/Private/ASCollectionViewFlowLayoutInspector.h @@ -24,6 +24,7 @@ AS_SUBCLASSING_RESTRICTED @property (nonatomic, weak, readonly) UICollectionViewFlowLayout *layout; - (instancetype)init NS_UNAVAILABLE; + - (instancetype)initWithFlowLayout:(UICollectionViewFlowLayout *)flowLayout NS_DESIGNATED_INITIALIZER; @end diff --git a/Source/Private/ASLayoutTransition.h b/Source/Private/ASLayoutTransition.h index f3e384e90..52a05834d 100644 --- a/Source/Private/ASLayoutTransition.h +++ b/Source/Private/ASLayoutTransition.h @@ -93,7 +93,7 @@ AS_SUBCLASSING_RESTRICTED @interface ASLayoutTransition (Unavailable) -- (instancetype)init __unavailable; +- (instancetype)init NS_UNAVAILABLE; @end diff --git a/Source/Private/ASMutableElementMap.h b/Source/Private/ASMutableElementMap.h index 221353e57..b6f9f22ea 100644 --- a/Source/Private/ASMutableElementMap.h +++ b/Source/Private/ASMutableElementMap.h @@ -21,8 +21,6 @@ NS_ASSUME_NONNULL_BEGIN AS_SUBCLASSING_RESTRICTED @interface ASMutableElementMap : NSObject -- (instancetype)init __unavailable; - - (instancetype)initWithSections:(NSArray *)sections items:(ASCollectionElementTwoDimensionalArray *)items supplementaryElements:(ASSupplementaryElementDictionary *)supplementaryElements; - (void)insertSection:(ASSection *)section atIndex:(NSInteger)index; @@ -49,4 +47,10 @@ AS_SUBCLASSING_RESTRICTED @interface ASElementMap (MutableCopying) @end +@interface ASMutableElementMap (Unavailable) + +- (instancetype)init NS_UNAVAILABLE; + +@end + NS_ASSUME_NONNULL_END diff --git a/Source/Private/ASSection.h b/Source/Private/ASSection.h index 4944415d5..42f71e2ce 100644 --- a/Source/Private/ASSection.h +++ b/Source/Private/ASSection.h @@ -19,7 +19,7 @@ @property (nonatomic, assign, readonly) NSInteger sectionID; @property (nonatomic, strong, nullable, readonly) id context; -- (nullable instancetype)init __unavailable; +- (nullable instancetype)init NS_UNAVAILABLE; - (nullable instancetype)initWithSectionID:(NSInteger)sectionID context:(nullable id)context NS_DESIGNATED_INITIALIZER; @end