Creating instances of a class in Objective C

  • Thread starter Whovian
  • Start date
  • Tags
    Class
In summary, you are relatively new to Objective C. You are using C strings instead of NSStrings, but you are more comfortable with them. And you know this is probably a bad example of Object-oriented programming, but this is for the sake of example.
  • #1
Whovian
652
3
I'm relatively new to Objective C. I know I'm using C strings instead of NSStrings, but I'm more comfortable with them. And I know this is probably a bad example of Object-oriented programming, but this is for the sake of example.

Code:
#import <Foundation/Foundation.h>

@interface Greeter : NSObject
{
    char* name;
}
-(void) setname : (char*) newname;
-(char*) getname;
-(void) greet;
-(void) ask;
@end

@implementation Greeter
-(void) setname : (char*) newname
{
    name = newname;
}
-(char*) getname
{
    return name;
}
-(void) greet
{
    printf("Hello, %s!\n",name);
}
-(void) ask
{
    printf("What's your name?\n");
    scanf("%s",name);
}
@end

int main()
{
    @autoreleasepool
    {
        Greeter* me;
        [me ask];
        [me greet];
    }
}

Nothing happens. I'm not completely sure why not, but again, I'm new to Objective C's approach to Object Oriented programming.
 
Technology news on Phys.org
  • #2
did you try putting in the args for your main method

int main ( int argc, const char *argv[] )

that may solve your problem especially if the error returned was can't find main method.
 
  • #3
No, I didn't get an error message, and int main() is perfectly valid (though some use int argc,const char* argv[] since it's useful for running the program for the command line.) The problem is that when I run it, nothing happens.
 
  • #4
in many OO languages main() is different from main(...with args...) this is how they distinguish between methods for polymorphism to work ie the methodname+args is the signature that is searched for and called on a match.

okay so put a pritf in the main method before the autorealeasepool stuff and see if it prints out:

printf("Entering main()...");

and

maybe an exiting printf:

printf("Exiting main()...");

this will tell you if main is being called or not and that the autoreleasepool is the issue.
 
  • #5
No, the following has worked perfectly:

Code:
#import <Foundation/Foundation.h>

int main()
{
    @autoreleasepool
    {
        printf("Hello world!");
    }
}
 
  • #6
ahh okay do you need to get an instance of greeter?

Greeter* me = [[Greeter alloc] init];
 
  • #7
Also, I obviously don't know how the scanf function works, since I'm getting an error, so a pointer?
 
  • #8
I've never used Objective C and I don't understand the details of the syntax, but all your problems seem to have the same cause: you declared some pointers to objects, but you never created any objects.

You defimed "me" as a pointer to a Greeter, but it doesn't actually point to a Greeter object. Simiilarly inside the Greeter, "name" is a pointer to a character, but you haven't allocated any space to hold the characters and set "name" to point to that space. So if you are lucky, "scanf" will fail because "name" isn't pointing to a location it can write characters to. If you are unlucky, it will be pointing to a random place that you CAN write characters to but shouldn't, and you have taken the first small step on the road to writing some malware!

I think you need to get straightened out about a basic rule of C: pointers are not arrays, despite the fact that some of the syntax of C makes it look as if they might be.
 
  • #9
AlephZero said:
I've never used Objective C and I don't understand the details of the syntax, but all your problems seem to have the same cause: you declared some pointers to objects, but you never created any objects.

You defimed "me" as a pointer to a Greeter, but it doesn't actually point to a Greeter object. Simiilarly inside the Greeter, "name" is a pointer to a character, but you haven't allocated any space to hold the characters and set "name" to point to that space. So if you are lucky, "scanf" will fail because "name" isn't pointing to a location it can write characters to. If you are unlucky, it will be pointing to a random place that you CAN write characters to but shouldn't, and you have taken the first small step on the road to writing some malware!

I think you need to get straightened out about a basic rule of C: pointers are not arrays, despite the fact that some of the syntax of C makes it look as if they might be.

Yea, I need to get my understanding of scanf straightened out. And for some reason, from my experiences, it appears messages are sent to pointers to objects, which I don't understand, but seems to be the case.
 
  • #10
yes, in c code for pointers you either malloc memory for it:

char *x = malloc(24); // assigns memory but you will have to copy something to it

strcpy("hello world",x);

or you assign it to a variable:

int y=3;
int *z = &y;

or for arrays:

int xxx[100];
int *pxxx = xxx; // notice no & since xxx is an array; equivalent to --> int *pxxx = &xxx[0];

each time you increment pxxx++ it will internally add 2 bytes to the pointer effectively making the pointer point to the next element of the array.

Good catch AlephZero, I was focusing on the Greeter object and didn't look further. I mostly do Java coding and haven't done much C since maybe 20 years ago.
 

1. How do I create an instance of a class in Objective C?

In Objective C, you can create an instance of a class using the "alloc" and "init" methods. The "alloc" method allocates memory for the instance and the "init" method initializes the instance with the necessary values.

2. Can I create multiple instances of the same class in Objective C?

Yes, you can create multiple instances of the same class in Objective C. Each instance will have its own set of properties and values, but they will all share the same methods and behaviors defined in the class.

3. How do I pass parameters to the init method when creating an instance in Objective C?

You can pass parameters to the init method by creating a custom init method in your class with the necessary parameters, and then using that method when creating your instance. For example, if your custom init method is called "initWithName:", you would use it as follows: [[MyClass alloc] initWithName:@"John"].

4. Is there a shorthand way to create an instance of a class in Objective C?

Yes, there is a shorthand way to create an instance of a class in Objective C using the "new" keyword. This will automatically call the "alloc" and "init" methods for you, so you do not have to write them separately. For example, you can create an instance of a class called "MyClass" using the following syntax: [MyClass new].

5. Can I create an instance of a class without using the alloc and init methods in Objective C?

No, you cannot create an instance of a class without using the alloc and init methods in Objective C. These methods are necessary for allocating memory and initializing the instance with the required values. However, you can create a custom init method that does not require the use of the alloc method, and then use that method to create your instance.

Similar threads

  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
2
Replies
35
Views
2K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
Back
Top