Re: Newbie Object Sharing Question

2009-01-31 Thread Richard Somers

On Jan 30, 2009, at 12:46PM, Brian Slick wrote:

It starts to occur to me that I don't actually want an instance, I  
want the real deal.


On Jan 31, 2009, at 9:31AM, Brian Slick wrote:

It may be a nonsensical statement here (good to know), but in the 3D  
CAD world that I'm used to, which I have been leveraging in my  
attempts to understand OOP, there is a distinction between instances  
and the source item.



A working knowledge of 3D CAD programs does not translate directly  
into the learning of OOP concepts. In a 3D CAD assembly you could have  
a master object and multiple instances of that object. In OOP you have  
classes, instances, and pointers. Not exactly the same thing. I would  
recommend you forget about 3D CAD for a while and concentrate on the  
fundamentals of C and Objective-C. Best of luck.


Richard

___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Re: Newbie Object Sharing Question

2009-01-31 Thread Ken Thomases

On Jan 31, 2009, at 10:31 AM, Brian Slick wrote:

And I believe that I do understand the difference between objects  
and pointers (I could be mistaken).  The part I'm missing is what  
this needs to look like in code.  I'm afraid I need some handholding  
here.  If I knew what I needed to do in order for there to be only a  
single data object referenced from multiple places, I wouldn't have  
asked the question in the first place.


OK, so I recommend that you read a good introductory book on the C  
language, followed by Apple's documentation on Objective-C (and maybe  
a third-party book on that, too), then the Cocoa Fundamentals Guide.



Well, at the very simplest, here's a trivial example:

NSString* foo = [[NSString alloc] initWithUTF8String:"Here is some  
text"];

NSString* bar = foo;

Now, we have two pointers, foo and bar, which both refer to the same  
object, a string whose content is "Here is some text".  The first  
statement allocated and initialized the string object, and assigned  
the pointer foo to refer to it.  The second line assigned the pointer  
bar to refer to the same object.  The assignment copied the pointer,  
but did not copy the object.


Let's suppose we create a custom class declared like so:

@interface ThingAMaBob : NSObject
{
NSString* incantation;
}
-(id)initWithMagicalIncantation:(NSString*)anIncantation;
@end

and defined like so:

@implementation ThingAMaBob
-(id)initWithMagicalIncantation:(NSString*)anIncantation
{
if (self = [super init])
{
incantation = [anIncantation retain];
}
return self;
}

-(void)dealloc
{
[incantation release];
[super dealloc];
}
@end

We might have code in some other part of our program which goes like  
this:


NSString* magic = @"Abracadabra!";
	ThingAMaBob* thing1 = [[ThingAMaBob alloc]  
initWithMagicalIncantation:magic];


Now, magic contains a pointer to a string object.  We're creating a  
new ThingAMaBob instance and we've passed a reference to that same  
string object into the initializer.  Within the initializer there's  
the parameter anIncantation which is a pointer which receives that  
passed-in reference.  We invoke the -retain message on that string  
object, which returns a reference to that same string object back, and  
we assign the instance variable "incantation" to point to it.  So, now  
we have three pointers (magic, anIncantation, incantation) which all  
refer to the same object.


After the initializer returns, there are just two because  
anIncantation is no longer in scope.  But it's still the case that  
"magic" and the "incantation" instance variable of our new ThingAMaBob  
refer to the same string object.


If we then proceed to do:

	ThingAMaBob* thing2 = [[ThingAMaBob alloc]  
initWithMagicalIncantation:magic];


then much the same thing happens.  After that line executes, the  
string object is referred to by "magic", the "incantation" instance  
variable of the ThingAMaBob instance pointed to by thing1, and the  
"incantation" instance variable of the ThingAMaBob instance pointed to  
by thing2.  So, thing1 and thing2 are sharing a reference to a single  
string object (here I used a bit of shorthand; I referred to the  
objects pointed to by the two pointers by just mentioning the names of  
the pointers).



A singleton implementation is _one_ solution to the issue you're  
having, but it's not necessary, and not even recommended for  
something as simple as this.


Can you expand upon why not?  Based on the blog post I linked, it  
sounds like this is exactly the situation in which to do this.


Well, in your original post you already pointed out that it seemed  
much more complicated than necessary.  Since you can achieve what you  
need by simply passing references to an object around, creating a  
singleton class is overkill.


It is sometimes appropriate to create a singleton, but it can be  
somewhat limiting.  If you start out writing a program using a  
singleton class, and later realize you need more than one instance of  
the class, you will discover that you need to rewrite a bunch of code.



One stumbling block is that you have two view controllers, but  
evidently no central application controller.  View controllers  
should have logic and state specific to a view and that view's  
relationship to the model.  An application controller manages your  
application overall.  It manages the other controllers, including  
your view controllers.  It has the primary responsibility for  
managing the application-global (as opposed to, for example,  
document-specific) model, and for providing access to that model to  
other parts of the program.


Well, I just used the Tab Bar Application template, and other than  
the AppDelegate that doesn't contain very much, there is no provided  
application controller.  The need to create one was not immediately  
obvious to me, as none of the examples in the book I'm reading

Re: Newbie Object Sharing Question

2009-01-31 Thread Brian Slick


On Jan 30, 2009, at 9:47 PM, Ken Thomases wrote:



On Jan 30, 2009, at 2:46 PM, Brian Slick wrote:

It starts to occur to me that I don't actually want an instance, I  
want the real deal.


That statement is nonsensical.  There is no "real deal".  An  
instance is real.  It's not some pale reflection of something else.


You do want an instance.

I suspect that perhaps you don't understand the difference between  
an object and a pointer to an object.  The reason I suspect that is  
because the simple solution to your dilemma, the thing you're not  
seeing, is that you want a single instance referenced from multiple  
places using multiple pointers.


It may be a nonsensical statement here (good to know), but in the 3D  
CAD world that I'm used to, which I have been leveraging in my  
attempts to understand OOP, there is a distinction between instances  
and the source item.


And I believe that I do understand the difference between objects and  
pointers (I could be mistaken).  The part I'm missing is what this  
needs to look like in code.  I'm afraid I need some handholding here.   
If I knew what I needed to do in order for there to be only a single  
data object referenced from multiple places, I wouldn't have asked the  
question in the first place.


I read through some documentation about model objects and objects  
in general, and stumbled upon the concept of Singletons.  Some  
additional searching lead me to this blog post:

http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html
...which seems to describe exactly what I want.  I reconfigured  
MyListItemArray as a singleton, and remapped my data source  
methods accordingly, and everything seemingly works perfectly.   
Items added in one view are displayed in the other, and so on.  It  
works so well that I have to assume there is a catch.


I can't shake the feeling that this seems more difficult than in  
ought to be, and generally when I feel that way there tends to be  
a single line of code solution that I haven't found yet.  Are  
singletons really the only way (that doesn't involve saving to a  
file, I suppose) to share a model object across multiple view  
controllers?  I think I'm missing something really fundamental.


Yes, I think your are missing something fundamental -- the ability  
to share references to an object simply by assigning multiple  
pointers to point to the same thing.


Yes, exactly.  How do I do that?

A singleton implementation is _one_ solution to the issue you're  
having, but it's not necessary, and not even recommended for  
something as simple as this.


Can you expand upon why not?  Based on the blog post I linked, it  
sounds like this is exactly the situation in which to do this.


One stumbling block is that you have two view controllers, but  
evidently no central application controller.  View controllers  
should have logic and state specific to a view and that view's  
relationship to the model.  An application controller manages your  
application overall.  It manages the other controllers, including  
your view controllers.  It has the primary responsibility for  
managing the application-global (as opposed to, for example,  
document-specific) model, and for providing access to that model to  
other parts of the program.


Well, I just used the Tab Bar Application template, and other than the  
AppDelegate that doesn't contain very much, there is no provided  
application controller.  The need to create one was not immediately  
obvious to me, as none of the examples in the book I'm reading through  
have dealt with accessing the same data set in different tabs.


I also do not yet see how this solves my problem.  I need to see some  
code.  From my view controllers, I don't believe that I know how to  
send messages to the AppDelegate (say, for the table data source  
methods) without creating an instance, which I believe brings me back  
around to the same problem I started with.


If your application has one central list of items, then it would be  
the application controller's job to create and load that model.  It  
would hold references to the model objects in instance variables.   
It would provide access to those model objects to other parts of the  
program.  One way would be to pass the references in to the view  
controllers when those view controllers are initialized.  Another  
way would be for the application controller to expose those  
references through properties and have the view controllers  
reference those properties -- the view controllers would need a  
reference to the application controller to do that.  Again, they  
could get such a reference by being passed it, or if your  
application controller is your application delegate, they can obtain  
the reference using [NSApp delegate].


Regards,
Ken


Thank you for the explanation, but I'm afraid I still have no idea  
what I specifically need to do, or even what help topic to go search  
on for addit

Re: Newbie Object Sharing Question

2009-01-30 Thread Joseph Crawford
Sorry I misunderstood the question then :)  Just a newbie trying to  
lend a hand where I can.  Thanks for correcting me.


Joseph Crawford

On Jan 30, 2009, at 9:47 PM, Ken Thomases wrote:


On Jan 30, 2009, at 1:51 PM, Joseph Crawford wrote:

Why don't you create a base controller that those 2 controllers  
subclass, that way they would inherit the functionality of reading  
those properties, also they would inherit the methods for getting,  
setting, etc.


BaseController
-- Controller 1
-- Controller 2

in Base controller define those properties and any methods for  
interacting with them
in Controller 1 and Controller 2 have your Class specific stuff  
there, because they both inherit from BaseController you would get  
those properties.


This is completely irrelevant to the original question.  Having a  
common base class does not make two objects share state.




On Jan 30, 2009, at 2:46 PM, Brian Slick wrote:


I want both view controllers to reference MyListItemArray.


You want both view controllers to reference _an instance_ of  
MyListItemArray.  MyListItemArray is a class.



So in each one I do something like:
MyListItemArray *listArray = [[MyListItemArray alloc] init];


As you seem to have figured out, this is creating (allocating and  
initializing) a new instance of the class.  This newly-created  
object is separate and distinct from other objects in your program,  
even ones created by execution of this same line of code at other  
times.



[...]  It starts to sink in that what I have done is create a  
*local* instance of MyListItemArray in each view controller, and  
what's local to ViewControllerTabA has nothing to do with what is  
local to ViewControllerTabB.  I'm getting local copies of the  
array, not the source array.


As near as I can tell, there is no "source" array, nor is there any  
copying going on.


You have explicitly created two separate arrays from the get-go.   
You have then filled them out separately to have similar initial  
contents.



It starts to occur to me that I don't actually want an instance, I  
want the real deal.


That statement is nonsensical.  There is no "real deal".  An  
instance is real.  It's not some pale reflection of something else.


You do want an instance.

I suspect that perhaps you don't understand the difference between  
an object and a pointer to an object.  The reason I suspect that is  
because the simple solution to your dilemma, the thing you're not  
seeing, is that you want a single instance referenced from multiple  
places using multiple pointers.



I read through some documentation about model objects and objects  
in general, and stumbled upon the concept of Singletons.  Some  
additional searching lead me to this blog post:

http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html
...which seems to describe exactly what I want.  I reconfigured  
MyListItemArray as a singleton, and remapped my data source  
methods accordingly, and everything seemingly works perfectly.   
Items added in one view are displayed in the other, and so on.  It  
works so well that I have to assume there is a catch.


I can't shake the feeling that this seems more difficult than in  
ought to be, and generally when I feel that way there tends to be  
a single line of code solution that I haven't found yet.  Are  
singletons really the only way (that doesn't involve saving to a  
file, I suppose) to share a model object across multiple view  
controllers?  I think I'm missing something really fundamental.


Yes, I think your are missing something fundamental -- the ability  
to share references to an object simply by assigning multiple  
pointers to point to the same thing.


A singleton implementation is _one_ solution to the issue you're  
having, but it's not necessary, and not even recommended for  
something as simple as this.


One stumbling block is that you have two view controllers, but  
evidently no central application controller.  View controllers  
should have logic and state specific to a view and that view's  
relationship to the model.  An application controller manages your  
application overall.  It manages the other controllers, including  
your view controllers.  It has the primary responsibility for  
managing the application-global (as opposed to, for example,  
document-specific) model, and for providing access to that model to  
other parts of the program.


Since you were concentrating on the view controllers, you thought  
you had to create the instance of MyListItemArray in each view  
controller.  It follows that you got one such instance for each view  
controller.


You should have a single application controller object (an instance  
of some custom class).  This application controller is often also  
the application delegate.  It is also often instantiated in the main  
nib, where the "delegate" outlet of the main application object is  
connected to it.


If your application has one central li

Re: Newbie Object Sharing Question

2009-01-30 Thread Ken Thomases

On Jan 30, 2009, at 1:51 PM, Joseph Crawford wrote:

Why don't you create a base controller that those 2 controllers  
subclass, that way they would inherit the functionality of reading  
those properties, also they would inherit the methods for getting,  
setting, etc.


BaseController
-- Controller 1
-- Controller 2

