C and Objective-C relationships

Discussion in 'iOS Programming' started by nashyo, Jan 13, 2013.

  1. macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #1
    I started my App development career learning Objective-C, having no background in programming whatsoever. It has been 12 months since I started learning code and I am now curious about learning another language.

    A friend mentioned that learning C would enhance my understand of Objective-C, so I got my hands on this book.

    http://www.amazon.co.uk/C-Programming-Language-2nd/dp/0131103628

    I have progressed 33% of the way through this book to find that the similarities are so similar that I might as well not bother reading the rest of it.

    It would be good to get a thread going on other developers thoughts on C, in the context of how it might benefit an Objective-C developer of iOS Apps.
     
  2. macrumors 68030

    Joined:
    Feb 13, 2012
    Location:
    Perth, Western Australia
    #2
    Objective-C is an add on to C.

    All the non-object oriented Obj-C code you write (including, likely much of the code in your methods) is plain C. It's not just similar - it IS C.


    Plain C will be useful to you if you ever do any low level work with hardware, or write code for another platform, possibly where Objective-C does not exist.
     
  3. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #3
    K&R may not have been the best choice of books. Just my humble opinion.

    You might have been better served by one of the phone book sized books that provides tons of example of more specific things.

    B
     
  4. thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #4
    What would you have suggested as an alternative?
     
  5. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #5
    I edited my post. I can't think of a specific example right now with my pre-coffee brain, but K&R is a thin book where a lot of the actual work is left as an exercise to the user. Thus, if you already know some programming of any kind it doesn't really add anything to your arsenal beyond the language reference. You do it the way you already know it.

    B
     
  6. thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #6
    Good point @balamW

    I suppose it might be worth learning Python or something completely different, to enhance my skills as a 'problem solver'
     
  7. macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #7
    I wouldn't forgo that entire book... I did the same thing as you, learning Obj-C before C, except I knew Obj-C was an extension of C and so I thought I knew all there was to know about C.

    But it turns out that on account of learning Obj-C first, I didn't learn about some of the plain old C ways of doing things. For example:

    A struct is basically a class full of instance variables and no methods. Do you know how to use them? NSRange and NSRect are examples of structs that Apple uses that you might have used before.

    Do you know how to use a c-array? They look like this
    Code:
    int array[3];
    array[0] = 2;
    array[1] = 3;
    array[2] = 10;
    printf("%i", array[1]);
    Can you use a function pointer?

    Do you know how to allocate memory?

    Can you use switch statements?

    I would just look through this and read any chapters that you don't think you understand just so you can make sure you know all there is to know about C.

    http://c.learncodethehardway.org/book/

    Also note that ALL VALID C CODE IS ALSO VALID OBJ-C CODE.

    If you know how to do something in C and Obj-C, and doing it the C way won't take significantly more time or lines to write, do it the C way. Your code will be more portable (more platforms understand C than Obj-C,) and chances are it'll execute faster.

    Other languages you can learn:
    C++
    java

    C++ is mostly just an extension of C, although occasionally the authors decided to take out features of C that they felt were insecure. Honestly, I don't like C++... I feel like the main new features that it has and other languages don't just lead to more confusing code. IE, they allow you to redefine operators in the language. So typing

    Code:
    "string1" + "string2"
    Is valid. What does it result in? "string1string2"? Okay, fine, but what about

    Code:
    "string1" - "string2"
    I don't think it's valid, but it could be depending on how the string class is written, and lord knows what they decided to make the minus operator do.

    Java is inspired by C++ but they managed to weed out a lot of the stupid ideas from C++... unfortunately, they didn't get all of them, because a terrible concept introduced in C++, known as exceptions, still exists in Java (and I actually think Java's standard classes utilizes them more.) The basic concept of an exception is that, rather than have errors when you try to compile buggy code, or return error codes, it's much better to crash or slow down the application. The person who introduced the concept in C++ is on my time traveling hit list.
     
  8. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #8
    Everyone learns things differently.

    I'd suggest a C-based book on a specific topic that is either related to what you are working on or in a completely different area. (games? databases? ...)

    Learning new languages is great, but doesn't always translate to putting new arrows in the quiver.

    B
     
  9. ChristianJapan, Jan 13, 2013
    Last edited: Jan 13, 2013

    macrumors demi-god

    ChristianJapan

    Joined:
    May 10, 2010
    Location:
    日本
    #9
    How about SQL to be able to use databases ? Of course there are libs around but some solid SQL is good in many cases. Things like stored procedures, trigger ...

    Maybe PHP for server-side stuff.
    And JavaScript also can be handy to know, too.

    If you can Objective-C the classical C is too close and concepts like ARC are great compared to nasty old malloc/free.
     
  10. thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #10
    Good point. I want to explore animations and user interface design from a coding perspective, so I think I will read in those areas.

    @ArtOfWarfare from what you have said here I feel pretty confident that I know enough about C.

    ----------

    Ye, I've just figured out how to set up APNS using SQL and PHP.

    Will explore that in more detail.

    ----------

    Easyapns.com was really helpful
     
  11. macrumors regular

    Joined:
    Aug 23, 2011
    #11
    I disagree. Unless you're working on a separate subproject for which portability is a must, you might as well stick with Objective-C and the safety and other benefits it has to offer - all or nothing. For example, I can't recall where I saw it, but I recently saw an article from someone making the point that iOS developers who are using "int" instead of "NSInteger" are probably doing fine now, but might run into easily-avoidable bugs once Apple's ARM hardware goes 64-bit. Also, Objective-C has much more pleasant ways to sort and iterate over stuff than pure C. If your project is already using Objective-C, then why not just stick with it, own it, and milk it for all it's worth?

    I'm also not totally convinced that C will be that much faster for most purposes than Objective-C once compiled, particularly since Apple has done so much work to make sure that Objective-C is a first-class player in Clang/LLVC.

    That being said, this is a matter of personal choice, so don't take my word for it. Go with what works best for you.

    Interesting to see such hate for exceptions. They of course exist in Obj-C as well… do they bother you there as well?

    In my case, I learned C in college, then promptly forgot about it after changing from a CS to an English Lit major (one of the worst decisions in my life, but I'm slowly recovering). When I decided I wanted to earnestly pick up iOS dev, I had to relearn a lot of stuff, but in retrospect it wasn't as tough as I thought it would be - just practice enough, and it becomes second nature, just like with anything else. All that being said, if I have the choice, I'd still rather write in Objective-C than straight C, as much of a n00b as that might make me.
     
  12. macrumors 65816

    ctdonath

    Joined:
    Mar 11, 2009
    #12
    Thing is, the language designer doesn't know what shouldn't be allowed/facilitated. You might not see the sensibility of it, but it may be reasonable in a particular application; I can see some application defining operator-() such that "string1"-"string" returns "1", and "string1"-"string2" returns "string1" (to wit, remove every instance of rvalue from lvalue).

    Sure, it makes it easier to do stupid/confusing things, but balanced by making it much easier to do smart/sensible things. Powerful tools are dangerous when wielded by the confused. If a language is going to allow unrestricted manipulation of pointers, then risk of redefining the subtraction operator is minor.

    ----------

    The real value in reading "K&R C" is historical: that is the book that introduced and defined C to the world. As a competent programmer it will tell you little you don't already know, but as a student of the totality of programming it will help understand why things are what they are because that book is where that major branch of the discipline started. It's a bit like learning Latin: not so much for daily use, as comprehension of origins of dialects.
     
  13. macrumors 603

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #13
    A good programmer usually knows more than one programming language, and learning the C language core of the Objective C superset can give a coder far greater understanding into how their device's processor actually runs stuff, how to debug their apps in more detail, and the possibility of some options for improving the performance of their code.

    K&R is a great historical document. It doesn't include some of the newer C99 (and later) constructs used in modern coding, but can give you great insight into the core of the C language that everything else (including Objective C) was built on top of.
     
  14. macrumors regular

    Joined:
    Aug 23, 2011
    #14
    Or you could create a function named something like remove_string_from_string() and not confuse the next developer who looks at that code (who might be you, in a few months after you've forgotten that you've overloaded the hyphen operator in that project).

    Just because you can do things like overload the basic arithmetic operators doesn't mean you should, even in the languages that allow you to do so.
     
  15. macrumors 65816

    ctdonath

    Joined:
    Mar 11, 2009
    #15
    That's not the issue. I'm not saying you should; I was trying to (on short notice) suggest a case where it might be sensible, as just because we can't think of a good reason now doesn't mean one doesn't exist.

    The issue is whether the language should let you. Some people seem to think that, just because they can't think of a good use, the option shouldn't be available to anyone.
     
  16. macrumors regular

    Joined:
    Aug 23, 2011
    #16
    Coincidentally, an iBooks book was recently released on this topic: All the C You Need to Know (the "you," implicitly, being an Objective-C developer), by Bill Dudney. The price looks a bit high relative to its length, though it does take advantage of the iBooks platform with some interactivity tricks and such. Has anyone given this a look yet and can give a quick review?
     
  17. thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #17
    looks pretty good. might get it
     

Share This Page