开发者

[__NSCFSet iName]: unrecognized selector sent to instance

I just do not seem to follow this.

This is my code

-(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:

***开发者_如何学Python Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFSet iName]: unrecognized selector sent to instance 0x5e280b0'

I have no clue why this is happening. Help would be appreciated.


Assume you have currentItem as retain property , Try with using below with your saveClicked:

    self.currentItem = item;

So, you code should be ...

-(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];
}


It doesn't make sense to release item as that would mean you've taken ownership only once and you're relinquishing it. So the currentItem will be pointing to a deallocated object. You will have to take ownership by retaining the object and later releasing it once you're done with the object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