Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

CaptSaltyJack

macrumors 6502
Original poster
Jun 28, 2007
351
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
 

MrA

macrumors newbie
Jul 13, 2008
9
0
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

macrumors 6502
Original poster
Jun 28, 2007
351
1
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

macrumors 6502
Original poster
Jun 28, 2007
351
1
... 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

macrumors newbie
Jul 13, 2008
9
0
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

macrumors 65816
Jun 29, 2007
1,051
0
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
 

CaptSaltyJack

macrumors 6502
Original poster
Jun 28, 2007
351
1
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

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

macrumors regular
Oct 20, 2007
218
0
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

macrumors 65816
Jun 30, 2007
1,128
611
UK
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

macrumors 6502
Original poster
Jun 28, 2007
351
1
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.
 

OS X Dude

macrumors 65816
Jun 30, 2007
1,128
611
UK
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.

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

Thanks :D
 

CaptSaltyJack

macrumors 6502
Original poster
Jun 28, 2007
351
1
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

macrumors member
May 24, 2003
75
0
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.
 

OS X Dude

macrumors 65816
Jun 30, 2007
1,128
611
UK
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 :)
 

grimjim

macrumors member
May 24, 2003
75
0
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

macrumors 6502
Original poster
Jun 28, 2007
351
1
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 ;)
 

TonyHoyle

macrumors 6502a
Sep 14, 2007
999
0
Manchester, UK
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

macrumors member
Aug 30, 2007
31
0
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

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

macrumors regular
Apr 4, 2008
211
62
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

macrumors 6502
Original poster
Jun 28, 2007
351
1
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

macrumors 6502a
Sep 14, 2007
999
0
Manchester, UK
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

macrumors 6502
Jun 26, 2008
318
0
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.