in Base controller define those properties and any methods for  
interacting with them
in Controller 1 and Controller 2 have your Class specific stuff  
there, because they both inherit from BaseController you would get  
those properties.


This is completely irrelevant to the original question.  Having a  
common base class does not make two objects share state.




On Jan 30, 2009, at 2:46 PM, Brian Slick wrote:


I want both view controllers to reference MyListItemArray.


You want both view controllers to reference _an instance_ of  
MyListItemArray.  MyListItemArray is a class.



 So in each one I do something like:
MyListItemArray *listArray = [[MyListItemArray alloc] init];


As you seem to have figured out, this is creating (allocating and  
initializing) a new instance of the class.  This newly-created object  
is separate and distinct from other objects in your program, even ones  
created by execution of this same line of code at other times.



[...]  It starts to sink in that what I have done is create a  
*local* instance of MyListItemArray in each view controller, and  
what's local to ViewControllerTabA has nothing to do with what is  
local to ViewControllerTabB.  I'm getting local copies of the  
array, not the source array.


As near as I can tell, there is no "source" array, nor is there any  
copying going on.


You have explicitly created two separate arrays from the get-go.  You  
have then filled them out separately to have similar initial contents.



It starts to occur to me that I don't actually want an instance, I  
want the real deal.


That statement is nonsensical.  There is no "real deal".  An instance  
is real.  It's not some pale reflection of something else.


You do want an instance.

I suspect that perhaps you don't understand the difference between an  
object and a pointer to an object.  The reason I suspect that is  
because the simple solution to your dilemma, the thing you're not  
seeing, is that you want a single instance referenced from multiple  
places using multiple pointers.



I read through some documentation about model objects and objects  
in general, and stumbled upon the concept of Singletons.  Some  
additional searching lead me to this blog post:

http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html
...which seems to describe exactly what I want.  I reconfigured  
MyListItemArray as a singleton, and remapped my data source methods  
accordingly, and everything seemingly works perfectly.  Items added  
in one view are displayed in the other, and so on.  It works so  
well that I have to assume there is a catch.


I can't shake the feeling that this seems more difficult than in  
ought to be, and generally when I feel that way there tends to be a  
single line of code solution that I haven't found yet.  Are  
singletons really the only way (that doesn't involve saving to a  
file, I suppose) to share a model object across multiple view  
controllers?  I think I'm missing something really fundamental.


Yes, I think your are missing something fundamental -- the ability to  
share references to an object simply by assigning multiple pointers to  
point to the same thing.


A singleton implementation is _one_ solution to the issue you're  
having, but it's not necessary, and not even recommended for something  
as simple as this.


One stumbling block is that you have two view controllers, but  
evidently no central application controller.  View controllers should  
have logic and state specific to a view and that view's relationship  
to the model.  An application controller manages your application  
overall.  It manages the other controllers, including your view  
controllers.  It has the primary responsibility for managing the  
application-global (as opposed to, for example, document-specific)  
model, and for providing access to that model to other parts of the  
program.


Since you were concentrating on the view controllers, you thought you  
had to create the instance of MyListItemArray in each view  
controller.  It follows that you got one such instance for each view  
controller.


You should have a single application controller object (an instance of  
some custom class).  This application controller is often also the  
application delegate.  It is also often instantiated in the main nib,  
where the "delegate" outlet of the main application object is  
connected to it.


If your application has one central list of items, then it would be  
the application controller's job to create and load that model.  It  
would hold references to the model objects in instance variables.  It  
would provide access

Re: Newbie Object Sharing Question

2009-01-30 Thread Joseph Crawford
Why don't you create a base controller that those 2 controllers  
subclass, that way they would inherit the functionality of reading  
those properties, also they would inherit the methods for getting,  
setting, etc.


BaseController
-- Controller 1
-- Controller 2

in Base controller define those properties and any methods for  
interacting with them
in Controller 1 and Controller 2 have your Class specific stuff there,  
because they both inherit from BaseController you would get those  
properties.


Joseph Crawford


On Jan 30, 2009, at 2:46 PM, Brian Slick wrote:

I'm sure there is a really easy concept somewhere that I'm just not  
getting, but well I'm just not getting it.  This is for the iPhone,  
but I believe it is generic to Cocoa.


Let's say I'm making a task list program, and the primary UI is a  
UITabBar.  On one tab, I want a table view presenting the list a  
certain way, and on another tab I want a table view presenting the  
same list in a different way.  Let's assume the user can create new  
items in either view.


