Objective-C and its relation to C
Actually, I am very new to Mobile programming, and need to take your opinions.
I am a Java Developer with a C background, and I need to start Learning Objective-C with a target to do mobile app for iPhone and开发者_如何学运维 to refresh my knowledge in C (as I know, Objective-C is a pure superset for C, ain't it?).
So, the question is, With objective C, can I achieve my two objectives which are again:
- Do mobile dev for iPhone refresh my
- refresh my knowledge with C
Objective C is another object oriented extension of C. Everything you know in C will work.
The best document to understand Objective C is
http://developer.apple.com/mac/library/documentation/cocoa/conceptual/ObjectiveC/ObjC.pdf
You can do a lot with just C but you will be severely crippled. Every feature of C works in Obj-C.
If you really enjoy a world of hurt you could consider Objective-C++ which combines both C++ and objective-C
Yes, Objective-C is a pure superset of C; it doesn't feel greatly like C, though. It's really more like java, +- some syntax changes, than traditional pointer-arithmetic-using systems-programming low-level C. The most obvious example being, for instance, that iPhone programming traditionally uses the NSString class to represent strings, rather than a C-style *char. You still can use *char to represent a string in Objective-C; it's just frowned upon and for practically everything you'll use it for (including, in all likelihood, 100% of all iPhone functionality) the NSString class is both cleaner and better-featured. That said, it'll get you back to the syntax pretty well, if not the idioms.
And yes, Objective-C is literally mandatory for iPhone dev. So yes, learning Objective-C is the way to accomplish both your goals.
Objective-C is a strict superset of C. But, there are a lot of add ons in syntax that are overwhelming, in my opinion, and I can guarantee your going to use a lot of those additions to the C language because of Apple's frameworks. So if you were going to refresh your memory on C I would, personally, do something else.
As for an example:
All your programming is done, in the majority of case, in two classes. The AppDelegate or ViewController. Calling functions in C looks like:
foo();
But, because all this programming is done in classes you need to call it with an object:
FooViewController *FooCaller;
[FooCaller foo];
You use this syntax very often in iPhone programming, but the C syntax is not used often in iPhone programming.
Also, as for methodology, it is very different also because Objective-C is OO and C is procedural.
Objective-C is a superset of C.
- Yes.
- Maybe. It is possible to use mostly objective C features without going back to regular C.
An interesting note is that Objective-C began its life as a sort of preprocessor to C. From /usr/include/objc/objc.h
:
typedef struct objc_class *Class;
typedef struct objc_object {
Class isa;
} *id;
Each Objective-C object can be described as a struct with a pointer to its class, its ivars, and function pointers for its methods. Message sending, etc. complicate things but when it comes down to it, everything could be done in C if you were patient enough.
In my experience with mac development, the only times I've needed to use C was when I fell back to the older Carbon apis. Cocoa is all Objective C and quite different from old school c. As far as I know, all of the iPhone APIs use objective c and you would only ever need to use gangster style c if you're using a library already written with in it.
精彩评论