0

Does someone knows how to make this kind of "class auto instantiator":

myDict = [NSDictionnary dictionnaryWithCapacity: 0];

I can't find any resource on this (maybe I just don't know the terminology)

Larme
  • 24,190
  • 6
  • 51
  • 81
Pierre de LESPINAY
  • 44,700
  • 57
  • 210
  • 307

3 Answers3

2

Not sure what you mean... Do you mean a class method to create an object?

@implementation myClass

+(myClass *)myClassWithParameter:(int)whatever
{
    myClass instance = [[myClass alloc] init];
    [instance doWhatever:whatever];
    return instance;
}
jcaron
  • 17,302
  • 6
  • 32
  • 46
0

It's a class method (denoted by "+" instead of "-").

+ (MyClass *)myClassWithObject:(ObjectType *)object
{
    //do whatever you need to make an instance of MyClass using that object's data
    return myClassInstance;
}
Mike Boch
  • 141
  • 7
0

That's just a static method which you can use in place of alloc/init. In objective-c, static method are called on an instance of the meta-class which is why they are sometimes called "Class methods".

@implementation TheBestObjectEver
+ (instancetype)makeMeAnObject {
    return [[self alloc] init];
}
@end

TheBestObjectEver *myObject = [TheBestObjectEver makeMeAnObject];
CrimsonChris
  • 4,651
  • 2
  • 19
  • 30