PDA

View Full Version : Should I start with basic Mac Cocoa programming?




CaptSaltyJack
Jul 17, 2008, 02:53 PM
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



MrA
Jul 17, 2008, 07:02 PM
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.

CaptSaltyJack
Jul 17, 2008, 08:27 PM
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:

CaptSaltyJack
Jul 17, 2008, 08:50 PM
... 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.

MrA
Jul 17, 2008, 09:16 PM
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:

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.

detz
Jul 17, 2008, 09:33 PM
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-Programming-Mac-OS-3rd/dp/0321503619/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1216346346&sr=8-1

CaptSaltyJack
Jul 17, 2008, 09:44 PM
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-Programming-Mac-OS-3rd/dp/0321503619/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1216346346&sr=8-1

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.

grimreaper1377
Jul 17, 2008, 09:57 PM
Looking for the same path. At least the MVC pattern is not foreign to me since I was working on Rails before :cool:

OS X Dude
Jul 18, 2008, 11:23 AM
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

CaptSaltyJack
Jul 18, 2008, 11:31 AM
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:


- (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.

OS X Dude
Jul 18, 2008, 01:33 PM
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:


- (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.

So how many exceptions to standard C are there for use in Xcode, and is it mandatory to use them?

Thanks :D

CaptSaltyJack
Jul 18, 2008, 01:51 PM
So how many exceptions to standard C are there for use in Xcode, and is it mandatory to use them?

Thanks :D

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)

grimjim
Jul 18, 2008, 02:35 PM
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.

CaptSaltyJack
Jul 18, 2008, 02:41 PM
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.

+1

Immerse yourself in the Foundation classes. Learn them. Love them. ;)

OS X Dude
Jul 18, 2008, 03:05 PM
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 :)

CaptSaltyJack
Jul 18, 2008, 03:13 PM
Take the advice above.. buy this book: http://www.amazon.com/Cocoa-Programming-Mac-OS-3rd/dp/0321503619

If that doesn't dive deep enough into Obj-C for you, then also grab this: http://www.amazon.com/Programming-Objective-C-Developers-Library-Stephen/dp/0672325861

CaptSaltyJack
Jul 18, 2008, 03:15 PM
OK thanks, sorry to be like persistent but where would these exceptions be listed?

http://developer.apple.com/documentation/Cocoa/Reference/Foundation/ObjC_classic/index.html

and then, all Cocoa frameworks:
http://developer.apple.com/referencelibrary/API_Fundamentals/Cocoa-api-date.html

grimjim
Jul 18, 2008, 03:43 PM
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.
)

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...

CaptSaltyJack
Jul 18, 2008, 04:01 PM
Obj-C is C, but it still looks a bit different and requires some getting used to. For example... in C:


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?


@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 ;)

TonyHoyle
Jul 18, 2008, 04:15 PM
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:

@interface Dog : NSObject
{
@private NSString *name;
@private NSNumber age;
@private Owner *owner; // Owner class is defined elsewhere
}
@property (copy) Owner *owner
@end


...

@implemenation Dog
@synthesize owner
@end



(May have got some of the details wrong there.. I'm tired :p).

Limb
Jul 21, 2008, 10:34 AM
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-Programming-Mac-OS-3rd/dp/0321503619/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1216346346&sr=8-1

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

cis4life
Jul 21, 2008, 12:49 PM
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.

CaptSaltyJack
Jul 23, 2008, 01:15 PM
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...


In the Inspector, set its Object Class Name to Person. Add the keys for personName and expectedRaise, as shown in Figure 8.6.

Bind the Content Array of the array controller to the employees array of File's Owner, which is the instance of My Document (Figure 8.7).


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.

TonyHoyle
Jul 23, 2008, 07:55 PM
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.

ayasin
Jul 23, 2008, 11:38 PM
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.

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.

CaptSaltyJack
Jul 23, 2008, 11:44 PM
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.

*whoosh* right over my head.. :o

Man, I don't know how people are picking up a book and coding iPhone apps within a week...

ayasin
Jul 23, 2008, 11:53 PM
*whoosh* right over my head.. :o

Man, I don't know how people are picking up a book and coding iPhone apps within a week...

What other languages do you know? I might be able to explain it in the context of those languages so you get away from the Objective C confusion and can focus on the concept.

CaptSaltyJack
Jul 23, 2008, 11:56 PM
I know C, but just command line (no GUI). I know PHP. I also know C#.NET, but that's ridiculously quick and easy to build apps. I just wish Xcode/Cocoa was as "rapid app" as C#.NET :(

ayasin
Jul 24, 2008, 12:07 AM
I know C, but just command line (no GUI). I know PHP. I also know C#.NET, but that's ridiculously quick and easy to build apps. I just wish Xcode/Cocoa was as "rapid app" as C#.NET :(

Once you get used to it it's actually pretty fast. I find it much nicer than C# (and I've been doing .NET since beta including writing a bunch of stuff to dynamically generate IL). In C# say you have a class and one of the members of the class is an interface that you're going to call to do things for you. The interface is basically like what the "files owner" is in IB. Once you attach an object that implements that interface to the class, the class can do whatever it needs to with that object. That's what the framework does when you load the view (it attaches your implementing object to the interface). Just like with your class that has the interface, if you change the object that's implementing the interface you can have different things happen or different sets of data.

