Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allocate a new object without Foundation?

I want to construct a simple Objective C program without Foundation. I tried:

#include <stdio.h>

@interface Foo{
    char * bar;
}
-(void)hello;
@end

@implementation Foo
-(void)hello {
    printf("Hello world!");
}
@end

int main(){
    Foo * foo = [Foo alloc];
    return 0;
}

Then it tells me Foo may not respond to +alloc and autorelease called without pool...

How can I initialize an object without using alloc from Foundation?

like image 276
SwiftMango Avatar asked Mar 23 '13 22:03

SwiftMango


People also ask

How to create an object without using new operator in Java?

Yes, we can create an object without using new operator in java. If we know the name of the class and it has a public default constructor than we can create an object in the following way. MyObject object = ( MyObject) Class. forName("com.w3spoint.MyObject"). newInstance(); The clone () can be used to create a copy of an existing object.

How to instantiate a prefab in a game object?

The right way is to make a prefab , you allocate that prefab as an Object , then whenever you want to instantiate just use the reference of this Object and cast it as a GameObject. Show activity on this post. To load a prefab into a variable so you can instantiate it later you would use the following code:

Is there a way to create an object behind the scenes?

However, you can accomplish the same design pattern (ie. abstract away the building of your objects, and then simply call them with a single command later) through a Factory pattern. When you want an object, call something like Factory.CreateGameEntity (), and that method will handle everything behind the scenes.

Why does C++ support stack-allocated objects?

C++ supports stack-allocated objects for the reason of runtime efficiency. Stack-based objects are implicitly managed by the C++ compiler. They are destroyed when they go out of scope and dynamically allocated objects must be manually released, using the delete operator otherwise, a memory leak occurs.


2 Answers

It's very simple to create a new object without using alloc from Foundation. The Objective-C runtime library itself provides functions that allow one to allocate objects from classes and deallocate them later, so that you need no extra library to create and destruct objects.

The function id class_createInstance(Class cls, size_t extraBytes) receives a class object, from which to allocate a new object, and an integer, which is almost always zero, and returns a new instance of cls.

Similarly, the function id object_dispose(id obj) takes an Objective-C object, calls the C++ destructor of every C++ object instance variable, removes existing associated references and frees it.

class_createInstance and object_dispose are both declared in /usr/include/objc/runtime.h.

So, you can implement your own +alloc and -dealloc methods. Your program would look like this:

#include <stdio.h>
#include <objc/runtime.h>

@interface Foo{
    char * bar;
}
+(id)alloc;
-(void)hello;
@end

@implementation Foo
+(id)alloc {
    // Returns a new 'Foo' object. In this case, 'self' is a 'Foo' class object,
    // whose type is 'Class', as required by `class_createInstance`.
    return class_createInstance(self, 0);
}
-(void)dealloc {
    object_dispose(self);
}
-(void)hello {
    printf("Hello world!");
}
@end

int main(){
    Foo *foo = [Foo alloc];
    [foo hello];
    [foo dealloc];

    return 0;
}

Compile it as you normally do:

gcc Foo.m -o Foo -lobjc

That's all!

like image 151
LuisABOL Avatar answered Sep 18 '22 15:09

LuisABOL


You can write Objective C without Foundation, but of course you may end up re-inventing the wheel to some degree.

As millimoose suggests, you could use the GNUStep Objective C runtime.

For more info, see:

  • http://jongampark.wordpress.com/2008/05/02/objective-c-without-cocoa/
  • How do you create a simple Objective-C command line project in Xcode
  • https://softwareengineering.stackexchange.com/questions/101832/why-is-objective-c-not-widely-used-beyond-cocoa-environments
  • How does one use Obj-C 2.0 with GNUstep?
like image 33
occulus Avatar answered Sep 17 '22 15:09

occulus