do you need to know C to learn objective C?

Discussion in 'Mac Programming' started by schmintan, Apr 17, 2007.

  1. schmintan macrumors regular

    Joined:
    Mar 1, 2007
    #1
    i know a bit of c# and vb ( from windows work) and i recently purchased a mac to get a different perspective on the computer world ( and they are pretty!). i want to give objective c a go for writing coca apps. do i need to know C or C++ to become ok at objective C?
     
  2. mduser63 macrumors 68040

    mduser63

    Joined:
    Nov 9, 2004
    Location:
    Salt Lake City, UT
    #2
    Nope, you don't. Get Programming in Objective-C by Stephen Kochan. He doesn't assume any prior knowledge of C at all.
     
  3. iSee macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    #3
    No you don't. In fact, some say it is harder for a programmer used to procedural programming (as typically used with C) to get used to object-oriented programming with a language like objective-C.

    The barrier that you might run in to is that some books on learning objective-C do assume you already know C or C++. Just avoid those books.

    A good one that assumes no prior knowledge of C is Programming in Objective-C: http://www.amazon.com/Programming-O...9611959?ie=UTF8&s=books&qid=1176820042&sr=8-1

    Also, Apple has several good articles on objective-c and cocoa. Lemmie see... here's one directly to the objective-c tutorial: http://developer.apple.com/document...veC/index.html#//apple_ref/doc/uid/TP30001163.
    And here's a link to a cocoa one: http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/index.html. Notice the "Related Reference" and "Related Documents" links on the left side; these are other useful, free links.
     
  4. whooleytoo macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #4
    Well, Obj-C is a strict superset of C, so if you become proficient in Obj-C you will have learnt C.

    But if you have any signficant previous coding experience you shouldn't have much trouble diving straight into Obj-C, if you have a good book at hand. Learning the language itself is relatively easy, it's learning what the Cocoa frameworks offer in features and shortcuts that will take time.
     
  5. Soulstorm macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #5
    Objective C does not require any knowledge of any language or progrmming concept.

    But, if you want to really know it all, after you have learnt Objective C, I recommend you take simple look at what plain C has to offer. Personally, I learnt C++ first and then moved to Objective C, and I see that I still need the low-level access C and C++ offer.

    My advice. Start with Programming In Objective C by Steven Kochan, as others have suggested, then take a book on C and see the low-level functions C offers.

    Personally, I couldn't have understood the memory management in Objective C if I didn't know about pointers in C and C++.
     
  6. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #6
    As whooleytoo pointed out, Objective-C is a superset of the C language so you can't avoid learning the syntax and programming constructs/flow control etc of the C language. Perhaps your question should be do I need to learn the C standard library to use the Cocoa framework? I don't think you do but some of the functions in the C standard library are very handy.

    b e n
     
  7. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #7
    lazydog is right, knowing how to program in straight procedural C will be very useful, even as a Cocoa programmer. There's still a lot you can't do directly from Cocoa, or can do more completely from C-based APIs, such as QuickTime, CoreAudio, CoreImage, CoreVideo, OpenGL, some Spotlight/metadata searching features, accessing FireWire and USB ports, custom UI, etc. You can do a lot from Cocoa, but not everything.
     
  8. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #8
    HiRez - for the things you mention that cannot be done in Obj-C, are you implying that when writing in Obj-C and one needs to do these things, one can drop into C (from within Obj-C) to do things things?

    If so, is this done by calling a C routine, or are there directives in Obj-C to drop into inline C? Or, is just a matter of coding the C within the Obj-C, since there are no Obj-C equalivalents?

    (Am I making sense?)

    Todd (I just ordered the Obj-C and Cocoa book dynamic duo pair mentioned above)
     
  9. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #9
    You just call C functions directly. Objective-C is implemented in C so you can use ALL of the C functionality directly. You don't need to use any special directives, bridges or anything like that.
     
  10. MacDonaldsd macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #10
    Thats interesting to know.

    Im quite proficient in C and its taking me a while to get my head around Cocoa/obj-c so your probably best going straight to obj-c :)
     
  11. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #11
    If you take C#, and forget everything that has anything to do with objects, the rest is reasonably similar to C. If you take Objective-C, and forget everything that has to do with objects, the rest _is_ C.

    If you know C#, then you know programming in general, you know object oriented programming, and you have some idea of C. That should be enough to learn Objective-C without problems.
     
  12. slooksterPSV macrumors 68030

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
    #12
    Personally it helped me, but I've always been interested in programming and that, so I knew what he was talking about in the book. The Steve Kochan one, but I related it to my first programming book, and really it was a breeze to read cause it flowed really smoothly and he makes it like you've never heard of the word programming at all. So I suggest reading it, and heck read over some of the chapters again to build a better foundation. He even includes a C tutorial part in the book so if you read some of that then followed from chapter 1 or 2; you'd be good. Now if you want to make Mac OS X GUI apps then you'll need to learn both Objective-C (Obj-C for short) and Cocoa (cocoa is really easy as well).
    Here's a simple program in Objective-C
    PHP:
    #import <Foundation/Foundation.h>

    int main()
    {
     
    NSString *hello = [[NSString allocinitWithString:@"Hello world this is a string"];
     
    NSLog(@"%@"hello);
     [
    hello free];
     return 
    0;
    }
     
  13. mduser63 macrumors 68040

    mduser63

    Joined:
    Nov 9, 2004
    Location:
    Salt Lake City, UT
    #13
    The message used to release objects is release, not free.
     
  14. slooksterPSV macrumors 68030

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
    #14
    Oops my bad, I know it's release, I have not a clue why I typed free
     
  15. jhande macrumors 6502

    Joined:
    Sep 20, 2006
    Location:
    Denmark
    #15
    Cuz the question was about C and you automatically dropped into malloc/free mode :D

    As someone who is in the process of going from C -> Objective-C, I must admit this is probably the most painless transitions I've yet expreienced. There is one caveat though: Since it's a strict superset of C it is waay too easy to drop into procedural mode and just bang away. I've done that a couple of times when trying things out.

    An excellent implementation, and Cocoa really is the bees knees compared to other libraries. But still having the standard library instantly accessible gives me a nice comfortable feeling :)
     
  16. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #16
    The only real knock I have on Obj-C is that while certain code snippets can be brief (thanks mostly to the many utility functions of the Cocoa library, like reading a file into a string in a single call), sometimes it can take a lot of lines of code and setup to perform a certain procedure, compared to more efficient languages such as Python and Ruby (these are especially strong at string parsing and collections manipulation). Some of the new features coming with Leopard should help (garbage collection, abbreviated iterator functions, etc.).
     
  17. whooleytoo macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #17
    Yeah, the string handling especially. I love the simplicity and clarity of languages such as Delphi in this area.

    Compare:

    Delphi
    Code:
    Fullname := Surname + ',' + Firstname;
    to Obj-C
    Code:
    [Fullname setString: [Surname stringByAppendingFormat:@",%@", Firstname]];
     
  18. Masna macrumors newbie

    Joined:
    Apr 20, 2007
    #18
    There's no need to supply NSLog with a brand new string, only to fill it with the hello instance. This makes more sense (considering NSLog takes an initial NSString instance argument):

    NSString *hello = [[NSString alloc] initWithString:mad:"Hello world!"];
    NSLog(hello);

    Cheers!
     
  19. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #19
    You could always write a category method :D

    Code:
    @interface NSObject (Free)
    - (void)free;
    @end
    
    @implementation NSObject (Free)
    - (void)free
    {
        [self release];
    }
    @end
     
  20. Soulstorm macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #20
    I can also go to the bathroom by making a stop at Colorado, first :)
     

Share This Page