简体   繁体   中英

-[__NSArrayM insertObject:atIndex:]: object cannot be nil when calling addChildBehavior:

There are 3 behaviors in my method, I'm pretty sure animationOptions is the one causing bug. AnimationOptions is only used to ban rotation.If i delete this behavior,my code work fine.

Here is my bug.Terminating app due to uncaught exception

'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil’

After I add a exception breakpoint, the breakpoint stay at this line:

[self addChildBehavior:self.animationOptions];

If i delete this line, my code work fine.

But how can i fix this bug, I can not find where is this line mistake

Here is my DropitBehavior.m

#import "DropitBehavior.h"
@interface DropitBehavior()
@property(strong,nonatomic) UIGravityBehavior *gravity;
@property(strong,nonatomic) UICollisionBehavior *collider;
@property(strong,nonatomic) UIDynamicItemBehavior *animationOptions;
@end

@implementation DropitBehavior
-(UIGravityBehavior *)gravity
{
    if (!_gravity) {
        _gravity=[[UIGravityBehavior alloc]init];
        _gravity.magnitude=0.90;
    }
    return _gravity;
}

-(UICollisionBehavior *)collider
{
    if (!_collider) {
        _collider=[[UICollisionBehavior alloc]init];
        _collider.translatesReferenceBoundsIntoBoundary=YES;
    }
    return _collider;
}

-(UIDynamicItemBehavior *)animationOptions
{
    if (_animationOptions) {
        _animationOptions=[[UIDynamicItemBehavior alloc]init];
        _animationOptions.allowsRotation=NO;
    }
    return _animationOptions;
}

-(void)additem:(id <UIDynamicItem>)item
{
    [self.gravity addItem:item];
    [self.collider addItem:item];
    [self.animationOptions addItem:item];
}

-(void)removeitem:(id <UIDynamicItem>)item
{
    [self.gravity removeItem:item];
    [self.collider removeItem:item];
    [self.animationOptions removeItem:item];
}

-(instancetype)init
{
    self=[super init];
    [self addChildBehavior:self.gravity];
    [self addChildBehavior:self.collider];
    [self addChildBehavior:self.animationOptions];
    return self;
}
@end

a NSMutable array doesn't accept a "nil" to be added to itself

"self.animationOptions" will call the

 -(UIDynamicItemBehavior *)animationOptions

method and the method will return only "nil" all the time.

 -(UIDynamicItemBehavior *)animationOptions
 {
    if (_animationOptions) {
    _animationOptions=[[UIDynamicItemBehavior alloc]init];    

your logic in this piece of code doesn't allow the system to construct "_animationOptions" object.

    if (!_animationOptions)
{
..code..

}

would help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM