Inherits from NSObject
Declared in JAGProperty.h
JAGProperty.m

Overview

JAGProperty is an Objective-C wrapper for a class’s properties that allows dynamic access to information about the property. It is backed by an objc_property_t object, but provides more friendly methods for accessing the information.

Suppose you have properties

    @property (nonatomic, readonly, copy) NSString* title;
    @property (assign, getter=getCount) int count;

and synthesizers

    @synthesize title = _title;
    @synthesize count;

The JAGProperty object for the first property has a variety of useful methods:

    property.name == @"title";
    property.isObject == YES;
    property.propertyClass == [NSString class];
    property.isNumber == NO;
    property.typeEncoding == @"@\"NSString\"";
    property.isNonAtomic == YES;
    property.isReadOnly == YES;
    property.ivarName == @"_title";
    property.customGetter == nil;
    property.getter == @selector(title);
    property.setterSemantics == JAGPropertySetterSemanticsCopy;

The JAGProperty object for the second property also has a variety of useful methods:

    property.name == @"count";
    property.isObject == YES;
    property.propertyClass == nil;
    property.isNumber == YES;
    property.typeEncoding == @"i";
    property.isNonAtomic == NO;
    property.isReadOnly == NO;
    property.ivarName == @"count";
    property.customGetter == @selector(getCount);
    property.getter == @selector(getCount);
    property.setterSemantics == JAGPropertySetterSemanticsAssign;

You can construct a JAGProperty with the appropriate objc_property_t object, but JAGPropertyFinder provides the suggested ways to find properties for a given class.

Tasks

Instance Methods

attributeEncodings

A comma-separated list of attributes.

- (NSString *)attributeEncodings

Return Value

An NSString of the comma-separated attributes.

Discussion

Possible attributes are:

  • R The property is read-only (readonly).
  • C The property is a copy of the value last assigned (copy).
  • & The property is a reference to the value last assigned (retain).
  • N The property is non-atomic (nonatomic).
  • G<myGetter> The property defines a custom getter selector myGetter. The name follows the G (for example, GmyGetter).
  • S<mySetter:> The property defines a custom setter selector mySetter. The name follows the S (for example, SmySetter:).
  • D The property is dynamic (@dynamic).
  • W The property is a weak reference (__weak).
  • P The property is eligible for garbage collection.
  • t<encoding> Specifies the type using old-style encoding.

Declared In

JAGProperty.h

canAcceptValue:

Test whether you can [model setValue:value forKey:[property name]] without receiving an NSInvalidArgumentException.

- (BOOL)canAcceptValue:(id)value

Parameters

value

Value to check for acceptance.

Return Value

YES if the property can be set with this value.

Discussion

This method is still under development. We are aiming for it to catch some cases that would lead to an exception, but we are erring on the side of letting bad things through rather than keeping good things out.

Declared In

JAGProperty.h

customGetter

Selector for custom getter. Nil if no custom getter.

- (SEL)customGetter

Return Value

Selector for custom getter. Nil if no custom getter.

Declared In

JAGProperty.h

customSetter

Selector for custom setter. Nil if no custom setter.

- (SEL)customSetter

Return Value

Selector for custom setter. Nil if no custom setter.

Declared In

JAGProperty.h

getter

Selector for getter. Defaults to @selector(propertyname) if no custom getter.

- (SEL)getter

Return Value

Selector for getter. Defaults to @selector(propertyname) if no custom getter.

Declared In

JAGProperty.h

isBlock

YES if the property is for a Block

- (BOOL)isBlock

Return Value

YES if the property is for a Block

Declared In

JAGProperty.h

isCollection

YES if the property is for an NSArray or NSSet subclass

- (BOOL)isCollection

Return Value

YES if the property is for an NSArray or NSSet subclass

Declared In

JAGProperty.h

isDynamic

Whether the property is dynamic.

- (BOOL)isDynamic

Return Value

YES if property has D attribute.

Declared In

JAGProperty.h

isEligibleForGarbageCollection

Whether the object is elegible for Garbage Collection in OS X.

