Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom property attributes in Objective-c

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

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.

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.

I tried using typdef. For example:

typdef int serializableInt;
serializableInt myInt;

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().

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.

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.

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

like image 769
Roberto Avatar asked Feb 07 '11 07:02

Roberto


People also ask

What is properties in Objective-C?

Objective-C properties offer a way to define the information that a class is intended to encapsulate. As you saw in Properties Control Access to an Object's Values, property declarations are included in the interface for a class, like this: @interface XYZPerson : NSObject.


1 Answers

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

And in the 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)
like image 197
Ossir Avatar answered Oct 05 '22 07:10

Ossir