0

I would like to start programming on Mac. I have some questions:

  • I don't really like Objective-C and I somewhat know C, I read that Objective-C is a "dialect" of C - is it possible to program in C and use all the libraries and frameworks provided by Mac OS (this is Cocoa, right?)?

  • is it possible to draw GUI in XCode's GUI Builder and then fill the logic in plain C?

  • if I want to use, say, Scintilla - how do I "load" its text editing component into a window in GUI builder? And how do I access its text buffers? And events?

4 Answers 4

11

Just learn Objective-C. It isn't that hard; much, much, simpler than, say, C++. The object model is extremely similar to Java or SmallTalk.

The entire Cocoa stack of APIs are all in Objective-C, the documentation is all in Objective-C, many of the design patterns (KVO, KVC, delegation) are inherently Objective-C isms and all of the examples are Objective-C.

You are going to have to have a working knowledge of Objective-C to effectively program the system anyway. Any attempts to avoid Objective-C is just going to make your job harder, yield less maintainable code, and vastly reduce the # of folks in the community who can help.

Sign up to request clarification or add additional context in comments.

Comments

7

Sure, by interacting with the runtime directly. It means you'll be doing a lot of objc_msgSend() et al. It's going to be horribly painful, you might as well just learn the Objective-C syntax and be done with it. (I explicitly note the syntax, because you will still need to learn the way objc does things in order to even use the C APIs).

2 Comments

I got downvoted, too. Your answer is correct, but I'd emphasize the painful part. Seems like a waste of effort over just writing [foo doSomething: bar];.
@bbum, @jer: I suspect the questioner doesn't like the answer "learn Objective-C anyway, your life will be easier".
2

The bit I can be least helpful on first: without having heard of it, according to its website Scintilla is for win32 and GTK+. As I believe that there's still no Cocoa native version of GTK+, there is no way to use a Scintilla component in a Cocoa program. OS X is not based on X11 (or Win32, for that matter).

Objective-C adds Smalltalk-style objects and dynamic dispatch to C. However, it is a strict superset, so C code is directly callable. The various GUI components rely on the dynamic, reflective nature of Objective-C so cannot directly call C code. However, the whole toolkit is built around the model-view-controller paradigm. It is quite feasible to design your view in Interface Builder, write a thin shim of a controller in Objective-C that does little more than call appropriate C functions and write your model entirely in C. C code can call Objective-C code, so you can wrap as many of the system objects as you want.

So this i pretty much a 'yes' to your second bullet point. Also relevant is that although Objective-C springs from C, Apple have made C++ fully callable (search for Objective-C++), so that's also an option.

Well worth looking into is Core Foundation. OS X originally supported two top-level programming environments — Cocoa and Carbon. Cocoa is Objective-C, Carbon is C with a bunch of legacy support libraries (and is now deprecated, with no 64 bit runtime to be supplied). To support both of these, much of the core system functionality is exposed through C interfaces at the lowest level, including all collections, strings and other relatively primitive objects. A bunch of other performance critical things like Core Text, Core Graphics, etc are also normally done via a straight C interface, even if you're otherwise completely enthusiastic about Objective-C.

6 Comments

Yes, but Core Foundation, Core Graphics, Core Audio, etc, not only aren't deprecated but are in many cases the only way in which OS X exposes certain functionality. And because of e.g. Core Text, CFString is going to remain forever as a toll-free bridge of NSString.
Those aren't really a part of Carbon. BTW: A very large chunk of CoreFoundation is implemented in Objective-C (not really relevant, but still interesting0.
Sorry — since my original answer says that Carbon is deprecated and then mentions the Core <etc> libraries separately yet you nevertheless felt the need to comment that Carbon is deprecated, I assumed you were confused. I'm aware that Carbon is quite separate and hope I haven't created any ambiguity.
@bbum I'm curious which part of CoreFoundation is implemented in Objective-C... I thought NSCFString and such are implemented in C, not the other way round. Aren't they?
@Yuji: Not a complete answer, but I've seen NSURL methods attributed to Core Foundation in Instruments. Easiest way to find out completely would be to nm /System/Library/Frameworks/CoreFoundation.framework/CoreFoundation and grep for square brackets.
|
0

There is a C-based API for UI programming on Mac OS X called Carbon. If you really cannot stand the sight of Objective-C I would start there. Many Cocoa classes and APIs have corresponding Carbon datatypes, etc. and vice versa but not all of them. Fair warning, Carbon is considered a "legacy" API, and it will likely continue to be marginalized as time goes on. Many Carbon APIs are not available for 64bit applications for example

Cocoa specifically is an Objective-C based API, using it from C would be awkward and difficult if possible at all. It would require some knowledge of Objective-C runtime APIs and possibly internals or otherwise writing a bunch of wrappers.

6 Comments

Do NOT use Carbon for anything. You can write your backend logic in plain C or C++, but definitely use Objective-C for interacting with your user interface.
+1 for being a legitimate answer to the question. Why was this down voted? He gave warning about Carbon...
I started with Carbon, but that was more than seven years ago. Times have changed: The Carbon app you write today may not work a year from now. Learning Carbon before Cocoa is simply a waste of your time. Objective-C is pretty good once you get used to it—just learn Cocoa. (No vote either way from me.)
@Alec Sloman: I agree the down votes are a bit harsh, but they are understandable. Carbon is deprecated so saying "you can use Carbon" is not really a legitimate answer.
This is the best answer because instead of saying just use Cocoa, he actually answers the question that was asked, which was "is it possible to program in C and use all the libraries and frameworks provided by Mac OS". He says, essentially, that in some cases yes, but it is not worth all the hassles. So why down vote a perfectly reasonable and intelligent answer?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.