iOS7 NSKeyedArchiver警告:替换键'UITintColor'的现有值; 类层次结构中可能的重复编码键

Lig*_*Man 5 uitableview ios

我使用xib创建了一个uitableviewcell.我有一个细胞工厂,细胞以这种方式取消存档:

- (instancetype)initWithNib:(NSString *)aNibName
{
    self = [super init];    
    if (self != nil) {

        self.viewTemplateStore = [[NSMutableDictionary alloc] init];
        NSArray * templates = [[NSBundle mainBundle] loadNibNamed:aNibName owner:self options:nil];

        for (id template in templates) {
            if ([template isKindOfClass:[UITableViewCell class]]) {                
                UITableViewCell * cellTemplate = (UITableViewCell *)template;
                NSString * key = cellTemplate.reuseIdentifier;

                if (key) {
                    [self.viewTemplateStore setObject:[NSKeyedArchiver archivedDataWithRootObject:template] forKey:key];
                } else {
                    @throw [NSException exceptionWithName:@"Unknown cell"
                                                   reason:@"Cell has no reuseIdentifier"
                                                 userInfo:nil];
                }
            }
        }
    }
    return self;
}

- (UITableViewCell *)cellOfKind:(NSString *)theCellKind forTable:(UITableView *)aTableView
{
    UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:theCellKind];

    if (!cell) {
        NSData * cellData = [self.viewTemplateStore objectForKey:theCellKind];

        if (cellData) {
            cell = [NSKeyedUnarchiver unarchiveObjectWithData:cellData];
        } else {
            DDLogError(@"Don't know nothing about cell of kind %@", theCellKind);
        }
    }
    return cell;
}
Run Code Online (Sandbox Code Playgroud)

如果将UIActivityIndi​​catorView添加到单元格,则此消息将显示在控制台中:

* NSKeyedArchiver警告:替换键'UITintColor'的现有值; 类层次结构中可能的重复编码键

它只发生在iOS7中.UIActivityIndi​​catorView具有默认值,我只需将其拖放到单元格即可.

有关此消息出现原因的任何线索?

谢谢.

Ale*_*nov 1

这是iOS7中引入的错误。

您正在调用[NSKeyedArchiver archivedDataWithRootObject:template],这会导致整个视图层次结构被存档(使用过程encodeWithCoder:)。

引起警告的问题是 的UIActivityIndicatorView子类UIView,它也符合<NSCoding>协议。在 iOS7 之前,导航栏、活动指示器等视图都有其 Tint 属性,但在 iOS7 中,Tint 属性本身存在UIView。因此,UIViewActivityIndi​​cator 及其祖先 UIView 都对这个 Tint 属性进行编码作为UITintColor键,首先由 进行设置UIView,然后由 进行覆盖UIActivityIndicatorView。这就是出现此警告的原因。无需担心。

顺便说一句,你为什么使用如此奇怪的代码来生成单元格?您可以在viewDidLoad中将所有reuseIdentifiers注册到适当的Nib:

[self.tableView registerNib:[UINib nibWithNibName:@"Cells" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"cell1"];
Run Code Online (Sandbox Code Playgroud)

然后当你调用时 tableView 本身会加载一个单元格

[tableView dequeueReusableCellWithIdentifier:@"cell1" forIndexPath:indexPath];
Run Code Online (Sandbox Code Playgroud)