Is this a valid way to create class properties (i.e. dot syntax) in Objective-C?

Possible duplicate:
How to declare class level properties in Objective-C?
Objective-C 2.0 dot notation - class methods?

Objective-C @property declarations do not allow class properties to be specified. For example, you cannot do this ...

@interface NSObject (MyExtension)

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

@end

      

... but I remember reading that properties are really just syntactic sugar for getting / setting messages, so when you type something.foo you really just send either [something foo] or [something setFoo: newFooVal].

So I got an idea and I wrote this regular class member ...

@interface NSObject (MyExtension)

    + (NSString *) className;

@end

      

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

#import "IS_NSObject.h"

@implementation NSObject (MyExtension)

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

@end

      

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

NSString * holderForClassName = UICollectionView.className;

      

Voila! Class level property! Even a compiler warning!

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

NSString * holderForClassName = [UICollectionView className];

      

So of course the question is ... basically I just "discovered" (ie was always there, but didn't use it like that) a class level property?

(Well, okay, property syntax, but what I got after the chaining, not a nested set of brackets.)

+3


source to share


1 answer


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

As you say, properties are just syntactic sugar for getter method and setter method.



@property and @synthesize just help you implement properties, they boil down to the same thing as if you were to inject getter and setter yourself.

In other words, nothing new or unexpected in your discovery, sorry. But class level properties can be useful sometimes, although I think some (most?) ObjC coding guidelines suggest calling class level gates with message syntax. It could also be because it is not showing up in intellisense when you are using dot notation.

+3


source







All Articles