Learning Objective-c without knowing C - objective-c

I'm planning to learn Objective C. Most of the books say that the reader should know C as a starting point.
Is it necessary to know C before diving in the Objective-C ocean ? I'm a ruby programmer.

I personally think you should be fine diving in without C. Yes, Objective-C is a superset of C, but that doesn't that all Objective-C coding will require C knowledge. The situation is similar for C++. I say if you have a firm knowledge of at least one programming language and know the basics of C's constructs and syntax you should be fine to start the other stuff. You don't need to be able to write strong C applications before you get started with Obj-C, but it will help you a lot once you can.
If you find you need to do something that requires deeper C-knowledge, then you can hit the C manual a bit harder, but it probably isn't necessary until you get to that point. You should be able to develop your experience of the two languages pretty simultaneously instead of having to learn C for a month before you can even touch Objective-C. :D
Here's what the Objective-C books want you to know about C:
typing system. Pointers, arrays, structs
functions.
general design and organization of programs.
Since you're a ruby programmer, most of this should come fairly easily. The biggest shock is going to be moving from a high-level dynamic language to a low-level static language, so be prepared for some big changes, buts its nothing you won't be able to handle.

From Wikipedia -
Objective-C is a reflective, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Objective-C is a thin layer on top of C, and moreover is a strict superset of C;
Yes , you should have knowledge of C.

As #arjan points out it's a superset of C. You might be able to work your way though examples and such without prior C experience, however to become productive you will eventually have to understand plain C. For examples many APIs such as SQLite access are only C (not ObjC/Cocoa). You really have to grasp concepts such as pointers, C-style strings etc.

You can't program in Objective-C without knowing C, as Objective-C is simply a superset of C.

I don't think it is absolutely necessary, I learnt Objective C with no knowledge of any other languages. However, saying that, it is very useful if you know C and a bit easier.

Nah. As long as you know what functions are, variables, objects, then you'll pick up "C" syntax by programming in objective C. To be more specific, I don't think you need to go and "learn C" first. Rather, I'd keep a C reference handy and look up anything you don't understand there.

Related

Learning C for Objective-C

I am relatively proficient in Objective-C but I have been looking around some frameworks and libraries I might use in the future and I am increasingly seeing the use of C. So far the only applications I have written contain only Objective-C. I know Objective-C is a superset of C, but what I mean when I say that I have only written in Objective-C is that I have only used Objective-C methods and the syntax of Objective-C that is distinctly different from C syntax. I've been going through questions related to the relationship between C and Objective-C (see links below) and I want to start learning C, but apparently there are three types of C (K&R, C89, and C99), and I am wondering which type I should learn to help me with Objective-C. I know from learning Objective-C I unknowingly learned C too, but I want to understand the ins and outs of C more and become familiar with its functions, syntax, features, etc. Also, is Objective-C based off of any one of the three types of C?
There are even more types of C. In the meantime C0X and C11 were defined... They all make only small evolutionary steps from their predecessors, so you shouldn't worry much about it. Objective-C is based on C99 (minus floating point pragmas, actually), so for now that would probably be the best fit.
It's not entirely clear from your question, but you do notice that those variations of C are just evolving specifications from different years? K&R from ca. 1978, C89 from 1989, C99 from 1999 etc... Objective-C was designed to be a strict superset of C, so you can probably expect Objective-C to incorporate C11 features some day.
(NB: several edits to include information from the comments)
I suggest you look at some introductory texts on C, and learn whatever C dialect the text teaches. This will likely be C89 or C99.
K&R C is missing modern features such as "prototypes" (declaring the number and types of arguments to your functions). C89 adds all that stuff in.
C99 has, mostly, small incremental features that are useful in specific domains. The bulk of the stuff you need to learn should be identical across C89 and C99. Fun fact: Microsoft never bothered to implement C99.
Actually, I will suggest a specific introductory text: use the Second Edition of K&R. This basically updates K&R to C89. K&R is an outstanding book, and a great choice for learning the C language.
https://stackoverflow.com/questions/1646667/kr-1st-edition-vs-2nd-edition

How to leverage the *C* in Objective-C?

I've been doing iPhone development full-time over the last couple of months.
Having previous experience only in OOP, I've relied heavily on Apple's APIs, which eventhough majestic I must reckon, led me to write code that in retrospective looks like a weird mix of ruby/smalltalk with added boilerplate.
So I'm thinking I'm missing out, and ordering K&R. Yet I wonder how what I'll learn, can specifically be put to good use in iOS work, without fighting against the devices' API and the whole OO pardadigm ?
The lower level APIs are largley in straight C, perhaps you'd like to explore them? For example Quartz 2D has a C API.
Objective-C is a superset of C. You can intermingle Objective-C and C function calls, statements, etc.
So for example, if you prefer Posix sockets to NSSocketPort, you can just mix in the C socket functions wherever you want. Or, if you wanted to keep it a little more object oriented, you could wrap C statements in an Objective-C function call that would isolate your C code from the rest of your Objective-C code.
Something that might help you understand the relationship between C and Objective-C would be to create an Objective-C wrapper for some C code. I’d recommend writing a simple iPhone app scripted in Lua. That should be enough of a project to really look at the C.
Not learning C is not missing out on anything.
I almost agree with #jeff except that I don't see why so many beginning Objective C programmers go straight for the iPhone stuff when they could be running a Mac app with much less hassle and convert it later.
But yes, buying a copy of K&R would be a very good idea (as well as getting a copy of the C90 or C99 standards, if you can afford them). In fact, I have a book on iOS games that suggests going straight to vanilla C for a lot of the low-level stuff to cut out the messaging overhead for performance's sake. Essentially, you're not doing anything you're not "supposed" to do by using C functions; the C library is still there to be used, so you aren't doing anything particularly strange or kludgy.

