3

Possible Duplicate:
How do I declare class-level properties in Objective-C?
Objective-C 2.0 dot notation - Class methods?

Objective-C's @property declarations do not allow you to specify class-level properties. For instance, you can't do this...

@interface NSObject (MyExtension)

    + @property (nonatomic, copy) NSString * className;

@end

...but I remember reading that properties are really just syntactical sugar for get/set messages, so when you type something.foo, you're really just sending either a [something foo] or a [something setFoo:newFooVal] message.

So I had an idea, and wrote this normal class-level member...

@interface NSObject (MyExtension)

    + (NSString *) className;

@end

and in the 'm' file, added this (again, completely normal)...

#import "IS_NSObject.h"

@implementation NSObject (MyExtension)

  + (NSString *) className
    {
        return NSStringFromClass([self class]);
    }

@end

...and sure enough, to get a string representation of any class name, I can now just do this...

NSString * holderForClassName = UICollectionView.className;

Voila! A class-level property! Not even a compiler warning!

Actually, the only thing missing is the suggestion in the code-completion/intellisense. But it does of course if you type it like this...

NSString * holderForClassName = [UICollectionView className];

So of course, the question is... did I basically just 'discover' (i.e. it's always been right there but its not used like this) a class-level property?

(Well, ok, property syntax, but that's what I was after for chaining purposes rather than nesting tons of brackets.)

Community
  • 1
  • 1
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
  • No, you discovered class methods. What's the difference between a method and a property? Not much other than the compiler will do most of the work for you when you synthesize a property and they are used pretty much exclusively to get/set instance variables. – trojanfoe Jan 19 '13 at 10:13
  • 1
    Possible duplicate: [How do I declare class-level properties in Objective-C?](http://stackoverflow.com/q/695980/1402846). – Pang Jan 19 '13 at 10:16
  • Not a duplicate because that person was asking how to do it and the answers say you can't. I'm bringing up how you can in fact use dot-syntax on a class, ala (pseudo-)class properties. – Mark A. Donohoe Jan 19 '13 at 10:18
  • LOL... ok, ok... I get it! This is an evil thing to do! Still, I like it for these kinds of things so I shall use it anyway. :) – Mark A. Donohoe Jan 19 '13 at 10:23
  • I think it's only evil if you call zero-argument methods using dot notation; I don't think the answerer (?!?) of that question was saying it was evil to use dot notation on a class-level getter/setter method. – trojanfoe Jan 19 '13 at 10:26
  • For me, I think for readability, I'll stick with it if I'm using it to return something that is property-like. In other words, in my example, the class *is* the object I'm trying to get a property (it's textual representation) of, so here I feel it makes sense. The object instance is the class object itself... if that makes sense. 'Gimme the name of that book' vs. 'Gimme the name of the book class.' 'Name' is just a property of whatever you're asking it on. – Mark A. Donohoe Jan 19 '13 at 10:30
  • It is, in fact, evil to use dot notation for anything but declared property, or a setter (when on LHS) or a zero argument non-void method (when on the RHS). – Carl Veazey Jan 19 '13 at 10:34
  • IMHO, for your case (`UICollectionView.className`), it's not evil because it's a `readonly` property (there's no setter) and you've implemented it as such (actually you also implemented it as `nonatomic` since it lacks `@synchronized`). – Pang Jan 19 '13 at 10:36
  • Even if it were a read-write though, I think it would be ok... if it made sense. For instance, an instance-counter that you increment or decrement when class instances are created/destroyed. I think SomeClass.instanceCount is also a valid use even though that is read-write. – Mark A. Donohoe Jan 19 '13 at 10:38
  • Dot syntax is completely orthogonal to `@property`. The dot is a convenience added at the same time as `@property`, but the two are independent. Note that adding categories to existing framework categories is generally frowned upon; if you do so, put a prefix on that method. – bbum Jan 19 '13 at 16:50

1 Answers1

3

You are describing a feature that has always been in the language.

As you mention properties are just syntactic sugar for a getter method and a setter method.

@property and @synthesize simply helps you implement properties, they compile down to the same thing as it would if you implemented the getter and setter yourself.

In other words, nothing new or unexpected in your discovery, sorry. But class level properties can be helpful sometimes, though I think some (most?) ObjC coding guidelines suggest calling class level getters with message syntax. That might also be why it does not show up in intellisense when you are using dotted notation.

driis
  • 161,458
  • 45
  • 265
  • 341
  • Yeah... not really seeing a difference between properties and get/set methods. If anything, they're just a convenience that ensures your methods adhere to key-value coding and help when you declaratively synthesize them. Still, I like this because it allows me to easily chain things using dot-syntax rather than brackets. – Mark A. Donohoe Jan 19 '13 at 10:17