Can't seem to grasp Objective-C

Discussion in 'Mac Programming' started by Awesomeness, Jul 30, 2010.

  1. Awesomeness macrumors member

    Feb 12, 2009
    The tutorial I used worked for me; I created an identical application from it, but I have gained nothing. The syntax is almost completely different from Java. I've been using Java for three years and Python the three before that, so I thought I could learn Objective-C. I can't seem to get a grasp on the stupid format of methods with the brackets and stuff. (Example would be '[slider setIntValue:numberToSetTo];') Random keywords have @s before them while others don't, and header files just confuse me. I don't get sent and received actions, outlets, or any of the connecting with the NIB stuff.

    It's just all so foreign to me... I've never had problems this bad learning any other programming languages... Is there anything like a book or tutorial that could explain all this to me?
  2. CMT macrumors regular


    Aug 24, 2009
    The first thing is to stop thinking it's stupid.

    Yes, it may be foreign but that's your point of view.
    For me, your language is foreign. is the best library to find everything you need.
  3. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    You definitely need to adjust your attitude. I wouldn't want to learn something I thought was stupid. If you want to learn it, come in excited. If you've used 2 languages, be excited to broaden your horizons. Message passing is different, that's good news.

    If you know C syntax, Objective-C is not hard to follow at all. The only things with @ keywords are for class declaration and exception handling (there are NSString literals, but that's different). Otherwise, fast enumeration and blocks are the only things syntactically different from C. That doesn't help much if you don't know C, but even so things are pretty consistent, and Java's basic flow control syntax, variable declaration, assignment, etc. are all just like C.

    A lot of your complaints are about Interface Builder. Stop trying to learn that and how IB-specific things work. Learn Objective-C first, then move onto the OS X GUI stuff. Your first Java app probably wasn't GUI-based.

    Programming In Objective-C 2.0 by Stephen Kochan teaches Objective-C proper without all the GUI mumbo-jumbo. Cocoa Programming for Mac OS X by Aaron Hillegass is a good follow-up on the GUI side, based on many others in this forum's experiences.

    Take a step back. Learn the basic syntax, there really isn't much to learn. Read the Apple docs, they are quite good. Pick a small CLI app you want to build, and work through it. I'm betting that it will quickly click.

  4. firewood macrumors 604

    Jul 29, 2003
    Silicon Valley
    Strange. For simple class use, I just never saw that much difference between:
    Obj C: [slider setIntValue:numberToSetTo];
    C++: slider.setValue(number);
    clean C: setSliderValue(slider, value);
    C: sliderPtr->value = (int)number;
    dirtier C: *((int *)((char *)sliderPtr + valueByteOffset)) = number;

    The difference comes when you want to extend the slider value to a 3D color projection from a remote database with a talking animation or something.
  5. PatrickCocoa macrumors 6502a

    Dec 2, 2008
    A few pointers . . .

    1. Thousands (millions?) of people use Objective-C every day, so it's possible to learn.

    2. People get imprinted with languages that they first learn or use for long periods of time. Once imprinted, your brain will have a hard time learning a new method (as you're experiencing).

    3. You'll probably have similar problems (and emotions) with some other aspects of the journey you're currently on:
    a) you'll be using Cocoa rather than Java APIs.
    b) you'll be using Xcode and Interface Builder rather than Eclipse or NetBeans or whatever.
    c) you'll be using OS X rather than Windows or Linux (unless you used OS X while you were doing your Java).
    In all three cases above, you'll experience moments of frustration and rage (been there) as things that are easy and obvious in Java/Eclipse/Windows are impossible and opaque in Objective-C/Xcode/OS X.

    Look upon this as a learning adventure, as you break the ruts in your mind and expand your horizons, and recognize and celebrate the difficulties as you encounter them.

    Or, if it makes you feel better, smirk at the poor Objective-C guy as he struggles through Java.
  6. cult hero macrumors 6502a

    cult hero

    Jun 6, 2005
    I tried picking up Objective-C a few years ago and basically had the reaction of, "What kind of a moron can like this syntax!"

    See, basically, I was kind of a snob and thought that "real" languages used the C/C++ syntax and the portions of Objective-C that are OOP seem to use a VERY different syntax which was derived from Smalltalk. I hated it.

    However, for the last two years I've been doing a lot of Ruby programming which was the first language of a different syntax that I liked. Ruby really did a good job of opening my mind about syntax and broke me of the stupid idea that there was "one true syntax" or whatever. Also, as I casually studied Objective-C I started to see the differences between it and C++ and why I liked it better (primarily because I love Ruby so much).

    Although I'm currently reading through Steven Kochan's Objective-C 2.0 I got this book ( as a gift from a friend. I read the 70 or so pages that introduce Objective-C and they give an EXCELLENT historical background on the language and explain WHY the language is the way it is. It gave me a much deeper appreciation for why the conventions are the way they are. Although Kochan's book is very in depth, the linked book's history is well worth the read for giving historical context.

    Objective-C is a very neat and very flexible language. They syntax is weird for someone coming from a Java or C++ background, but in my later years of programming I've found that extra verbosity is certainly not the worst thing in the world. It is different, but that's no necessarily a bad thing.
  7. macsmurf macrumors 65816


    Aug 3, 2007
    Obj C isn't really a difficult language to learn. The difficulty comes in the fact that it is a strict superset of C. I can't really think of any sane reason why that is but it results in the language not being as "clean" as Java.

    If you just want to check out the language you can follow the iphone programming course on iTunes U from Stanford. There are some platform specific things but it gives you a good introdution to the language. Otherwise buy a book.

    Also there is MacRuby if you don't want to bother with Obj C but it's not available for iPhone due to the Apple restrictions on the platform.
  8. xStep macrumors 68000

    Jan 28, 2003
    Less lost in L.A.
    OP, there are many threads here regarding learning Cocoa programming. All have great advice and discussion. The question is asked in some form at least once a week.

    To get kick started, perhaps look at the tutorials at CocoaDevCentral. If you like the authors writing, he has recently released a book a long the same line.

    Perhaps this Objective-C overview video is for you;

    Also look at the two "Objective-C 2.0 Intro" videos. These have a unique graphical presentation that may be helpful.
  9. Crick Riddle macrumors newbie

    Jul 31, 2010
    I've been having the same problem. First I bought a book on iPhone app programming. In the introduction it said "...of course we're assuming that you already have a working knowledge of Objective C". So off to the book store I went to get a book on Objective C, but of course I've never studied C programming before, so that didn't make sense. It just seems like I'm endlessly going backward before I can start going forward. This has been going on for over a year now. Frustrating! :apple:
  10. xStep macrumors 68000

    Jan 28, 2003
    Less lost in L.A.
    Just found this YouTube character. He has a series about Objective-C programming.
    Look to the right where his topics are and click on "Objective-C Tutorials".
  11. foshizzle macrumors regular

    Oct 17, 2007
    You'll appreciate Objective-C once you learn C, then C++ and have to suffer through the terrible object orientation of C that C++ is.

    Having learned both this summer (C++ at school, Obj. C at work, C last semester) I can handily say that Objective-C does such a better job at taking an incredible language, C, and making it object-oriented.

    Read Kochan's book. Maybe learn C first. You'll like Objective-C.

    Best this to know: it isn't loosely typed like Ruby (which I love) and you don't have to dot everything like java, instead you use brackets which seems more intuitive now.
  12. Sydde macrumors 68020


    Aug 17, 2009
    Velvet Green
    Well, there is also @synchronize, which does not fit your description. It would seem that the @ character is used as a macro marker, and any good teaching text (including a tutorial) should at least summarily explain what underlies it.

    C is not a difficult language per se, but it helps tremendously to understand how much the programmer has to do that other languages handle automagically.
  13. SaltyPiratePony macrumors newbie

    Oct 10, 2009
    Thank you gentlemen for your great advice. I've been eyeing Objective-C for a while now, but never really got the guts to take it on. Helpful threads like these really make me want to dive into it. After I plough through 700ish pages of "z/OS Basics" (IBM Redbooks series), I'm gonna try to pick up a copy of the book that Lee recommended!

    P.S. Not wanting to sound like a total queer but: Lee, your advice is concise & to the point as always. I really appreciate your replies (and other's too, of course) in the programming subforum.
  14. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    Glad to help, where I can. Now the test is to see if Awesomeness, whose last activity is about 25 minutes after starting this thread, ever comes back to read it. =)


    Edit: Also, to Sydde: while it looks like I failed to provide an exhaustive list, the point was that @-prefixed keywords were pretty finite and easy to keep track of. Certainly when most of us are writing Objective-C code, we're not thinking "damn, is it int or @int? for or @for? It's totally random!".
  15. chown33 macrumors 604

    Aug 9, 2009
    descending into the Maelström
    The algorithm is quite simple: is it a reserved word in C? If the answer is yes, then it's NOT preceded by @. If the answer is no, then it is.

    The rationale goes to history. It is desirable for every valid C program (in source form) to also be an identical valid Objective-C program. Since C does not reserve words like "synthesize" or "synchronize", it is entirely possible for those words to appear in valid C programs as functions, variables, typedefs, etc. To disambiguate the use of new keywords, it is necessary to identify them in some way as being keywords, and not a C function, variable, typedef, etc. Hence the leading @.

    The reason for choosing @ again goes to history. It is almost the only character on a typical computer keyboard which does not already have a meaning in C. Also, it's illegal as part of a C name, so it can't be present in in valid C program as part of a function, variable, typedef, etc. (Not entirely by coincidence, a similar thing happened in Java, where @ was used to add annotations in Java 5.)

    So as already mentioned before in this thread, to understand why Objective-C is the way it is, look at its history.
  16. cubist macrumors 68020

    Jul 4, 2002
    Muncie, Indiana
    Both Objective-C and C++ are attempts to extend C to support object-oriented programming, and both were originally implemented as translaters. Therefore, they both have suboptimal and quirky syntax. They were not designed from the get-go as OOP languages as Java was.

    Personally, I use Objective-C only for the user and system interface mechanisms, which are very powerful and convenient; the main logic of the programs I write in straight C. Objective-C does have its own string-manipulation functions, but they seem too much trouble to do anything complicated in. Of course, I have over 20 years experience programming in C, so ymmv.

    I also like to point out that C++/ObjC code produces a lot of indirection, whereas straight C produces machine code that's much more efficient. It's trendy to say performance doesn't matter, but in my world, if it isn't quick, it's dead.
  17. Sander macrumors 6502

    Apr 24, 2008
    And I'd like to point out that C++ does not inherently produce a lot of indirection. Remember, a valid C program is also a valid C++ program. You can "fake" OO programming in C, but I don't think the resulting code would be much more efficient than what the C++ compiler would have generated for you.
  18. Cromulent macrumors 603


    Oct 2, 2006
    The Land of Hope and Glory
    Without being overly pedantic that is not strictly true.
  19. Sander macrumors 6502

    Apr 24, 2008
    Are you sure you're not being overly pedantic? :D
  20. Thomas Harte macrumors 6502

    Nov 30, 2005
    I once had problems in real life with some swanky C99 code that used macros with a variable number of arguments.
  21. SaltyPiratePony macrumors newbie

    Oct 10, 2009
    Yeah, but honestly, this is the first thread I subscribed to. The speed topics are created by and afterwards get lost into oblivion on MR is insane really. But I've never been a hardcore forum user anyway. I -99,998% of the time- enter from the highlights listed on the MR homepage. ;)
  22. Cromulent macrumors 603


    Oct 2, 2006
    The Land of Hope and Glory
    Haha, I meant "At the risk of being...". Doh! Early morning brain fart there.
  23. Sander macrumors 6502

    Apr 24, 2008
    Ah, but C++ split off from C before C99. If you had code using Variable Length Arrays, that would have caused the C++ compiler to choke, too.

    The new C++0x standard tries to close the gap a little bit again. Both languages seem to be "cross-pollinating" a bit.

Share This Page