使用辅助 .h .m 文件向 NSManagedObject 添加其他属性 [英] add additional properties to NSManagedObject with secondary .h .m files

查看:58
本文介绍了使用辅助 .h .m 文件向 NSManagedObject 添加其他属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一些 NSManagedObject 类用于 CoreData 我需要添加一些额外的属性来格式化我正在使用 GRMustache 模板.

这是一个示例属性:

-(NSString *) PriceFormatted {NSNumberFormatter *nfm = [[[NSNumberFormatter alloc] init] autorelease];[nfm setNumberStyle:NSNumberFormatterCurrencyStyle];[nfm setCurrencyCode:[Helpers GetCurrencyCode]];[nfm setNegativeFormat:@"-¤#,##0.00"];[nfm setMaximumFractionDigits:2];返回 [nfm stringFromNumber:self.Price];}

我目前在生成的 NSManagedObject 类中有这个,但是如果我需要重新生成一个新的 NSManagedObject 类,这会导致问题.

我可以在第二组类中定义这些属性吗——类似于 C# 中的部分?

解决方案

可能最简单的方法是向生成的托管对象添加类别.

这里是苹果的文档,很简单.

引用:

<块引用>

您可以通过在接口文件中声明方法来向类添加方法在类别名称下并在实现文件中定义它们同名下.类别名称表示方法是添加到在别处声明的类,而不是新类.你不能,但是,使用类别将其他实例变量添加到班级.

类别添加的方法成为类类型的一部分.为了例如,添加到类别中的 NSArray 类的方法包括在内作为编译器期望的 NSArray 实例的方法曲目.然而,在子类中添加到 NSArray 类的方法,不包含在 NSArray 类型中.(这仅对静态类型对象,因为静态类型是唯一的方法编译器可以知道一个对象的类.)

类别方法可以做任何在类中定义的方法适当的可以.在运行时,没有区别.方法添加到类的类别被所有类继承子类,就像其他方法一样.

一个类别接口的声明看起来很像一个类接口声明——除了类别名称列在其中类名后的括号和超类没有被提及.除非它的方法不访问类的任何实例变量,类别必须为它扩展的类导入接口文件:

 #import "ClassName.h"@interface 类名(类别名)//方法声明@结尾

<块引用>

请注意,一个类别不能声明额外的类的实例变量;它只包括方法.然而,类范围内的所有实例变量也在类别的范围.这包括所有实例变量由类声明,即使是声明为@private 的.

您可以添加到一个类别中的类别数量没有限制class,但每个类别名称必须不同,并且每个类别都应该声明和定义一组不同的方法.

I have some NSManagedObject classes created for use with CoreData I need to add some additional properties for formatting I am doing using GRMustache templates.

Here is an example property:

-(NSString *) PriceFormatted {
    NSNumberFormatter *nfm = [[[NSNumberFormatter alloc] init] autorelease];
    [nfm setNumberStyle:NSNumberFormatterCurrencyStyle];
    [nfm setCurrencyCode:[Helpers GetCurrencyCode]];
    [nfm setNegativeFormat:@"-¤#,##0.00"];
    [nfm setMaximumFractionDigits:2];

    return [nfm stringFromNumber:self.Price];
}

I currently have this in my generated NSManagedObject class but this will cause issues if I need to regenerate a new NSManagedObject class.

Can I define these properties in a secondary set of classes - similar to partials in C#?

解决方案

Probably the easiest way is to add a category to your generated managed object.

Here is Apple's documentation on it, it is pretty easy.

To quote:

You can add methods to a class by declaring them in an interface file under a category name and defining them in an implementation file under the same name. The category name indicates that the methods are additions to a class declared elsewhere, not a new class. You cannot, however, use a category to add additional instance variables to a class.

The methods the category adds become part of the class type. For example, methods added to the NSArray class in a category are included as methods the compiler expects an NSArray instance to have in its repertoire. Methods added to the NSArray class in a subclass, however, are not included in the NSArray type. (This matters only for statically typed objects because static typing is the only way the compiler can know an object’s class.)

Category methods can do anything that methods defined in the class proper can do. At runtime, there’s no difference. The methods the category adds to the class are inherited by all the class’s subclasses, just like other methods.

The declaration of a category interface looks very much like a class interface declaration—except the category name is listed within parentheses after the class name and the superclass isn’t mentioned. Unless its methods don’t access any instance variables of the class, the category must import the interface file for the class it extends:

    #import "ClassName.h"   

    @interface ClassName ( CategoryName ) 
        // method declarations 
    @end 

Note that a category can’t declare additional instance variables for the class; it includes only methods. However, all instance variables within the scope of the class are also within the scope of the category. That includes all instance variables declared by the class, even ones declared @private.

There’s no limit to the number of categories that you can add to a class, but each category name must be different, and each should declare and define a different set of methods.

这篇关于使用辅助 .h .m 文件向 NSManagedObject 添加其他属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