NSCFString objectAtIndex无法识别的选择器发送到实例0xc16bad0

时间:2014-12-22 05:26:52

标签: ios objective-c

这是我的代码:

NSArray *items = [[NSArray alloc] initWithArray:CatTitle];

    NSLog(@"%@",items);
    menu = [[BlurMenu alloc] initWithItems:items parentView:self.view delegate:self];
    NSLog(@"I'm here !");
    isMenuCat = TRUE;
    [menu show];

NSLog “项目”获得结果正常时:

  

(           “\ U0627 \ U0646 \ U0642 \ U0637 \ U0627 \ U0639 \ U062c \ U0632 \ U0626 \ U064a”,           “\ U0627 \ U0646 \ U0642 \ U0637 \ U0627 \ U0639 \ U0643 \ U0644 \ U064a”,           “\ U062d \ U0631 \ U064a \ U0642”,           “\ U0645 \ U0634 \ U0643 \ U0644 \ U0629 \ U062e \ U0627 \ U0635 \ U0629 \ U0628 \ U0627 \ U0644 \ U062c \ U0647 \ U062f”       )

我在BlurMenu.m文件中使用了创建模糊菜单的库:

- (id)initWithItems:(NSArray*)items parentView:(UIView *)p delegate:(id<BlurMenuDelegate>)d {
    self = [super init];

    if (self) {

        int i = 0;
        NSMutableArray *temp_Arr = [[NSMutableArray alloc]init];
        for (NSArray *xx in items) {
            [temp_Arr addObject:[xx objectAtIndex:i]];
            NSLog(@"%@",[xx objectAtIndex:i]);
            i++;
        }

        userData = [NSUserDefaults standardUserDefaults];
        [userData setBool:NO forKey:@"isTwice"];
        [userData synchronize];

        self.parent = p;
        self.delegate = d;
        self.menuItems = temp_Arr;

        self.alpha = 0.0f;
        self.frame = p.frame;

        UIImage *background = [self blurredSnapshot];
        UIImageView *backgroundView = [[UIImageView alloc] initWithImage:background];
        [self addSubview:backgroundView];

        CGFloat height = [self calculateCollectionViewHeight];

        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setMinimumLineSpacing:ITEM_LINE_SPACING];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, (self.frame.size.height - height) / 2, self.frame.size.width, height) collectionViewLayout:layout];
        [_collectionView setDataSource:self];
        [_collectionView setDelegate:self];
        [_collectionView registerClass:[BlurMenuItemCell class] forCellWithReuseIdentifier:@"cellIdentifier"];
        [_collectionView setBackgroundColor:[UIColor clearColor]];
        [self addSubview:_collectionView];

        UIButton *close = [UIButton buttonWithType:UIButtonTypeCustom];
        close.frame = CGRectMake(0, self.frame.size.height - COLLECTION_VIEW_PADDING, self.frame.size.width, COLLECTION_VIEW_PADDING);
        close.backgroundColor = [UIColor clearColor];
        [close setTitle:@"Close" forState:UIControlStateNormal];
        [close setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        close.titleLabel.font = [UIFont fontWithName:@"GillSans-Light" size:18.0f];
        [close addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchDown];
        [self addSubview:close];

    }
    return self;
}

这是发生错误的行:

menu = [[BlurMenu alloc] initWithItems:items parentView:self.view delegate:self];

这是错误:

  

- [__ NSCFString objectAtIndex:]:无法识别的选择器发送到实例0xc16bad0   ***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [__ NSCFString objectAtIndex:]:   无法识别的选择器发送到实例0xc16bad0'

1 个答案:

答案 0 :(得分:2)

正如NSLog items所示,CatTitle是一个字符串数组;因此,当您将其传递到initWithItems:并使用此循环访问其内容时

int i = 0;
NSMutableArray *temp_Arr = [[NSMutableArray alloc]init];
for (NSArray *xx in items) {
    [temp_Arr addObject:[xx objectAtIndex:i]];
    NSLog(@"%@",[xx objectAtIndex:i]);
    i++;
}

你将它视为一个数组数组,但由于“xx”实际上不是一个数组而是一个字符串,它会产生你收到的NSInvalidArgumentException

因此,您可以尝试使用循环将字符串添加到temp_Arr

NSMutableArray *temp_Arr = [[NSMutableArray alloc]init];
for (NSString *xx in items) {
    [temp_Arr addObject:xx];
    NSLog(@"%@", xx);
}

但实际上,这是不必要的,因为您可以使用一行代码将items数组复制到temp_Arr中:

NSMutableArray *temp_Arr = [[NSMutableArray alloc] initWithArray:items copyItems:YES];