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

标签 ios objective-c iphone ios7

我的方法中有3种行为,我很确定animationOptions是导致错误的行为。 AnimationOptions 仅用于禁止旋转。如果我删除此行为,我的代码可以正常工作。

这是我的错误。由于未捕获的异常而终止应用程序

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

添加异常断点后,断点停留在这一行:

[self addChildBehavior:self.animationOptions];

如果我删除这一行,我的代码可以正常工作。

但是我该如何修复这个错误,我找不到这行错误在哪里

这是我的 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

最佳答案

NSMutable 数组不接受将“nil”添加到自身

“self.animationOptions”将调用

 -(UIDynamicItemBehavior *)animationOptions

方法和方法将始终只返回“nil”。

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

您在这段代码中的逻辑不允许系统构造“_animationOptions”对象。

    if (!_animationOptions)
{
..code..

}

有帮助

关于ios - -[__NSArrayM insertObject :atIndex:]: object cannot be nil when calling addChildBehavior:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28192420/

相关文章:

ios - 带有自定义单元格的 TableViewController swift

ios - 无法通过 Swift 中的计数器循环

ios - 通过代码在sqlite3中启用foreign_keys=ON

html - 视差图像模糊/V。在 iphone 上像素化。需要 CSS 提示

iphone - 在 plist 中处理持久 boolean 值的最佳方法?

ios - 消耗品订阅和不可续订订阅之间有什么区别?

iOS UITextView 无序/有序列表,如 Notes App

ios - 如何在 dote 后显示带有两位数的 Line 标签?

php - 实现聊天系统php、obj-c和mysql

objective-c - 如何读取文本文件?