1

以下是我用于取消归档数据的代码:

- (id)initWithCoder:(NSCoder *)decoder
{
    if((self = [super init]))
    {
        NSString *resoureItemClassName = NSStringFromClass([ResourceItem class]);
        self.selectedItemClassName=[decoder decodeObjectForKey:@"selectedItemClassName"];
        Class class = NSClassFromString(selectedItemClassName);
        id temp = nil;

        if(class)
        {      
            temp = [[class alloc] init];
        }

        if(temp)
        {
            if([temp isKindOfClass:[NSString class]])
            {
                self.selectedString = [decoder decodeObjectForKey:@"selectedString"];;
                self.ID = selectedString;
            }
            else if([selectedItemClassName isEqualToString:resoureItemClassName])
            {  
                DLog(@"Resource item")
            }

            [temp release];                         
        }
        else
        {    
            self.ID = nil;
        }
    }

    return self;
}

当我释放 tempObject 时,应用程序崩溃。如果我不这样做,那就是泄漏。我尝试使用自动释放方法。然后还是崩溃了?解决办法是什么?

更新:

selectedItemClassName 中的编码数据是 __NSCFConstantString 或 __NSCFString。崩溃仅发生在 __NSCFString 而不是 __NSCFConstantString 作为类名时,

可能是什么原因?

4

1 回答 1

0

怎么样:

- (id)initWithCoder:(NSCoder *)decoder
{
    if((self = [super init]))
    {
        NSString *resourceItemClassName = NSStringFromClass([ResourceItem class]);
        self.selectedItemClassName=[decoder decodeObjectForKey:@"selectedItemClassName"];
        Class class = NSClassFromString(selectedItemClassName);

        if(class)
        {      
            NSString *stringClassName = NSStringFromClass([NSString class]);
            if([stringClassName isEqualToString:self.selectedItemClassName])
            {
                self.selectedString = [decoder decodeObjectForKey:@"selectedString"];;
                self.ID = selectedString;
            }
            else if([self.selectedItemClassName isEqualToString:resourceItemClassName])
            {  
                DLog(@"Resource item")
            }
        }
        else
        {    
            self.ID = nil;
        }
    }

    return self;
}
于 2012-11-30T00:59:32.377 回答