I envision a couple of model objects like so:
MyListItem
MyListItemArray

And a couple of view controllers:
ViewControllerTabA
ViewControllerTabB

I want both view controllers to reference MyListItemArray.  So in  
each one I do something like:

MyListItemArray *listArray = [[MyListItemArray alloc] init];

I then provide the various table delegate methods, and link those to  
listArray.  I fire up the program, and see a few items in my list.   
I add a couple more items, then tap on the other view.  The new  
items are not there.  Using the log I have verified that the array  
size is the same as it started, so the new items simply are not there.


I throw in a few more log comments, trying to figure out exactly  
what I have.  I see the log messages for the initialization of the  
list items, but then I notice when I tap the next tab, I see those  
same initialization messages again.  It starts to sink in that what  
I have done is create a *local* instance of MyListItemArray in each  
view controller, and what's local to ViewControllerTabA has nothing  
to do with what is local to ViewControllerTabB.  I'm getting local  
copies of the array, not the source array.


It starts to occur to me that I don't actually want an instance, I  
want the real deal.  But I have no idea how to make that happen.   
How do I grab a hold of MyListItemArray and use it in the table data  
source methods without instantiating it?


I read through some documentation about model objects and objects in  
general, and stumbled upon the concept of Singletons.  Some  
additional searching lead me to this blog post:

http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html
...which seems to describe exactly what I want.  I reconfigured  
MyListItemArray as a singleton, and remapped my data source methods  
accordingly, and everything seemingly works perfectly.  Items added  
in one view are displayed in the other, and so on.  It works so well  
that I have to assume there is a catch.


I can't shake the feeling that this seems more difficult than in  
ought to be, and generally when I feel that way there tends to be a  
single line of code solution that I haven't found yet.  Are  
singletons really the only way (that doesn't involve saving to a  
file, I suppose) to share a model object across multiple view  
controllers?  I think I'm missing something really fundamental.


Thanks,

Brian
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/codebowl%40gmail.com

This email sent to codeb...@gmail.com


___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com


Newbie Object Sharing Question

2009-01-30 Thread Brian Slick
I'm sure there is a really easy concept somewhere that I'm just not  
getting, but well I'm just not getting it.  This is for the iPhone,  
but I believe it is generic to Cocoa.


Let's say I'm making a task list program, and the primary UI is a  
UITabBar.  On one tab, I want a table view presenting the list a  
certain way, and on another tab I want a table view presenting the  
same list in a different way.  Let's assume the user can create new  
items in either view.


I envision a couple of model objects like so:
MyListItem
MyListItemArray

And a couple of view controllers:
ViewControllerTabA
ViewControllerTabB

I want both view controllers to reference MyListItemArray.  So in each  
one I do something like:

MyListItemArray *listArray = [[MyListItemArray alloc] init];

I then provide the various table delegate methods, and link those to  
listArray.  I fire up the program, and see a few items in my list.  I  
add a couple more items, then tap on the other view.  The new items  
are not there.  Using the log I have verified that the array size is  
the same as it started, so the new items simply are not there.


I throw in a few more log comments, trying to figure out exactly what  
I have.  I see the log messages for the initialization of the list  
items, but then I notice when I tap the next tab, I see those same  
initialization messages again.  It starts to sink in that what I have  
done is create a *local* instance of MyListItemArray in each view  
controller, and what's local to ViewControllerTabA has nothing to do  
with what is local to ViewControllerTabB.  I'm getting local copies of  
the array, not the source array.


It starts to occur to me that I don't actually want an instance, I  
want the real deal.  But I have no idea how to make that happen.  How  
do I grab a hold of MyListItemArray and use it in the table data  
source methods without instantiating it?


I read through some documentation about model objects and objects in  
general, and stumbled upon the concept of Singletons.  Some additional  
searching lead me to this blog post:

http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html
...which seems to describe exactly what I want.  I reconfigured  
MyListItemArray as a singleton, and remapped my data source methods  
accordingly, and everything seemingly works perfectly.  Items added in  
one view are displayed in the other, and so on.  It works so well that  
I have to assume there is a catch.


I can't shake the feeling that this seems more difficult than in ought  
to be, and generally when I feel that way there tends to be a single  
line of code solution that I haven't found yet.  Are singletons really  
the only way (that doesn't involve saving to a file, I suppose) to  
share a model object across multiple view controllers?  I think I'm  
missing something really fundamental.


Thanks,

Brian
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com