Hopefully that helped and didn't confuse you further ;). That's not exactly how it works in obj-c but it should give you the general concept. Anyway, I'm tired so I probably don't make enough sense right now :P

ooo
Jul 25, 2008, 07:07 PM
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.

I bought the book and stopped around the same point. It got a bit too complicated for me, like I could learn it and do copy and paste but I didn't think it would stick in my head. I decided to jump right into iphone sdk after that point, but that got me no where. Now I'm back to square one, waiting for the nda to lift and some books to show up.

I understand object c, but I feel like it doesn't really help. It's more about understanding how to use interface builder and linking it with the iphone sdk. I feel there are too many ways to something. It almost feels like php.

CaptSaltyJack
Jul 25, 2008, 11:51 PM
I gave chapter 8 another chance, another read-through. Made sure I had plenty of sleep. ;) I think I got it now. It's just one of those things you have to drill into your head and do it over again, and maybe draw a diagram of your classes and vars/methods, and figure out what's going on.

Heck, I'll probably go through chapters 7 and 8 again, as that's where things start to get a little complicated for me.

ooo
Jul 26, 2008, 06:26 AM
Now the problem is, I don't see how those chapters are useful in the iphone sdk. If you look at the iphone sdk, a lot of controllers and stuff you can do in cocoa are not available in the iphone sdk.

CaptSaltyJack
Jul 26, 2008, 10:28 AM
Now the problem is, I don't see how those chapters are useful in the iphone sdk. If you look at the iphone sdk, a lot of controllers and stuff you can do in cocoa are not available in the iphone sdk.

They're not useful for the iPhone.. the book is "Cocoa Programming for Mac OS X," after all.. not ".. for the iPhone."

DipDog3
Jul 26, 2008, 03:22 PM
I bought the book and stopped around the same point. It got a bit too complicated for me, like I could learn it and do copy and paste but I didn't think it would stick in my head. I decided to jump right into iphone sdk after that point, but that got me no where. Now I'm back to square one, waiting for the nda to lift and some books to show up.

I am kinda at the same point. I read the book, but it got to the point where I couldn't understand what it was talking about. The I started back with the examples and that didn't help, and now I am back to square one!

Stile
Jul 28, 2008, 02:21 PM
I'm pretty new to all this as well, but I believe all of the binding tools in IB for Mac applications is not available for iPhone applications. In fact the Bindings inspector is not even there. So a lot of chapter 8 would be unusable on the iPhone.

It took me awhile to figure out what was going on in IB and it's relationship to the code I was writing. It all seemed rather magical like VB, blahh! I also have been doing .Net programming since version 1 of the framework and when you build UI's in Visual Studio it would actually create/modify code for you. IB is not like this (which is actually quite fortunate, Visual Studio 2003 would sometimes completely screw up your code), IB appears to simply set properties for classes that get instantiated when your application is running. So you create a View in IB and change all those values in the inspectors, when your application runs a class is instantiated and all those properties you set are applied to the new object. If you want a custom class to be hooked up to your View in IB you simply created one and set the Class name in the appropriate inspector field. Same goes for the controller.

The First Responder has something to do with keyboard events or something like that, I've not really had to mess with that at all so I'm a little fuzzy about it's use. All the ctrl+dragging that occurs is simply linking variables for objects, such as dragging a controller onto a view to set the controller's view property. You can create variables in your classes that can reference controls in your interface. So if you have a text field that you want to manipulate in code you would create a variable in your view or controller class like this:

IBOutlet UITextField *textField;

Then from IB you would be able to ctrl+drag your view onto the text field.

A delegate is like a reference to a C# interface. Some delegates require a specific protocol be implemented. A Protocol is very similar to an interface definition in C# but not all of it's methods are always required. I think I read somewhere that delegates were created as a way of extending a class through composition as opposed to subclassing.

Like I said, I'm pretty new to this as well. Some of it still seems foreign to me but once you start getting used to it you'll see the patterns and it will start to make more sense. Hopefully I didn't confuse you even more :) Apple really has made some cool APIs though. I just started messing around with the CoreAnimation stuff, it's very cool.

avayan
Jul 29, 2008, 11:12 PM
Yup! I am not alone.

I am in fact impressed by the fact that most of you guys have exactly the same questions and impressions I have been suffering from the past two weeks. Hence, I will not ask the same questions again.

I already bought the Objective C Book. I have read 80% of it and I am to the point where I do not think it will help any more. So Now I read about the Cocoa for MAC OS X book, but OSX Cocoa is clearly not the same as Cocoa Touch (or Iphone stuff). I read mixed reviews on how useful the book will be. What the heck, I'll buy it anyway, first thing in the morning tomorrow.

So here is the question. I have seen video tutorials for the IPHONE dated to march 2008, when the SDK was first released but with no Interface Builder. Hence, it was all about quite a lot of typing like in the old MS Visual C days.

I feel very good at programming on VB6 and BCB environments, which I can see IB will not be like. So... Is there like a video training out there of the steps that I need to take in order to link an outlet (or whatever it is that needs linking) to a class (or whatever the first whatever is linked to)? I have read a lot of stuff here, but unfortunately I am a VISUAL guy (which must be why I do so well with the Visual Object Oriented stuff).

Whatever the case, I know that together we will figure it out. Thanks for your input.

Best regards,

Mega-NOOBIE as of 7/29/08