How to instantiate a class in Objective-C that don't inherit from NSObject -



How to instantiate a class in Objective-C that don't inherit from NSObject -

given this:

person.h:

@interface person { } - (void) sayhello; @end

person.m:

#import "person.h" @implementation person - (void)sayhello { printf("%s", "steve"); } @end

how instantiate person? tried this:

person *p = [person new];

that doesn't work, nor this:

person *p = [person alloc];

[update]

i forgot tell, tried inheriting nsobject, new , alloc works. i'm curious if can instantiate class doesn't inherit nsobject?

you absolutely can so. class needs implement +alloc itself, way nsobject does. @ base, means using malloc() grab chunk of memory big plenty fit construction defining instance of class.

reference-counted memory management nice (retain/release); part of nsobject protocol. can adopt protocol , implement these methods too.

for reference, can @ the object class, root objc class nsobject, apple provides in open source repository objective-c runtime:

@implementation object // snip... + alloc { homecoming (*_zonealloc)((class)self, 0, malloc_default_zone()); } // ... - init { homecoming self; } // , on...

that beingness said, should think of nsobject integral part of objc runtime. there's little if reason implement own root class outside of curiosity, investigation, or experimentation (which should, however, not discouraged @ all).

objective-c class instantiation

Comments

Popular posts from this blog

How do I check if an insert was successful with MySQLdb in Python? -

delphi - blogger via idHTTP : error 400 bad request -

postgresql - ERROR: operator is not unique: unknown + unknown -