PDA

View Full Version : Learning Cocoa by Translating Code?




robbo91
Nov 18, 2010, 02:43 PM
Hey guys. I've studied computer science at uni for one year with Haskell for first semester and java for second semester. In my second semester class, our major assignment was a computer game in Java using Jcomponent, Actionlistener etc.

I've started looking into the C language and might move onto Objective C after that. I would like to one day translate the game I made into a Cocoa application. Could translating my existing code into Cocoa be a good learning experience or should I go a bit slower and learn other things first?



lloyddean
Nov 18, 2010, 03:21 PM
If it motivates you to actually want to do the port and you complete it it's a start and I'd say yes.

PatrickCocoa
Nov 18, 2010, 03:25 PM
Go slower and learn what Cocoa and Objective-C are all about first. Depending on how much time you have, this may be a couple hours a day for about a month.

Then take your existing code and try to translate it.

robbo91
Nov 18, 2010, 05:27 PM
I have about 3 months of spare time depending on how much part time work I get, so I can spend a couple of hours a day learning stuff for a month, then get into programming fun stuff like games later.

Thanks for the help guys.

BadWolf13
Nov 18, 2010, 06:24 PM
I'm with Patrick on this one. I am a recent Obj-c learner myself, having come from a background that includes Basic, C, C++ and Touring. The hardest thing for me with Cocoa & Obj-C was not the actual language, which is really just a matter of terms and context, but the way that Cocoa does things, which put everything I'd done before out the window.

mfram
Nov 19, 2010, 01:26 PM
I agree, the main problem with "translating" an existing app is not so much converting from Java to C/Obj-C. They are very similar syntactically. The bigger problem is that the design of Cocoa apps is very different than Java.

You can hack your way through it for simple apps. But eventually you'll realize a Cocoa app probably needs a different design in order to be implemented effectively.

whooleytoo
Nov 19, 2010, 02:01 PM
If I were you, I'd just pick a relatively simple project, and try to do it in Cocoa. For example - one I did myself when learning - a iTunes-like media player. You would need to break this down into the tasks needed:

- read a preferences file to get the location of your movies and song files.
- scan that folder, to get an array of files.
- Draw a list view, displaying that list of files.
- Add a movie view, to play the movies.
- Add play/pause/mute buttons, as necessary.

Between the Apple Cocoa documentation, and the online tutorials for each, you should have little time learning the above, if you're patient and take it step by step. In doing so, you'll learn lots of other things - using NSStrings, how to use outlets and actions, the MVC architecture etc.

ulbador
Nov 19, 2010, 02:39 PM
I wouldn't necessarily do this. While java and Objective C are similar in many respects, they are also hugely dissimilar in other respects. Everything in Java is an object, where with Objective C, this is not always a case. The whole concept of ActionListeners and such is handled in a completely different manner than in Objective C. Even the basics of laying out a form in Java are wildly (and THANKFULLY) different in Objective C.

As previous posters have mentioned, start with the basics and build up to the program instead of trying to directly replicate functionality. Even if you were porting the program and had experience in both languages already, you would probably still do things completely different because of how the languages differ.

PatrickCocoa
Nov 19, 2010, 04:12 PM
I'll just chime in again to agree with my earlier post. And to clarify somewhat.

You shouldn't try to port your current Java project into Objective-C/Cocoa AS A LEARNING PROJECT. As several others have said, particularly mfram, the design of projects is different in Objective-C/Cocoa. Once you spend a month or two on learning the basics with a smaller project or a set of tutorials, you can then take a whack at porting your Java project.

chown33
Nov 19, 2010, 06:12 PM
... Everything in Java is an object, ...

This is incorrect.

Java has primitive types as well as objects. The primitive types are: boolean, byte, char, short, int, long, float, double.

In many cases the primitive types can and will be automatically boxed and unboxed (converted to and from object types), but this can also fail in certain subtle cases, so it pays to be aware of it.

Java also has enum as a declarator for enumerated types.