Class methods which create new instances

They are class methods, not static methods1. This specific type, creating autoreleased objects, can be referred to as “factory methods” (formerly also “convenience constructors”), and they are discussed in the Concepts in ObjC Guide. They go something like this: + (instancetype)whatsisWithThingummy: (Thingummy *)theThingummy { return [[self alloc] initWithThingummy:theThingummy]; } Where Whatsis is your class, and … Read more

What’s the difference between “class method” and “static method”?

So my question is why are they called class methods instead of a static method? What is the difference between a static method and a class method? From Wikipedia: Static methods neither require an instance of the class nor can they implicitly access the data (or this, self, Me, etc.) of such an instance. This … Read more

What is the purpose of class methods?

Class methods are for when you need to have methods that aren’t specific to any particular instance, but still involve the class in some way. The most interesting thing about them is that they can be overridden by subclasses, something that’s simply not possible in Java’s static methods or Python’s module-level functions. If you have … Read more

How to make a class property? [duplicate]

Here’s how I would do this: class ClassPropertyDescriptor(object): def __init__(self, fget, fset=None): self.fget = fget self.fset = fset def __get__(self, obj, klass=None): if klass is None: klass = type(obj) return self.fget.__get__(obj, klass)() def __set__(self, obj, value): if not self.fset: raise AttributeError(“can’t set attribute”) type_ = type(obj) return self.fset.__get__(obj, type_)(value) def setter(self, func): if not isinstance(func, … Read more

Meaning of @classmethod and @staticmethod for beginner? [duplicate]

Though classmethod and staticmethod are quite similar, there’s a slight difference in usage for both entities: classmethod must have a reference to a class object as the first parameter, whereas staticmethod can have no parameters at all. Example class Date(object): def __init__(self, day=0, month=0, year=0): self.day = day self.month = month self.year = year @classmethod … Read more

What is the difference between class and instance methods?

Like most of the other answers have said, instance methods use an instance of a class, whereas a class method can be used with just the class name. In Objective-C they are defined thusly: @interface MyClass : NSObject + (void)aClassMethod; – (void)anInstanceMethod; @end They could then be used like so: [MyClass aClassMethod]; MyClass *object = … Read more