[__NSCFSet iName]:发送到实例的无法识别的选择器

时间:2011-06-11 17:24:14

标签: iphone objective-c xcode uialertview

我似乎没有遵循这一点。

这是我的代码

-(void)saveClicked:(id)sender{
    Item *item=[[Item alloc]init];
    item.iName=nameField.text;

    if ([appDelegate.list containsObject:item]) {
    //currentItem and item are object of class Item
    //currentItem was declared in the headerfile and synchronized
        currentItem=item;
        NSString *msg=[NSString stringWithFormat:@"%@ already exists in your Instock list",item.iName];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"edit",nil];
        [alert show];
        [alert release];
    }
}

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex==0) {
    }
    else {
        //getting the error here
        NSLog(@"%@",currentItem.iName);
    }
}

ERROR:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFSet iName]: unrecognized selector sent to instance 0x5e280b0'

我不知道为什么会这样。帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

假设您拥有currentItem retain属性,请尝试使用下面的saveClicked:

    self.currentItem = item;

所以,你的代码应该是......

-(void)saveClicked:(id)sender{
    Item *item=[[Item alloc]init];
    item.iName=nameField.text;

    if ([appDelegate.list containsObject:item]) {
    //currentItem and item are object of class Item
    //currentItem was declared in the headerfile and synchronized

        self.currentItem = item;

        NSString *msg=[NSString stringWithFormat:@"%@ already exists in your Instock list",item.iName];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:msg delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"edit",nil];
        [alert show];
        [alert release];
    }
    [item release];
}

答案 1 :(得分:0)

释放item是没有意义的,因为这意味着你只拥有一次所有权而你放弃它。所以currentItem将指向一个解除分配的对象。您必须通过保留对象来获取所有权,然后在完成对象后释放它。