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
  • 22,070
  • 5
  • 50
  • 76
Pierre de LESPINAY
  • 42,450
  • 54
  • 205
  • 294

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
  • 16,631
  • 5
  • 29
  • 43
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,611
  • 2
  • 18
  • 30