Should I start with basic Mac Cocoa programming?

Discussion in 'iPhone/iPad Programming' started by CaptSaltyJack, Jul 17, 2008.

  1. macrumors 6502

    Joined:
    Jun 28, 2007
    #1
    I'd like to develop on the iPhone, but I'm starting at square one with Cocoa and Objective-C. Is it a good idea to start with writing apps for Mac OS first, or could I just as easily jump right into iPhone development?

    Also, what is a good book or online resource to learn about developing for the iPhone? (the classes, API, etc)

    Thanks
     
  2. MrA
    macrumors newbie

    Joined:
    Jul 13, 2008
    #2
    You can jump right into iPhone coding. As long as you know C or some other similar language, you'll be fine. I started last Friday, not knowing any Obj-C, and I'm done a significant (on-trivial) portion of my app.

    As for resources, all you need is the SDK. The documentation in there is pretty good.
     
  3. thread starter macrumors 6502

    Joined:
    Jun 28, 2007
    #3
    You knew nothing about Obj-C last week, and now you're coding stuff? Man. My brain must be slow. :) There's just so much foreign looking stuff, like @property, and delegates.. :confused:
     
  4. thread starter macrumors 6502

    Joined:
    Jun 28, 2007
    #4
    ... not to mention the Interface Builder itself. I've got MyAppDelegate, and RootViewController, what the heck are these? I drag stuff such as buttons into the window, but they auto-center and I can't move them around.
     
  5. MrA
    macrumors newbie

    Joined:
    Jul 13, 2008
    #5
    It may look weird and foreign, but it's all stuff you've probably seen before. It's not a hard language to pick up. It's also a "light" language so there isn't much stuff to remember.
     
  6. macrumors 65816

    Joined:
    Jun 29, 2007
    #6
    Go get this book, order it now...overnight it...you will learn everything you need to know. I never touched Objective C or anything Mac related until two weeks ago. I have a few Mac apps done and just about two iPhone apps done. This book is for the Mac but they are so similar and Aaron does a great job of walking you through everything. I really really recommend it!

    http://www.amazon.com/Cocoa-Program...bs_sr_1?ie=UTF8&s=books&qid=1216346346&sr=8-1
     
  7. thread starter macrumors 6502

    Joined:
    Jun 28, 2007
    #7
    Already have it. :) Still going through it, but sometimes it stumps me. Guess I just need to slow down a bit and re-re-read.
     
  8. macrumors regular

    Joined:
    Oct 20, 2007
    #8
    Looking for the same path. At least the MVC pattern is not foreign to me since I was working on Rails before :cool:
     
  9. macrumors 6502a

    OS X Dude

    Joined:
    Jun 30, 2007
    Location:
    UK
    #9
    i downloaded the PDF of Obj-C 2.0 on ADC's site - but it doesn't say how to display text or anything - just how the code works.

    Like in C, printf("Hello World");

    would show Hello World on the screen. Would it do that in Obj-C? Is like every instruction in Obj-C the same as in C?

    I'm a n00b at this :p
     
  10. thread starter macrumors 6502

    Joined:
    Jun 28, 2007
    #10
    No worries, I'm a newb too ;) To Obj-C anyway.

    All C funcs should work. That goes for printf() too. But if you're running Cocoa apps via Xcode, NSLog() is what you want. The interface for NSLog is:

    Code:
    - (void)NSLog:(NSString *format, ...);
    
    So as an example:

    NSLog(@"Hello World");

    This text will show up in the debugger. Note that strings in Objective-C usually use the NSString object, not (char *). So prepend your NSStrings with @. In other words, "blah" is a C string, @"blah" is an NSString.
     
  11. macrumors 6502a

    OS X Dude

    Joined:
    Jun 30, 2007
    Location:
    UK
    #11
    So how many exceptions to standard C are there for use in Xcode, and is it mandatory to use them?

    Thanks :D
     
  12. thread starter macrumors 6502

    Joined:
    Jun 28, 2007
    #12
    not sure how many exceptions there are to the standard C rules. You don't have to use them, no, but if you want to write Cocoa apps for Mac & iPhone, then yes, you must use the NS objects (NSString, NSTextField, etc)
     
  13. macrumors member

    Joined:
    May 24, 2003
    #13
    Objective-C is a subset of C, so things like printf still work. You can use printf to write a string out to the console. But it's probably best to get into the habit of using the Foundation Classes as quickly as possible. It'll make things much easier. For example, the NSString class has loads of useful methods for string handling, that would be a real pain to reproduce using C chars.
     
  14. thread starter macrumors 6502

    Joined:
    Jun 28, 2007
    #14
    +1

    Immerse yourself in the Foundation classes. Learn them. Love them. ;)
     
  15. macrumors 6502a

    OS X Dude

    Joined:
    Jun 30, 2007
    Location:
    UK
    #15
    OK thanks, sorry to be like persistent but where would these exceptions be listed?

    I think this is like the last hurdle before i finally understand where all the pieces of this puzzle are, and how to learn obj-c.


    Thanks again everyone :)
     
  16. thread starter macrumors 6502

    Joined:
    Jun 28, 2007
    #16
  17. thread starter macrumors 6502

    Joined:
    Jun 28, 2007
    #17
  18. macrumors member

    Joined:
    May 24, 2003
    #18
    To be honest, I think you're looking at this the wrong way. :)

    Objective-C is C with the object-oriented stuff from Smalltalk stapled on, more or less. A lot of what you will write will be the same C that you've been used to. Conditional logic, pointers, and so on, work the same way.
    But on top of the basic language there are dozens of Foundation Classes, which are pretty much the building blocks that Cocoa and Cocoa Touch are built from. To get very far, you need to get familiar with these, or at least be aware of their existence, and willing to snoop around in the documentation to find out what they do.

    Once you get the idea of programming in a "Cocoa-ish" way, any differences to traditional C programming will be irrelevant: you will have better tools at your disposal, and you will probably want to use them instead of labouring away with vanilla C. As far as exceptions go, I don't know that there are any. Objective-C is still C. But unless you start using the Cocoa classes, you'll be doing a lot of unnecessary work and getting extremely frustrated.

    Sorry if that sounds like a quasi religious rant on behalf of Cocoa-ism :)

    Join us... Join us...
     
  19. thread starter macrumors 6502

    Joined:
    Jun 28, 2007
    #19
    Obj-C is C, but it still looks a bit different and requires some getting used to. For example... in C:

    Code:
    class Dog
    {
      private string _name;
      private int _age;
      private Owner _owner;  // Owner class is defined elsewhere
    
      public owner
      {
        get { return this._owner; }
        set { this._owner = value; }
      }
    }
    
    Ok I cheated, that was C#. ;) Now, how would I do this in Obj-C? Like this?

    Code:
    @interface Dog
    
    class Dog : NSObject
    {
      NSString *name;
      NSNumber age;  //or that should be *age, I'm too much of a newb to know
    }
    - (Owner *)owner;  // or would it be - (id)owner; ?
    - (void)setOwner:(Owner *)newOwner;
    
    @end
    
    (in another file...)
    
    @implementation Dog
    
    - (Owner *)owner
    {
      // [owner autorelease]; ??
      return owner;
    }
    
    - (void)setOwner:(Owner *)newOwner
    {
      owner = newOwner;
    }
    
    @end
    
    Feel free to fix the above code at will ;)
     
  20. macrumors 6502a

    Joined:
    Sep 14, 2007
    Location:
    Manchester, UK
    #20
    The things that caught me out where the difference between objC 1.0 and 2.0, especially things like @synthesize which aren't mentioned at all in the objC books you can buy. Wikipedia has a list of differences http://en.wikipedia.org/wiki/Objective-C#Objective-C_2.0

    The whole dot notation being a synonym for valueForKey thing had me going crosseyed when I first saw it - 15 years of understanding how C structures works really gets in the way when you're trying to debug!

    I found http://www.iphonesdkarticles.com/ quite helpful (although don't read his examples in the article too closely, he makes errors in them that aren't in the downloaded code.. work it out yourself or use the downloaded example code as a crib sheet when you get lost.. I prefer to work it out myself as I remember it better then).

    So for the above example you get:
    (May have got some of the details wrong there.. I'm tired :p).
     
  21. macrumors member

    Joined:
    Aug 30, 2007
    #21
    Just wanted to say thanks for the book recommendation! I went out and bought it yesterday, and while I do have a good understanding of C++ and programming, I was stumped in figuring out Objective C and Cocoa. So far between this book and the apple documents, I'm easily picking it up. So for anyone else looking for something to help get into any apple programming, this is a great book to get.

    -Limb
     
  22. macrumors regular

    Joined:
    Apr 4, 2008
    #22
    I agree,

    I too picked up this book and along with the apple docs, I am now breezing through these topics.

    NOTED:
    (I'm currently a C, C++, and C# programmer and have been for MANY years) So a soild foundation in programming does help alot.
     
  23. thread starter macrumors 6502

    Joined:
    Jun 28, 2007
    #23
    UGH! Regarding that Cocoa book, chapter 8. What the heck?? He just breezes through building this app called RaiseMan, and doesn't explain anything. He throws out all these names like canRemove, arrangedObjects, etc., and doesn't explain them one bit.

    This is kind of frustrating. A few examples here...

    Say what? What's the reasoning behind doing this? The first paragraph makes sense mostly, it's associating the NSArrayController with the class "Person".. an array of people. Fine. But the second paragraph above.. what the heck? Up to this point, we've never even used the "File's Owner" object. There was never an explanation of what it is or how it works.

    He continues on in a similar fashion, and the chapter ends with no explanation of what any of this means. Keep in mind, I have experience in C and C#. I don't think I'm unable to grasp this stuff, I just think his teaching style isn't so hot, unless I'm missing something, or he explains all this in detail later in the book. If the rest of the book is just the "follow these steps blindly" kind of approach, I'd rather just sell/donate it.
     
  24. macrumors 6502a

    Joined:
    Sep 14, 2007
    Location:
    Manchester, UK
    #24
    AFAIK Files Owner is the representation of the source file that you're binding the resources to.

    I'm still getting my head around all the drawing lines between icons in the interface builder and what is required myself.. get it wrong and the app crashes silently, unfortunately. Objective C itself is relatively easy.. interface builder is a total git to use in my limited experience.
     
  25. macrumors 6502

    Joined:
    Jun 26, 2008
    #25
    Files Owner is a placeholder for the object that will control the view. It's associated with a particular class so you can access actions and outlets on that class, but it allows you to use different instances of the class for different views.
     

Share This Page