- (BOOL)isEligibleForGarbageCollection

Return Value

YES if property has P attribute.

Declared In

JAGProperty.h

isId

YES if the property is for an id

- (BOOL)isId

Return Value

YES if the property is for an id

Declared In

JAGProperty.h

isNonAtomic

Whether the property is nonatomic.

- (BOOL)isNonAtomic

Return Value

YES if property has N attribute.

Declared In

JAGProperty.h

isNumber

YES if the property is any sort of integer, float, char, or BOOL

- (BOOL)isNumber

Return Value

YES if the property is any sort of integer, float, char, or BOOL

Declared In

JAGProperty.h

isObject

YES if the property is for an NSObject subclass or id.

- (BOOL)isObject

Return Value

YES if the property is for an NSObject subclass or id.

Declared In

JAGProperty.h

isReadOnly

Whether the property is readonly.

- (BOOL)isReadOnly

Return Value

YES if property has R attribute.

Declared In

JAGProperty.h

isWeak

Whether the property is a weak pointer, in either OS X Garbage Collection or iOS ARC.

- (BOOL)isWeak

Return Value

YES if property has W attribute, or is an Object and has setter semantics JAGPropertySetterSemanticsAssign

Discussion

The “W” attribute works for garbage-collected environments, but it does not detect ARC weak references. These are detected by an assign-pointer to an object. This also means that an iOS 4 object property with assign semantics will also return YES.

Declared In

JAGProperty.h

isWeakReference

Whether the property is a __weak reference, with respect to Garbage Collection.

Note this has nothing to do with iOS ARC weak label. An ARC property

- (BOOL)isWeakReference

Return Value

YES if property has W attribute.

Discussion

    @property (weak) id delegate;

will have isWeakReference == NO. @see isWeak for how to handle that case.

Declared In

JAGProperty.h

ivarName

Name of ivar backing the property.

- (NSString *)ivarName

Return Value

Name of ivar backing the property.

Declared In

JAGProperty.h

name

Name of property

- (NSString *)name

Return Value

Name of property

Declared In

JAGProperty.h

propertyClass

The class of the property, if it is a defined object.

- (Class)propertyClass

Return Value

The Class of the property, or nil if undefined.

Discussion

If it is an id or not an object, return nil.

Declared In

JAGProperty.h

setter

Selector for setter. Defaults to @selector(setPropertyname:) if no custom getter.

- (SEL)setter

Return Value

Selector for setter. Defaults to @selector(setPropertyname:) if no custom getter.

Declared In

JAGProperty.h

setterSemantics

Whether property is assign, retain, or copy.

- (JAGPropertySetterSemantics)setterSemantics

Return Value

JAGPropertySetterSemantics for assign, retain, or copy.

Discussion

Note that in ARC, strong is the same as retain, and weak is the same as assign.

Declared In

JAGProperty.h

typeEncoding

An NSString denoting the scalar/object type of the property.

- (NSString *)typeEncoding

Return Value

An NSString of the type encoding.

Discussion

Note that for properties for objects with a defined class, the return is the NSString @"CLASS_NAME" (including the @"", so it’s equivalent to @"@\"CLASS_NAME\"").

  • c A char (including BOOL)
  • i An int
  • s A short
  • l A long l is treated as a 32-bit quantity on 64-bit programs.
  • q A long long
  • C An unsigned char
  • I An unsigned int
  • S An unsigned short
  • L An unsigned long
  • Q An unsigned long long
  • f A float
  • d A double
  • B A C++ bool or a C99 _Bool
  • v A void
  • * A character string (char *)
  • @ An object (whether statically typed or typed id). For statically typed objects, it is of the form: @"NSString" Blocks are encoded as @?
  • # A class object (Class)
  • : A method selector (SEL)
  • [array type] An array, eg [12^f]
  • {name=type...} A structure, eg {example=@*i}
  • (name=type...) A union
  • bNUM A bit field of num bits
  • ^type A pointer to type, eg ^i
  • ? An unknown type (among other things, this code is used for function pointers)

Declared In

JAGProperty.h