1

I made few classes via Core Data. And I need some additional @propertys for one of that classes in runtime. This @propertys are responsible for download progress and I don't want to store them in Core Data DB. I tried to use a separate extension class:

@interface MyClass ()

    {
        CGFloat _downloadProgress;
        NSInteger _downloadErrorCounter;
        BOOL _downloadAllStuff;
        BOOL _downloadUserCanceled;
    }

    @property (nonatomic, assign) CGFloat downloadProgress;
    @property (nonatomic, assign) NSInteger downloadErrorCounter;
    @property (nonatomic, assign) BOOL downloadAllStuff;
    @property (nonatomic, assign) BOOL downloadUserCanceled;

@end

But private variables are not visible out of MyClass, and @propertys compile all right, but in runtime i get -[MyClass setDownloadErrorCounter:]: unrecognized selector sent to instance. Can anyone suggest me some solution?

4

3 回答 3

6

最简单的解决方案(如果您不想修改 Xcode 生成的类文件)是将属性添加到 Core Data 模型并将属性定义为transient。瞬态属性不会保存到存储文件中。

另一种选择是使用“mogenerator”之类的工具,它为每个实体生成两个类文件,一个用于 Core Data 属性(如果模型更改,则会被覆盖),一个用于自定义属性(不会被覆盖) .

更新:Xcode 7 开始, Xcode 为每个托管对象子类创建一个类和一个类别,比较category 中的 NSManagedObject 子类属性。自定义属性可以添加到模型更改时不会被覆盖的类定义。

于 2013-07-01T08:08:10.040 回答
1

只需添加

@synthesize downloadErrorCounter = _downloadErrorCounter;
...

在@实现中。注意,不是@dynamic。

于 2013-07-01T08:04:12.290 回答
0

尝试使用该@synthesize解决方案时出现错误:

@synthesize not allowed in a category's implementation.

解决方案是使用本博客中描述的关联对象:http: //kaspermunck.github.io/2012/11/adding-properties-to-objective-c-categories/

MyManagedObject+Additions.h

@property (strong, nonatomic) NSString *test;

MyManagedObject+Additions.m

NSString const *key = @"my.very.unique.key";

- (void)setTest:(NSString *)test
{
    objc_setAssociatedObject(self, &key, test, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (NSString *)test
{
    return objc_getAssociatedObject(self, &key);
}
于 2017-01-18T15:02:40.367 回答