NSCoding有助于对解除分配的实例进行非常奇怪的错误访问

时间:2013-07-21 04:09:22

标签: iphone ios objective-c nscoding

在我的代码中,我将内存存储在我的数组中......要获取它而不进行HTML请求。 第一次,当我填充我的数组时,一切都很好......当我从内存中加载数组并将它们加载到表中时,问题就出现了。

那是班级

@interface SubscriptionArray : NSObject{
    NSString *title;
    NSString *source;
    NSString *htmlUrl;
    NSInteger count;
}

@property (nonatomic,retain) NSString *title;
@property (nonatomic,retain) NSString *source;
@property (nonatomic,retain) NSString *htmlUrl;
@property (nonatomic) NSInteger count;
@end


#import "SubscriptionArray.h"

@implementation SubscriptionArray
@synthesize title,source,htmlUrl,count;

-(void)dealloc{
    [title release];
    [source release];
    [htmlUrl release];


}

#pragma mark NSCoding

- (id)initWithCoder:(NSCoder *)decoder {
    if (self = [super init]) {
        title = [[decoder decodeObjectForKey:@"title"] retain];
        source = [[decoder decodeObjectForKey:@"source"] retain];
        htmlUrl = [[decoder decodeObjectForKey:@"htmlUrl"] retain];
        count = [decoder decodeIntegerForKey:@"count"];

    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder {
    if (title) [encoder encodeObject:title forKey:@"title"];
    if (source) [encoder encodeObject:source forKey:@"source"];
    if (htmlUrl) [encoder encodeObject:htmlUrl forKey:@"htmlUrl"];
    if (count) [encoder encodeInteger:count forKey:@"count"];

}

数组在委托中声明(与3个不同的类共享)

 NSMutableArray *onlySubsriptions; @property(nonatomic, retain)
 NSMutableArray *onlySubsriptions;

我以这种方式加载

[onlySubsriptions removeAllObjects];
self.onlySubsriptions = [NSKeyedUnarchiver unarchiveObjectWithFile:fullFileName];

我在cellForRowAtIndexPath

中收到错误

例如我确定我有2个元素,当cellForRowAtIndexPath开始加载第一个元素(元素0)时抛出一个异常......它表示解除分配的实例。

我确定问题出现在编码上,因为我第一次获得数组时一切都很好......只有在下次加载我本地存储的数组时才出现问题。

****关于******的更多信息 enter image description here

**** cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";


    TDBadgedCell *cell = [[[TDBadgedCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    SubscriptionArray * element=[[SubscriptionArray alloc]init];
    if (indexPath.section==0) {
        NSLog(@"CASO SPECIALELEMENTS");
        element =[delegate.reader.specialElements objectAtIndex:[indexPath row]];
    }
    if (indexPath.section==1) {
        NSLog(@"CASO CATEGORIA");
        element =[delegate.reader.categories objectAtIndex:[indexPath row]];
    }
    if (indexPath.section==2) {
        NSLog(@"CASO SUBSCRIPTIONS");
        element =[delegate.reader.onlySubsriptions objectAtIndex:[indexPath row]];
    }

1 个答案:

答案 0 :(得分:0)

要处理这种异常,你可以使用malloc工具找出僵尸并修复它。 产品> Profile将启动Instruments,然后你应该有一个名为“Malloc”的“Trace Template”。这个question可能对您有用。