To program for iOS, you need to speak to iOS. Everything you say to iOS will be in
accordance with the iOS API. (An API, for application programming interface, is a list
or specification of things you are allowed to say when communicating.) Therefore, you
will need some knowledge of the C programming language, for two reasons:
-
Most of the iOS API involves the Objective-C language, and most of your iOS
programming will be in the Objective-C language. Objective-C is a superset of C.
This means that Objective-C presupposes C; everything that is true of C trickles up
to Objective-C. A common mistake is to forget that “Objective-C is C” and to ne‐
glect a basic understanding of C.
-
Some of the iOS API involves C rather than Objective-C. Even in Objective-C code,
you often need to use C data structures and C function calls. For example, a rectangle
is represented as a CGRect, which is a C struct, and to create a CGRect from four
numbers you call CGRectMake, which is a C function. The iOS API documentation
will very often show you C expressions and expect you to understand them.
The best way to learn C is to read The C Programming Language (PTR Prentice Hall, 1988) by Brian W. Kernighan and Dennis M. Ritchie, commonly called K&R (Ritchie was the creator of C). It is one of the best computer books ever written: brief, dense, and stunningly precise and clear. K&R is so important for effective iOS programming that I keep a physical copy beside me at all times while coding, and I recommend that you do the same. Another useful manual is The C Book, by Mike Banahan, Declan Brady, and Mark Doran, available online at http://publications.gbdirect.co.uk/c_book/.
It would be impossible for me to describe all of C in a single chapter. C is not a large or difficult language, but it has some tricky corners and can be extremely subtle, powerful, and low-level. Moreover, since C is described fully and correctly in the manuals I’ve just mentioned, it would be a mistake for me to repeat what they can tell you better than I. So this chapter is not a technical manual of C.
You don’t have to know all about C, though, in order to use Objective-C effectively; so my purpose in this chapter is to outline those aspects of C that are important for you to understand at the outset, before you start using Objective-C for iOS programming. This chapter is “Just Enough C” to start you off comfortably and safely.
If you know no C at all, I suggest that, as an accompaniment to this chapter, you also read select parts of K&R (think of this as “C: The Good Parts Version”). Here’s my proposed K&R syllabus:
• Quickly skim K&R Chapter 1, the tutorial.
• Carefully read K&R Chapters 2 through 4.
• Read the first three sections of K&R Chapter 5 on pointers and arrays. You don’t need to read the rest of Chapter 5 because you won’t typically be doing any pointer arithmetic, but you do need to understand clearly what a pointer is, as Objective- C is all about objects, and every reference to an object is a pointer; you’ll be seeing and using that * character constantly.
• Read also the first section of K&R Chapter 6, on structures (structs); as a beginner, you probably won’t define any structs, but you will use them quite a lot, so you’ll need to know the notation (for example, as I’ve already said, a CGRect is a struct).
• Glance over K&R Appendix B, which covers the standard library, because you may find yourself making certain standard library calls, such as the mathematical func‐ tions; forgetting that the library exists is a typical beginner mistake.
No comments:
Post a Comment