Failing to compile Objective-C on Xcode 4
I've bought Xcode 4 in order to start developing iOS applications. I come from a background of C, C++. I've entered the examples from here: http://en.wikibooks.org/wiki/Objective-C_Programming/syntax to an XCode project ( Command line tool->Foundation template, not that I know what it means, just what I read somewhere ). I named the files: point.h, point.m, main.m
At first, I got 3 errors. I got rid of 2 of them by changing the class's开发者_JAVA百科 name from "Point" to "MyClass"
but now i get the following warning:
Method '+new' not found (return type defaults to 'id')
About the line:
MyClass *point = [MyClass new]
I also get no output in the debugger output section.
Any ideas how to solve this?
Seems you've got some issues...
I would say you forgot to inherit from NSObject
, so the method +new
is not found.
@interface MyClass : NSObject
@end
Ok, let's start at the beginning. Read this.
Now, "+ new" is meaningful. It's telling you that your requesting to send a message to the CLASS "MyClass". This is very different from an instance of "MyClass". Class messages in other languages are referred to as "static methods" or "class methods". In ObjC, class methods are represented with a +
, and instance methods are represented with a -
.
The most common class method is alloc
. In ObjC you send this message to the class to create a new instance and return it. Once you've allocated memory for the new instance, you can send the init
message to it. From then on, you can retain
or release
it. I think you get the idea. Most messages are intended to be sent to the instance of a class, not the class itself.
So....
MyClass* c = [[MyClass alloc] init];
[c doStuff];
[c release];
First, we allocate new memory to hold an object of MyClass
. Then we send it an init
message to ensure that it's all setup properly. After that, we send a doStuff
message to the initialized instance of MyClass stored in the c
variable. Then we release the memory by sending a release
message.
A note about retain/release.
When we allocated, the retain count goes from 0 to 1. When we released, the retain count goes from 1 to 0. This is akin to some "smart pointers" in other languages. Once the retain count reaches 0, the object is deallocated. So, when the release message is sent here, you should be able to set a breakpoint inside your MyClass dealloc block (an instance method). Just remember, when you specify init or dealloc blocks, always send the message to super as well so that you get proper cleanup.
edit:
Yes, I think you should inherit from NSObject
as @macmade says. You get all kinds of really useful stuff from this base class like new, alloc, init, release, retain, autorelease, etc. The reason for this is because your instance will be living on the heap, not the stack. In a language like Java or C#, you would get this unified type system transparently. Since this is ObjC, you need to opt-in for it.
If you would rather manage your Point like a struct (have a look at the built in CGPoint), you can do that too, but in that case you would need to manage it very differently. It would be stack memory, not heap memory. You can get a good example of the difference by looking at the source to things like CGPoint or CGRect. Actually, all this stuff is very clearly documented in Apple's The Objective-C Programming Language.
精彩评论