Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
I'm having trouble with the real concept of objects... to me from my past programming experience, an object is either a type also known as a struct, or it is a GUI object like a button or whatever. I can make these things work but I'm just entering in the code he says to put in, I don't know what's going on when it comes to classes instances objects and all that.

Objective-C objects are not that different than your experience (if I understand your experience properly). They aren't really structs (like in C++), but I suspect most of your difficulty is really that of nomenclature. Is your experience C++? Java?

Pretty much everything you run into in the book is one or more objects. Each object has an "interface" (in C++ this is equivalent to a class declaration) and an "implementation" (in C++ this is equivalent to a class definition).

In objective-C you send "messages" to objects like [object messagename:contents_of_message]. In C++ or similar languages you do the same thing by calling functions: object->messagename(contents_of_message). In c++ you have static class functions, whereas in objective-C you prefix the message declaration/definition with a "+" to accomplish largely the same thing.

There are some important differences (C++ checks to see if the "message" is part of the "class" at compile-time, objective-C allows/requires you to explicitly call the parent-class's version of overloaded functions with [super message], etc.), but most of the concepts are pretty much the same. Mostly it's the names of things that change.

(Note: before people jump all over me, yes, I know I oversimplified much of what i just said).
 
first, oversimplifying is great. I tend to have trouble understanding things :confused:
I have a strong BASIC background, that's the only language I've done anything serious in. Not classic BASIC though, but advanced BASIC with GUI. So like I said, in the BASIC's I'm familiar with, an object is simply anything you put on the screen. A button, a label, a graphic square, an image, etc.. And to get them to "talk" to each other they would have unique ID's, 1..2..3... etc. and you would use functions, also known as subroutines, that would be called on at the appropriate time. Also recently I worked on C so that I could use the good tutorials and books that require you to know some C. I can't write a program in C but it's plenty enough to get by.

Just for future reference, I'm bad at explaining things too.
so what is an object, exactly? Is it the "methods" inside a class? Is it another name for a class? Is it a GUI object?
I also get confused because in IB you use an object to "link" a class to your UI. Is THAT the mysterious object?

I would appreciate any help... I've gone through a lot of tutorials and books looking for the secret but I'm lost in this area.
 
An object is a collection of state with behaviors. A struct with associated methods, essentially. A class is a blueprint for creating objects (it describes what state and behavior objects of that type have), and objects of a particular class are known as instances of that class.
 
So a class is what defines an object, and the actual object is what I add to my UI in IB?

an object may or may not correspond to something on the screen. An object is merely an instance of a class. It may or may not have an on-screen representation.

I'll simplify again. A class might be, for example, "dog." The class "dog" has some member variables, like "name," and "color." In objective C, it might look like:

@interface dog
{
NSString *name;
UIColor *color;
}
@end

@implementation dog
- (void)setName:(NSString *)n
{
name=n;
}
-(NSString *)name
{
return name;
}
@end

Individual instance of "dog" are "objects." For example, I might create a new dog, and name it Spot and color it black. I might add functions to dog to teach it how to draw on the screen, etc.

Buttons and such are also objects. They have member variables (like "state" and "enabled") and functions (like setState and getEnabled).

It gets funkier when you create a class such as "pet," then "subclass" or "inherit" from that class to create classes like "dog."
 
I also get confused because in IB you use an object to "link" a class to your UI. Is THAT the mysterious object?

I would appreciate any help... I've gone through a lot of tutorials and books looking for the secret but I'm lost in this area.

just to answer this ...

in IB, the gui element (button, for example) is an object. you "link" it to an instance (or an object) of a particular type (class); essentially all you are doing is telling the button "when someone presses you, please send a particular message to this particular object i have created."

One of the problems with this book is that he really should (IMHO) teach you the hard way of doing things (manually instantiating your controllers and telling the button that your controller is the delegate) before teaching you how to drag and drop and draw connections between things; it's a lot easier to understand what is happening when you have to manually do stuff like:

mycontroller *controller=[[mycontroller alloc] init];
[button setDelegate:controller];

At least I think so. And once you see all the work you have to do to set up all the connections, then you can see how much IB helps you and really appreciate and understand what is going on beneath the covers.
 
thank you so much guys I think I'm finally seeing the light.
So....
An object is... an object like you said. It's just that the Object I'm tihnking of is a CUSTOM object.
a class is like NSView, NSObject, etc. or it's a custom one that I create and "link" to my custom object.
 
back of book

-
 

Attachments

  • Picture 2.jpg
    Picture 2.jpg
    134.9 KB · Views: 82
Just for future reference, I'm bad at explaining things too.
so what is an object, exactly? Is it the "methods" inside a class? Is it another name for a class? Is it a GUI object?
I also get confused because in IB you use an object to "link" a class to your UI. Is THAT the mysterious object?

I would appreciate any help... I've gone through a lot of tutorials and books looking for the secret but I'm lost in this area.

Huge difference between object oriented programming and what you're used to. Basic is not at all object oriented, even though new versions have limited object support to control the GUI. It is only incidently that the GUI elements are objects. Object oriented programing is about the design of the program and the data it works with. But you're in luck, objective C is a good langauge to learn it in, and IMHO much easier than C++ or Java. Check out this book before you dive into Cocca and confuse yourself. Once your about halfway through it (should only take a couple days) then start the Cocca stuff.

http://www.amazon.com/Programming-Objective-C-Developers-Library-Stephen/dp/0672325861

I started to write about objects in simple terms, but it is much easier to follow an example. The above book teaches object design without using any of the OS/X frameworks-just simple objective c. Once you learn that (and it is not hard) following what is going on with the GUI and the frameworks will make lots of sense.
 
As an Amazon Associate, MacRumors earns a commission from qualifying purchases made through links in this post.
dealloc question

I've been working through the book and have run into something I'm confused about. (Wasn't sure if I should post to this thread or start a new one...)

Anyway in chapter 8 there's this bit of code in MyDocument.m:

Code:
- (id)init
{
    [super init];
	employees = [[NSMutableArray alloc] init];
    return self;
}

- (void)dealloc 
{
	[self setEmployees:nil];
	[super dealloc];
}

- (void)setEmployees:(NSMutableArray *)a
{
	if (a == employees)
		return;
	[a retain];
	[employees release];
	employees = a;
}

And I'm wondering about using [self setEmployees:nil] in the dealloc method. In the past it's always been [objectName release] in the dealloc, so if I do [self setEmployees:nil] instead, don't I still have a retain count of 1? Or does setting an object to nil automatically decrease its retain count?

Thanks!
 
I'm a Java programmer, I just began reading this book tonight. Although I've run into an error, I don't understand how to write this code. I can read it; but I'm unsure how to write it. Any tips or suggestions?

EDIT: Chapter 3 is starting to make things clearer....
 
I've been working through the book and have run into something I'm confused about. (Wasn't sure if I should post to this thread or start a new one...)

Anyway in chapter 8 there's this bit of code in MyDocument.m:

Code:
- (id)init
{
    [super init];
	employees = [[NSMutableArray alloc] init];
    return self;
}

- (void)dealloc 
{
	[self setEmployees:nil];
	[super dealloc];
}

- (void)setEmployees:(NSMutableArray *)a
{
	if (a == employees)
		return;
	[a retain];
	[employees release];
	employees = a;
}

And I'm wondering about using [self setEmployees:nil] in the dealloc method. In the past it's always been [objectName release] in the dealloc, so if I do [self setEmployees:nil] instead, don't I still have a retain count of 1? Or does setting an object to nil automatically decrease its retain count?

Thanks!

If memory serves, That example uses garbage collector neutral code. I mean that the code will work with or without the autorelease and garbage collector support. the NSDocument object has that array as a property and thus the need to set it to nil in order to tell garbage collector to clean it up when it gets to that object in memory. Then again, the [super] object may be holding a autorelease pool and so setting it's collection to nil allows that pool to see that [self] is no longer being used and can autorelease it accordingly...
 
I've been working through the book and have run into something I'm confused about. (Wasn't sure if I should post to this thread or start a new one...)

Anyway in chapter 8 there's this bit of code in MyDocument.m:

Code:
- (id)init
{
    [super init];
	employees = [[NSMutableArray alloc] init];
    return self;
}

- (void)dealloc 
{
	[self setEmployees:nil];
	[super dealloc];
}

- (void)setEmployees:(NSMutableArray *)a
{
	if (a == employees)
		return;
	[a retain];
	[employees release];
	employees = a;
}

And I'm wondering about using [self setEmployees:nil] in the dealloc method. In the past it's always been [objectName release] in the dealloc, so if I do [self setEmployees:nil] instead, don't I still have a retain count of 1? Or does setting an object to nil automatically decrease its retain count?

Thanks!

Look at setEmployees:... It releases the old value, and retains the new one. [nil retain] is a no-op, so it works.
 
Question for those who have already gone through the book:
I have been reading an Objective C book for a couple weeks and am getting used to the syntax. I understand the language but I haven't started reading about the frameworks yet. I have a good background in C an C++. My question is should I finish my other book and then read this one, or can I stop reading the other and just start this one. My goal is to be able to make iphone apps, and maybe some for the mac. Thanks for the advice.
 
Question for those who have already gone through the book:
I have been reading an Objective C book for a couple weeks and am getting used to the syntax. I understand the language but I haven't started reading about the frameworks yet. I have a good background in C an C++. My question is should I finish my other book and then read this one, or can I stop reading the other and just start this one. My goal is to be able to make iphone apps, and maybe some for the mac. Thanks for the advice.

This book seems more geared towards the programmer with none to some experience and possibly haven't seen Obj-C before. I think you will be fine as I read through it without any issues and I come from a C++/Java/C# background.
 
Question for those who have already gone through the book:
I have been reading an Objective C book for a couple weeks and am getting used to the syntax. I understand the language but I haven't started reading about the frameworks yet. I have a good background in C an C++. My question is should I finish my other book and then read this one, or can I stop reading the other and just start this one. My goal is to be able to make iphone apps, and maybe some for the mac. Thanks for the advice.

This book doesn't get into iPhone specific stuff, but it'll give you a really good grasp on the Mac OS X frameworks. Once you understand that, the transition into iPhone stuff should be easy enough since the iPhone frameworks (UIKit, etc...) are based on AppKit.

Also, the objective-c syntax style is something that I was only able to learn by actually getting into Xcode and tapping out some code. Objective-c will change your understanding of object oriented coding. It has also made me despise working in C++.
 
I'm a Java programmer, I just began reading this book tonight. Although I've run into an error, I don't understand how to write this code. I can read it; but I'm unsure how to write it. Any tips or suggestions?

EDIT: Chapter 3 is starting to make things clearer....

interesting, i'm also on Chapter 3 and i'm just all "W H A T ??"... :eek:

i'm not a java programmer though, only some average experience with web development and scripting... and i only read one Objective-C book before i started this - so it may not all have sunk in yet.
 
did amazon give me the shaft?

so this is a book that certainly will be referenced for years to come... i'm just wondering if anyone else has sub par binding on their new book? or if this is normal... for um... developer books? :confused: i'm not *terribly* picky about books, i'm just concerned this is going to fall apart by the end of the year.
 

Attachments

  • IMG_0033.JPG
    IMG_0033.JPG
    73.7 KB · Views: 64
  • IMG_0032.JPG
    IMG_0032.JPG
    46.8 KB · Views: 66
so this is a book that certainly will be referenced for years to come... i'm just wondering if anyone else has sub par binding on their new book?

You're not alone... I've had mine for a month and the binding has broken in three places, and pages are falling out.

Not sure if I should try getting amazon to replace it, or the publisher. (Amazon might be more helpful, at least until *everyone* starts returning them... dunno about the publisher; I can't really see Addison-Wesley caring to replace them.)
 
You're not alone... I've had mine for a month and the binding has broken in three places, and pages are falling out.

Not sure if I should try getting amazon to replace it, or the publisher. (Amazon might be more helpful, at least until *everyone* starts returning them... dunno about the publisher; I can't really see Addison-Wesley caring to replace them.)

it was actually pretty simple to request a replacement from amazon on their website... but the replacement didn't arrive yet so i'm concerned they didn't receive my request...
 
strange... i just received an email from amazon.

"Please accept our sincere apologies for this situation. Normally, in such circumstances we create a replacement order and ship it to you at no additional charge. Unfortunately, this item is now unavailable from our supplier and we cannot send a replacement order. Please return the item at our expense. We will refund you in full, including associated shipping costs, upon receipt of the return."

i wonder if all the books have defects and Addison-Wesley are doing a reprint? by the way, my book is now REALLY falling apart, so i wouldn't be surprised if they are doing a reprint.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.