Re: Datasource for tableview and more
Re: Datasource for tableview and more
- Subject: Re: Datasource for tableview and more
- From: Brendan Younger <email@hidden>
- Date: Sun, 2 Sep 2001 00:58:08 -0400
You're in luck, Peter. Unlike Ondra, I don't believe in RTFM replies.
While you definitely *should* read the manual especially given your lack
of understanding, it is often better to have concrete examples to work
from.
On Saturday, September 1, 2001, at 04:51 PM, Mark's Studio wrote:
>
Does it work like this
>
>
>
In MyDocument
>
>
if i want to access something from the GroupController
>
>
groupController = [GroupController sharedInstance];
>
>
[groupController anythingInGroupController]
>
or just
>
[GroupController anythingInGroupController]
The first one. Any method with a "+" preceding it means that it returns
a completely new object. A method with a "-" sign means that if you
already have an object, you can call upon it to execute that method.
That said, you make some implicit assumptions in the above. First, when
you write:
groupController = [GroupController sharedInstance];
You assume that sharedInstance is defined as such:
+ (GroupController*)sharedInstance;
(This is a safe assumption.)
Also, in the line "[groupController anythingInGroupController]",
anythingInGroupController is assumed to be a an instance method (it has
the "-" sign in front of it.) In this case, you already have an object
represented by the groupController variable and you want it to perform
some method. However, in the line: "[GroupController
anythingInGroupController]", you assume that anythingInGroupController
is a factory method (has the "+" sign in front of it). In this case
what you're doing is asking the generic, abstract thing called
GroupController to create a new, distinct instance of itself. (Hence
the names factory and instance.)
>
>
and do i need to declare everything with a + sign that i want to access
>
Declaring methods with "+" or "-" only tells the compiler and users of
the class which methods create new instances and which methods are
performed only by specific instances of the class. The tacit assumption
is that you implement these methods to do what the "+" or "-" sign
implies.
>
so if i want to Archive the NSMutableArray *GroupDB in MyDocument but
>
that is declared in GroupController
>
>
i have to do this in GroupController
>
>
+ (NSMutableArray *)GroupDB {
>
return GroupDB;
>
}
>
Bad. Don't do this. I assume that GroupDB is an instance variable. If
so, you are not creating a new instance of the class GroupController,
only passing along an object that already exists. This should have
a "-" sign in front of it. Also, it is often unwise to pass along
potentially mutable classes in accessor methods. You may want to
typecast it to an NSArray which won't stop other code from modifying it,
but if it does, it'll produce a compiler warning. Something like this:
- (NSArray *)groupDB
{
return groupDB;
}
(Notice the lowercase names. It is customary to start class names with
capitals and start methods names with lowercase letters.)
Even better than the above, but slower, would be:
- (NSArray *)groupDB
{
return [NSArray arrayWithArray:groupDB];
}
(Note also that even though I made a new instance of a class, it was not
a new instance of GroupController and therefore should not have the "+"
sign in front of it.)
>
and then this in MyDocument i can do this
>
>
[NSArchiver archivedDataWithRootObject:[GroupController
>
GroupDB]];
>
>
if i wanted
This is fine.
>
Original method from GroupController
>
>
- (IBAction)SelectedInGroupView:(id)sender
>
{
>
Group *aGroup;
>
int index = [GroupView selectedRow];
>
if(index > -1 && index <=[GroupDB count])
>
{
>
[GroupView selectRow:index byExtendingSelection:NO];
>
aGroup = [GroupDB objectAtIndex:index];
>
selectedGroupName =[aGroup groupName];
>
selectedGroupColor = [aGroup groupColor];
>
}
>
>
}
>
>
if i make a method in MyDocument
>
>
>
- (void)SelectionDidChangeInGroupView:
>
{
>
Group *aGroup;
>
int index = [GroupView selectedRow];
>
if(index > -1 && index <=[[GroupController GroupDB] count])
>
{
>
[GroupView selectRow:index byExtendingSelection:NO];
>
aGroup = [[GroupController GroupDB] objectAtIndex:index];
>
selectedGroupName =[aGroup groupName];
>
selectedGroupColor = [aGroup groupColor];
>
}
>
>
}
>
is it something like that
The above code is fine.
>
>
>
>
>
> -andrew
>
Peter Mark
>
>
Mark's Recording Studio A/S
>
Faelledvej 19 b DK2200 N
>
Copenhagen Denmark
>
Tel: +45 35366078 Fax: +45 35366038
>
www.marks-studio.dk
>
email@hidden