View Full Version : Cocoa with C++, instead of Objective-c?
bronxbomber92
2006.12.02, 01:17 PM
Is it possible to use Cocoa, instead of Objective-C? I prefer C++ much more, and I can't seem to wrap my head around "messages" and how to creat objects, and just the overall syntax Objective-C has added to C.
akb825
2006.12.02, 02:55 PM
Cocoa requires ObjectiveC, though with ObjectiveC++ you can do most of your code in C++, but the Cocoa parts in ObjectiveC. You could also look up Carbon, which is the root of Cocoa, depending on if everything you need was originally based on Carbon or not.
bronxbomber92
2006.12.02, 03:15 PM
Damn.... I guess I'll have to learn Objective-C :P I bought Aron Hillegasse's book hoping to learn Objective-C and cocoa, but it doesn't teach objective-c well :P
akb825
2006.12.02, 03:18 PM
There really isn't that much to ObjectiveC. (I learned it from Hillegasse's book as well) However, I am like you where I prefer C++, which is why I only use Cocoa for apps that I make that tie in to a standard OS X GUI. (and if the main part of it isn't tied in, I do as much as I can in C++)
bronxbomber92
2006.12.02, 03:32 PM
The syntax of Objective-c just seems very un organized and messy... Now, this Objective-C++, where can I read more about this?
akb825
2006.12.02, 03:37 PM
There's some good info as to the rules at Wikipedia. http://en.wikipedia.org/wiki/Objective-C#Objective-C.2B.2B
bronxbomber92
2006.12.02, 03:41 PM
Thanks, I think I'll take a look at this language! Do you know the file extension for it?
akb825
2006.12.02, 03:46 PM
It's .mm. It isn't really a new language, it's more of a way to use C++ alongside ObjectiveC.
bronxbomber92
2006.12.02, 03:48 PM
Oh, thanks a lot! So, assume everything in objective-c is the same as in objective-c++ (except the changes that were made from c -> c++)?
akb825
2006.12.02, 04:41 PM
ObjectiveC is the same, but you can use C++ constructs alongside ObjectiveC. (I assume you can use some extra things in C++ like for (int i = 0,...), too) You'll generally have an ObjectiveC class to hold your GUI elements etc., then use that to call C++ class's functions, and possibly send messages to your ObjectiveC class from your C++ class's functions.
OneSadCookie
2006.12.02, 06:36 PM
Objective C is a very pleasant, clean, expressive language -- in every way the polar opposite of C++. If you're put off by the square brackets and "keywords" before arguments, you're doing yourself a serious disservice. Just 'cos it doesn't look like C is no excuse to dismiss it out of hand!
unknown
2006.12.02, 07:55 PM
it you dont like objective C, you can use java.
akb825
2006.12.02, 08:10 PM
Or not. :p (I personally despise Java, though opinions do vary)
As OSC pointed out, ObjectiveC is quite different in philosophy than C++. Then again, being different doesn't necessarily mean better. ObjectiveC is more about being simple and small. C++ seems to be more about making all types equal (such as with operator overloading, which allows it to not rape primitives the way Java does) as well as having tools to make those types easier to use. (such as templates and references) I suppose a replacement for templates in ObjectiveC would be id, but I don't like it as much for 2 reasons: we get back to the raping of primitives, and though it's kind of nice to be able store anything, type checks can be very useful for catching bugs before they happen due to compiler errors. (besides, you always have the choice with void * in C++) I would have to say, sending any message to anything is pretty nice, though, since it makes receiving messages from UI elements very easy. Although we once again get to the compiler being less of a help. (it will throw warnings saying that a message can't be found, but I honestly get more warnings because one of my classes has a slightly different format but same name as another class', usually in the Cocoa library, or that it doesn't see a message that actually is there)
bronxbomber92
2006.12.02, 10:36 PM
I'm not saying I refuse to learn it, but I wanted to make sure there wasn't an easier (or familiar) option for me, instead of Obj-C.
How can Java be used with it? I know C# can use cocoa# also, but cocoa# isn't very far in development :(
unknown
2006.12.02, 10:59 PM
There is java libraries.
Just make a new cocoa java program in Xcode
OneSadCookie
2006.12.02, 11:25 PM
Cocoa/Java is deprecated; you shouldn't use it for new projects.
PyObjC and RubyCocoa are much better bets if you want Cocoa without ObjC.
Andrew
2006.12.06, 04:24 PM
It shouldn't take more than 2 hours to get a good handle on Objective-C, especially if you have Hillegass' book. Start reading the book from the very beginning, and actually write/run the code he shows you.
The most difficult thing for most people to grasp in Cocoa isn't the Objective-C language itself; it's the Cocoa API. The API is beautifully engineered, but it is very different from other APIs you might have seen.
As for Objective-C++, don't touch it with a 10 foot pole until you have a FIRM grasp of Objective-C. Its purpose is to allow you to mix C++ and Objective-C code. It is NOT an alternative to Objective-C.
FreakSoftware
2006.12.06, 10:08 PM
I learned Objective-C in an hour. It's ridiculously simple, and I quite agree with OSC that it's the antithesis of C++.
I also completely agree with everything Andrew just said.
bronxbomber92
2006.12.06, 10:18 PM
Yeah, last night I re-readover the first 100 pages of the book, and I know I feel much more comfortable with it. Before, when I first started reading "Cocoa Programming" I didn't understand how the methods worked, and declarations of classes and the allocation and init methods (why they were used) id foo = [[NSMutableArray alloc] init];
Overall, the difference between know and when I started reading the book, I have a better grasp of programming (and pointers and memory management in particular) then before.
Know the syntax, and basic concepts that I've read make sense now. And I must say, I do appreciate how Objective-C is "styled"!
Btw, as Andrew says its the cocoa API that is more difficult, I thought some parts of Cocoa were apart of the Objective-C language :P
slooksterpsv
2006.12.24, 02:28 PM
I've found that Objective-C/Objective-C++ combined with C++ is a great way to program. Objective-C is easier to program in, in my opinion. Here's just a simple class that extends NSObject - NOTE THIS IS NOT THE FULL CODE:
//... more code above
@interface Simple : NSObject
{
NSNumber num;
NSString *name;
}
-(void) setName:(NSString *)nme;
-(void) setNumber: (NSNumber)nmb;
-(NSString *) getName;
-(NSNumber) getNumber;
@end
@implementation Simple
-(void) setName:(NSString *)nme
{
nme = [nme copy];
[name release];
name = nme;
}
-(void) setNumber:(NSNumber)nmb
{
num = nmb;
}
-(NSString *) getName
{
return name;
}
-(NSNumber) getNumber
{
return num;
}
@end
int main()
{
Simple *smp;
smp = [[Simple alloc] init];
[smp setName:@"Your Name"];
NSLog(@"%@ is your name", [smp getName]);
[smp release];
return 0;
}
}
erwincoumans
2007.01.08, 05:09 PM
akb825, could you release the source code for your Monkey3D application, even if its work-in-progress?
That would be helpful for people new to MacOS X as learning resource.
Preferably under a liberal license, like Zlib, MIT or BSD.
Thanks,
Erwin
akb825
2007.01.08, 06:35 PM
I was planning on releasing the source under the BSD license once I do a bit more real-world testing. I'm pretty close to that testing, but I'll consider releasing the source earlier with a note that it's not completely tested yet.
Duane
2007.01.12, 05:00 PM
There is java libraries.
Just make a new cocoa java program in Xcode
why, out of interest? They certainly aren't portable.
vBulletin® v3.6.8, Copyright ©2000-2008, Jelsoft Enterprises Ltd.