Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Nullability #399

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Masonry/MASCompositeConstraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@
*/
@interface MASCompositeConstraint : MASConstraint

NS_ASSUME_NONNULL_BEGIN

/**
* Creates a composite with a predefined array of children
*
* @param children child MASConstraints
*
* @return a composite constraint
*/
- (id)initWithChildren:(NSArray *)children;
- (instancetype)initWithChildren:(NSArray *)children;

NS_ASSUME_NONNULL_END

@end
4 changes: 2 additions & 2 deletions Masonry/MASCompositeConstraint.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ @interface MASCompositeConstraint () <MASConstraintDelegate>

@implementation MASCompositeConstraint

- (id)initWithChildren:(NSArray *)children {
- (instancetype)initWithChildren:(NSArray *)children {
self = [super init];
if (!self) return nil;
if (!self) return self;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bit awkward, i know, but otherwise the compiler will complain that the method should not return nil due to nullability.


_childConstraints = [children mutableCopy];
for (MASConstraint *constraint in _childConstraints) {
Expand Down
16 changes: 14 additions & 2 deletions Masonry/MASConstraint+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

@interface MASConstraint ()

NS_ASSUME_NONNULL_BEGIN

/**
* Whether or not to check for an existing constraint instead of adding constraint
*/
Expand All @@ -21,7 +23,7 @@
/**
* Usually MASConstraintMaker but could be a parent MASConstraint
*/
@property (nonatomic, weak) id<MASConstraintDelegate> delegate;
@property (nullable, nonatomic, weak) id<MASConstraintDelegate> delegate;

/**
* Based on a provided value type, is equal to calling:
Expand All @@ -32,11 +34,15 @@
*/
- (void)setLayoutConstantWithValue:(NSValue *)value;

NS_ASSUME_NONNULL_END

@end


@interface MASConstraint (Abstract)

NS_ASSUME_NONNULL_BEGIN

/**
* Sets the constraint relation to given NSLayoutRelation
* returns a block which accepts one of the following:
Expand All @@ -50,17 +56,23 @@
*/
- (MASConstraint *)addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute;

NS_ASSUME_NONNULL_END

@end


@protocol MASConstraintDelegate <NSObject>

NS_ASSUME_NONNULL_BEGIN

/**
* Notifies the delegate when the constraint needs to be replaced with another constraint. For example
* A MASViewConstraint may turn into a MASCompositeConstraint when an array is passed to one of the equality blocks
*/
- (void)constraint:(MASConstraint *)constraint shouldBeReplacedWithConstraint:(MASConstraint *)replacementConstraint;

- (MASConstraint *)constraint:(MASConstraint *)constraint addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute;
- (MASConstraint *)constraint:(nullable MASConstraint *)constraint addConstraintWithLayoutAttribute:(NSLayoutAttribute)layoutAttribute;

NS_ASSUME_NONNULL_END

@end
8 changes: 8 additions & 0 deletions Masonry/MASConstraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
@interface MASConstraint : NSObject

NS_ASSUME_NONNULL_BEGIN

// Chaining Support

/**
Expand Down Expand Up @@ -223,6 +225,8 @@
*/
- (void)uninstall;

NS_ASSUME_NONNULL_END

@end


Expand Down Expand Up @@ -252,6 +256,8 @@

@interface MASConstraint (AutoboxingSupport)

NS_ASSUME_NONNULL_BEGIN

/**
* Aliases to corresponding relation methods (for shorthand macros)
* Also needed to aid autocompletion
Expand All @@ -265,4 +271,6 @@
*/
- (MASConstraint * (^)(id offset))mas_offset;

NS_ASSUME_NONNULL_END

@end
2 changes: 1 addition & 1 deletion Masonry/MASConstraint.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ @implementation MASConstraint

#pragma mark - Init

- (id)init {
- (instancetype)init {
NSAssert(![self isMemberOfClass:[MASConstraint class]], @"MASConstraint is an abstract class, you should not instantiate it directly.");
return [super init];
}
Expand Down
6 changes: 5 additions & 1 deletion Masonry/MASConstraintMaker.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ typedef NS_OPTIONS(NSInteger, MASAttribute) {
*/
@interface MASConstraintMaker : NSObject

NS_ASSUME_NONNULL_BEGIN

/**
* The following properties return a new MASViewConstraint
* with the first item set to the makers associated view and the appropriate MASViewAttribute
Expand Down Expand Up @@ -124,7 +126,7 @@ typedef NS_OPTIONS(NSInteger, MASAttribute) {
*
* @return a new MASConstraintMaker
*/
- (id)initWithView:(MAS_VIEW *)view;
- (instancetype)initWithView:(MAS_VIEW *)view;

/**
* Calls install method on any MASConstraints which have been created by this maker
Expand All @@ -135,4 +137,6 @@ typedef NS_OPTIONS(NSInteger, MASAttribute) {

- (MASConstraint * (^)(dispatch_block_t))group;

NS_ASSUME_NONNULL_END

@end
4 changes: 2 additions & 2 deletions Masonry/MASConstraintMaker.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ @interface MASConstraintMaker () <MASConstraintDelegate>

@implementation MASConstraintMaker

- (id)initWithView:(MAS_VIEW *)view {
- (instancetype)initWithView:(MAS_VIEW *)view {
self = [super init];
if (!self) return nil;
if (!self) return self;

self.view = view;
self.constraints = NSMutableArray.new;
Expand Down
4 changes: 4 additions & 0 deletions Masonry/MASLayoutConstraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
*/
@interface MASLayoutConstraint : NSLayoutConstraint

NS_ASSUME_NONNULL_BEGIN

/**
* a key to associate with this constraint
*/
@property (nonatomic, strong) id mas_key;

NS_ASSUME_NONNULL_END

@end
12 changes: 8 additions & 4 deletions Masonry/MASViewAttribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@
*/
@interface MASViewAttribute : NSObject

NS_ASSUME_NONNULL_BEGIN

/**
* The view which the reciever relates to. Can be nil if item is not a view.
*/
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, actually the view can be nil according to the comment, need to fix this in a new commit

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, fixed it. fixed also the designated initializer which allows view to be nil too. did not fix the convenience initializer, as view ist used for both view and item there, and item may not be nil.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually now did fix the convenience initializer, as the item property is weak and thus needs to be nullable

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i mean, it could be forced that the parameter is non-nil in the initializer, and the property still can become nullable - but as the MASConstraintDelegateMock explicitly passes nil, I went with making it nullable.

@property (nonatomic, weak, readonly) MAS_VIEW *view;
@property (nullable, nonatomic, weak, readonly) MAS_VIEW *view;

/**
* The item which the reciever relates to.
*/
@property (nonatomic, weak, readonly) id item;
@property (nullable, nonatomic, weak, readonly) id item;

/**
* The attribute which the reciever relates to
Expand All @@ -32,12 +34,12 @@
/**
* Convenience initializer.
*/
- (id)initWithView:(MAS_VIEW *)view layoutAttribute:(NSLayoutAttribute)layoutAttribute;
- (instancetype)initWithView:(nullable MAS_VIEW *)view layoutAttribute:(NSLayoutAttribute)layoutAttribute;

/**
* The designated initializer.
*/
- (id)initWithView:(MAS_VIEW *)view item:(id)item layoutAttribute:(NSLayoutAttribute)layoutAttribute;
- (instancetype)initWithView:(nullable MAS_VIEW *)view item:(id)item layoutAttribute:(NSLayoutAttribute)layoutAttribute;

/**
* Determine whether the layoutAttribute is a size attribute
Expand All @@ -46,4 +48,6 @@
*/
- (BOOL)isSizeAttribute;

NS_ASSUME_NONNULL_END

@end
6 changes: 3 additions & 3 deletions Masonry/MASViewAttribute.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

@implementation MASViewAttribute

- (id)initWithView:(MAS_VIEW *)view layoutAttribute:(NSLayoutAttribute)layoutAttribute {
- (instancetype)initWithView:(MAS_VIEW *)view layoutAttribute:(NSLayoutAttribute)layoutAttribute {
self = [self initWithView:view item:view layoutAttribute:layoutAttribute];
return self;
}

- (id)initWithView:(MAS_VIEW *)view item:(id)item layoutAttribute:(NSLayoutAttribute)layoutAttribute {
- (instancetype)initWithView:(MAS_VIEW *)view item:(id)item layoutAttribute:(NSLayoutAttribute)layoutAttribute {
self = [super init];
if (!self) return nil;
if (!self) return self;

_view = view;
_item = item;
Expand Down
6 changes: 5 additions & 1 deletion Masonry/MASViewConstraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
@interface MASViewConstraint : MASConstraint <NSCopying>

NS_ASSUME_NONNULL_BEGIN

/**
* First item/view and first attribute of the NSLayoutConstraint
*/
Expand All @@ -34,7 +36,7 @@
*
* @return a new view constraint
*/
- (id)initWithFirstViewAttribute:(MASViewAttribute *)firstViewAttribute;
- (instancetype)initWithFirstViewAttribute:(MASViewAttribute *)firstViewAttribute;

/**
* Returns all MASViewConstraints installed with this view as a first item.
Expand All @@ -45,4 +47,6 @@
*/
+ (NSArray *)installedConstraintsForView:(MAS_VIEW *)view;

NS_ASSUME_NONNULL_END

@end
6 changes: 3 additions & 3 deletions Masonry/MASViewConstraint.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ @interface MASViewConstraint ()

@implementation MASViewConstraint

- (id)initWithFirstViewAttribute:(MASViewAttribute *)firstViewAttribute {
- (instancetype)initWithFirstViewAttribute:(MASViewAttribute *)firstViewAttribute {
self = [super init];
if (!self) return nil;
if (!self) return self;

_firstViewAttribute = firstViewAttribute;
self.layoutPriority = MASLayoutPriorityRequired;
Expand All @@ -65,7 +65,7 @@ - (id)initWithFirstViewAttribute:(MASViewAttribute *)firstViewAttribute {

#pragma mark - NSCoping

- (id)copyWithZone:(NSZone __unused *)zone {
- (instancetype)copyWithZone:(NSZone __unused *)zone {
MASViewConstraint *constraint = [[MASViewConstraint alloc] initWithFirstViewAttribute:self.firstViewAttribute];
constraint.layoutConstant = self.layoutConstant;
constraint.layoutRelation = self.layoutRelation;
Expand Down
4 changes: 4 additions & 0 deletions Masonry/NSArray+MASAdditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ typedef NS_ENUM(NSUInteger, MASAxisType) {

@interface NSArray (MASAdditions)

NS_ASSUME_NONNULL_BEGIN

/**
* Creates a MASConstraintMaker with each view in the callee.
* Any constraints defined are added to the view or the appropriate superview once the block has finished executing on each view
Expand Down Expand Up @@ -69,4 +71,6 @@ typedef NS_ENUM(NSUInteger, MASAxisType) {
*/
- (void)mas_distributeViewsAlongAxis:(MASAxisType)axisType withFixedItemLength:(CGFloat)fixedItemLength leadSpacing:(CGFloat)leadSpacing tailSpacing:(CGFloat)tailSpacing;

NS_ASSUME_NONNULL_END

@end
4 changes: 4 additions & 0 deletions Masonry/NSArray+MASShorthandAdditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@
*/
@interface NSArray (MASShorthandAdditions)

NS_ASSUME_NONNULL_BEGIN

- (NSArray *)makeConstraints:(void(^)(MASConstraintMaker *make))block;
- (NSArray *)updateConstraints:(void(^)(MASConstraintMaker *make))block;
- (NSArray *)remakeConstraints:(void(^)(MASConstraintMaker *make))block;

NS_ASSUME_NONNULL_END

@end

@implementation NSArray (MASShorthandAdditions)
Expand Down
6 changes: 5 additions & 1 deletion Masonry/View+MASAdditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
@interface MAS_VIEW (MASAdditions)

NS_ASSUME_NONNULL_BEGIN

/**
* following properties return a new MASViewAttribute with current view and appropriate NSLayoutAttribute
*/
Expand Down Expand Up @@ -72,7 +74,7 @@
*
* @return returns nil if common superview could not be found
*/
- (instancetype)mas_closestCommonSuperview:(MAS_VIEW *)view;
- (nullable instancetype)mas_closestCommonSuperview:(MAS_VIEW *)view;

/**
* Creates a MASConstraintMaker with the callee view.
Expand Down Expand Up @@ -106,4 +108,6 @@
*/
- (NSArray *)mas_remakeConstraints:(void(NS_NOESCAPE ^)(MASConstraintMaker *make))block;

NS_ASSUME_NONNULL_END

@end
4 changes: 4 additions & 0 deletions Masonry/View+MASShorthandAdditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
@interface MAS_VIEW (MASShorthandAdditions)

NS_ASSUME_NONNULL_BEGIN

@property (nonatomic, strong, readonly) MASViewAttribute *left;
@property (nonatomic, strong, readonly) MASViewAttribute *top;
@property (nonatomic, strong, readonly) MASViewAttribute *right;
Expand Down Expand Up @@ -64,6 +66,8 @@
- (NSArray *)updateConstraints:(void(^)(MASConstraintMaker *make))block;
- (NSArray *)remakeConstraints:(void(^)(MASConstraintMaker *make))block;

NS_ASSUME_NONNULL_END

@end

#define MAS_ATTR_FORWARD(attr) \
Expand Down
4 changes: 4 additions & 0 deletions Masonry/ViewController+MASAdditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

@interface MAS_VIEW_CONTROLLER (MASAdditions)

NS_ASSUME_NONNULL_BEGIN

/**
* following properties return a new MASViewAttribute with appropriate UILayoutGuide and NSLayoutAttribute
*/
Expand All @@ -24,6 +26,8 @@
@property (nonatomic, strong, readonly) MASViewAttribute *mas_bottomLayoutGuideTop NS_DEPRECATED_IOS(8.0, 11.0);
@property (nonatomic, strong, readonly) MASViewAttribute *mas_bottomLayoutGuideBottom NS_DEPRECATED_IOS(8.0, 11.0);

NS_ASSUME_NONNULL_END

@end

#endif