自定义属性在Objective-C的属性 [英] Custom property attributes in Objective-c

查看:180
本文介绍了自定义属性在Objective-C的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可自定义属性的属性来在Objective-C创建就像在VB.NET?例如,在VB.NET可以创建可浏览属性,并在运行时读取它来确定是否应显示的属性或没有。

Can custom property attributes be created in Objective-C just like in VB.NET? For example, in VB.NET you can create the "Browsable" attribute and read it at runtime to determine whether you should display a property or not.

Public Class Employee
    <Browsable(True)> _
    Public Property Property1() As String
        Get

        End Get
        Set(ByVal Value As String)

        End Set
    End Property

    <Browsable(False)> _
    Public Property Property2() As String
        Get

        End Get
        Set(ByVal Value As String)

        End Set
    End Property
End Class

我愿做同样在Objective-C,哪怕是只能在编译时设定的,不能在所有改变的固定属性。

I would like to do the same in Objective-C, even if it is a fixed attribute that can only be set at compile time and cannot be changed at all.

我试图做的是一个属性添加到我的类的属性,以确定属性是否应被序列化与否。

What I'm trying to do is to add an attribute to properties of my class to determine whether the properties should be serialized or not.

我知道标准Objective-C的属性(只读,非原子等),但这些不帮我...除非你要使用他们的创造性的方式。我也看了成使用C与属性 __属性__((插入属性在这里))关键字,但C有服务于特定目的的特定属性,我甚至确保您能够在运行时读取它们。如果我错过了一个可以帮助我,让我知道。

I know the standard Objective-C attributes (readonly, nonatomic, etc.), but those don't help me... unless you have a creative way of using them. I also looked into using C attributes with the __attribute__(("Insert attribute here")) keyword, but C has specific attributes that serve specific purposes, and I'm not even sure you can read them at runtime. If I missed one that can help me, let me know.

我试着用 typdef 。例如:

typdef int serializableInt;
serializableInt myInt;

和使用 property_getAttributes() Objective-C运行的功能,但所有它告诉我是敏是一个int。我猜的typedef是pretty很像在这种情况下,宏...除非我能在运行时创建类型serializableInt的变量。总之,这里的<一个href=\"http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtPropertyIntrospection.html#//apple_ref/doc/uid/TP40008048-CH101\">Apple's您从 property_getAttributes获取值文档()

and use the property_getAttributes() Objective-C runtime function, but all it tells me is that myInt is an int. I guess typedef is pretty much like a macro in this case... unless I can create a variable of type serializableInt at runtime. Anyhow, here's Apple's documentation on the values you get from property_getAttributes().

另一个要求是该属性具有NSObject的子类,以及基本数据类型工作。我想过增加了类黑名单或白名单作为伊娃说会告诉我哪些属性跳过或序列化,这基本上是同样的想法的想法。我只是想,因此很容易,当你看到一个类的头文件来了解移动的黑/白名单属性,这是我对面创建的任何类一致,这是不容易出错。

The other requirement is that this attribute has to work with NSObject sub-classes as well as primitive data types. I thought about the idea of adding to the class a black lists or white lists as an ivar that would tell me which properties to skip or serialize, which is basically the same idea. I'm just trying to move that black/white list to attributes so it's easy to understand when you see the header file of a class, it's consistent across any class I create and it's less error prone.

此外,这是要考虑的事情。我并不真的需要attribue有一个值(TRUE或FALSE; 1,2,3;或其他),因为该属性本身就是价值。如果该属性存在,那么序列化;否则,请跳过。

Also, this is something to consider. I don't really need the attribue to have a value (TRUE or FALSE; 1, 2, 3; or whatever) because the attribute itself is the value. If the attribute exists, then serialize; otherwise, skip.

任何帮助是AP preciated。如果你肯定知道这是不可能的Objective-C的,然后让我知道。谢谢你。

Any help is appreciated. If you know for sure that this is not possible on Objective-C, then let me know. Thanks.

推荐答案

如果您要添加属性属性类,方法或伊娃,你可以尝试使用的 github.com/libObjCAttr 。这真的很容易使用,通过添加的CocoaPods它,然后你可以添加这样的属性:

If you want to add attribute to property, class, method or ivar, you can try to use github.com/libObjCAttr. It's really easy to use, add it via cocoapods, and then you can add attribute like that:

@interface Foo

RF_ATTRIBUTE(YourAttributeClass, property1 = value1)
@property id bar;

@end

和在code:

YourAttributeClass *attribute = [NSDate RF_attributeForProperty:@"bar" withAttributeType:[YourAttributeClass class]];
// Do whatever you want with attribute, nil if no attribute with specified class
NSLog(@"%@", attribute.property1)

这篇关于自定义属性在Objective-C的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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