Creating a Lua plugin with Objective C Knowledge

I want to build a plugin for an application(TeamSpeak), however I have no lua knowledge. Is it possible to somehow wrap or bridge my Obj-C knowledge to lua?
If needed I could learn the basic syntax of lua to get my plugin started, but ultimately I want the meat of the program in Obj-C.
Any advice appreciated!
Lua's api written in c so rather than going from obj-c to Lua, you will have to go from obj-c to c to Lua. This is not as bad as it sounds though, obj-c and c play well together so you can have functions to expose to Lua in your .m source files and/or use .c source files in your project. As far as I know there isn't a luabind type library for obj-c but if you are using c++ with your obj-c this may also be an option.
Once you get past the stack manipulation, you will probably find the api pretty intuitive. The c api for tables can be a little confusing at first but it is also extremely powerful and allows you to expose your application's functions in many interesting ways.
Lua is a fairly simple, yet extremely powerful language. For example, there is only one datastructure: tables. Unless you are trying to do complex things with metatables you can probably dive right in with little trouble coming from Objective C. Do note that array indices (i.e. tables with sequential numerical keys) are 1-based instead of 0-based. Take a look at Programming in Lua (PiL) to get started.
Search for iPhone Wax on Google. Might not be what you need, but might help your needs. It lets you call Obj-C via Lua.

Moving from C to Objective-C?

I was wondering what I needed to know in terms of C to move onto Objective-C. I would be uber-grateful if you could let me know what you think is sufficient to move on Objective-C.
Thanks,
~Daniel
If you're looking to learn C before moving on to Objective-C (which, in my opinion, is a great idea), I would recommend to you Steve Kochan's 'Programming in Objective-C 2.0'. Very similar to his 'Programming in C' book, it will contain all the C you're going to need to use Objective-C, and it's a great primer for learning both languages.
If you're not looking to read a book, though, or you already have one, I would assume that after learning pointers, you will have covered most of the important parts of the C language (of course, it will depend on the order of material in the book, but pointers are usually sufficiently advanced to move on). Good luck!
You need a book. "Programming in Objective-C 2.0" is a fairly good one, and there are a few others. It's not a bad idea to have two or three.
You need to understand basic pointer concepts. C or C++ is the way to go for that.
You need to understand basic object-oriented concepts. Objective-C is a poor platform on which to learn them, so you may want to first learn basic OO concepts on C++ or Java first.
Once you get started with Objective-C, you need to learn the screwy syntax, of course, and all about the UI concepts, but, more importantly, you need to really, REALLY understand and "internalize" the Objective-C heap management scheme. Java gives you no hint of this, C++ is a poor background, Qt is a hair better, but still pretty far off the mark, so pretty much you have to pick it up as you learn Objective-C.
Yes, we have a lot of kids here that have learned how to take an Objective-C example, modify a few lines, and produce an "app", without actually understanding what they are doing. But learning that way they're never going to be able to produce anything of consequence. You need to INVEST in learning, not simply "pick it up as you go".
Personally I would suggest you learn C++ so that you can master the concept of polymorphism, which is pretty important in objective C and iPhone programming. Since you already know C I think that this would be better than jumping from C to objective C.

Mixing procedural and Object oriented programming

So, a long long time ago I used to program procedurally initially with Basic on my first BBC Model B and then a small amount of Pascal at university as well as assembly along the way.
Then along came OO which seemed much more sensible all round and that was all I used with C++, Perl, Java, Ruby.....
Coming back to programming non web stuff after a break of some 8 years or so on the iPhone I've found Objective C really easy to pick up but every now and again have to drop out of Objective C and back to plain old C for low level stuff and that's where it's back to procedural programming again.
What I'm after is a good guide about how to mix Object Orientated and procedural styles, how to do memory management, whether to wrap the procedural parts in object etc. etc.
Also a good basic guide to good procedural style for C would be helpful as I don't even seem to be able to think in procedural terms any more.
My recommendation would be to use object oriented programming as the highest level of organization and procedural programming for implementation. OO is best at defining interfaces, contracts between components, partitioning code into coherent chunks, etc.
When programmers first start learning about objects, there's a tendency to go nuts with objects, making everything in sight an object when a simple built-in type would do. Every number becomes a class, then methods proliferate on the classes, just in case they're needed. It can become a form of procrastination, delaying the point at which you finally get down to writing code that does some work.
If it helps, just think of your procedures/functions as static class methods. The scope of your memory references is a little different, but not much. If you limit your functions to only accessing data passed as parameters, it's even easier as you don't have to figure out the symbols.
EDIT: I should clarify that I'm talking in a general way here. I've not done any iPhone development so I can speak to specifics with regard to memory access/management there.
As far as I know, object oriented programming is essentially to keep data and actions on this data tied together.
C++ and Ojective-C provide already baked mechanisms to help you, but you can still manage to do it by yourself.
Many C libraries just bind a structure pointer as the first parameter of OO methods.
You may be able to find more information around this by googling "C object oriented programming".
You don't have to think in a procedural way. Wrap all low level things into classes. You can use objective-c++.