Re: Seeking debugging advice
Re: Seeking debugging advice
- Subject: Re: Seeking debugging advice
- From: David Aames <email@hidden>
- Date: Fri, 27 Oct 2006 23:34:28 +0100
Okay, I'm gonna try to illustrate the point with example code. Let's
say that the general architecture of your data model & controller is
something similar to the following:
1. You have class which has a string property
2. You have an array (NSMutableArray) which holds instances of this
class
3. The class another single property which is used as an identifier
to the user (eg name, id, etc)
4. The identifier property is used in a table to represent the object
5. When the user clicks on a row additional user elements are used to
display the other attributes of the current selected object (eg a
checkbox NSButton could be used to visualize a BOOL property, etc)
6. In this particular case we assume that one of our string
properties (outlined in 1) is going to be displayed using a NSTextView
Now let us consider our general algorithms for getting this setup
running. Firstly we need to update the current selected object's
property whenever the user modifies the NSTextView - so we're going
to use a delegate and implement - (void)textDidChange:(NSNotification
*)aNotification which is called everytime the contents of the text
view change.
We also need to provide an implementation for the action method of
the table - obviously the controls need to be updated when the user
clicks on another row. So let's write down the general algorithm for
these two methods.
- (void)textDidChange:(NSNotification *)aNotification
1. Retrieve the NSText object
2. Retrieve the current selected object
3. Update the relevant property
- Action method
1. Check if the user clicked on a row
2. If a row was clicked retrieve the object representing the just
selected row
3. Update the relevant GUI objects representing the respective
properties
Because the actual logic bug which I was investigating for 4 days
lies in the delegate method of the text view I'm gonna first provide
some sample code for the table action method. So here it goes:
if([self currentSelectedLocation])
{
NSLog(@"Clicked on a valid row");
[self enableLocationEditboxes];
DALocation* loc = [self currentSelectedLocation];
NSLog(@"Current clicked location object is: %@", loc);
[tagsLocsLocationView setString:[loc locationAddress]];
}
else
[self disableLocationEditboxes];
The method is pretty straightforward. [self currentSelectedLocation]
returns the current selected object or nil if no row is selected.
[tagsLocsLocationView setString:[loc locationAddress]]; updates the
view to display the address of the current object.
Now onto the implementation of the delegate method. My first
implementation was something similar to the following:
NSLog(@"Address changed");
NSText* t = [aNotification object];
DALocation* loc = [self currentSelectedLocation];
NSLog(@"Setting the address to %@", [t string]);
[loc setLocationAddress:[t string]];
Now imagine that we have two instances of our objects. When you type
something in the text view the address property is set to the address
of the mutable string which acts as a storage for the text view. So
when you select the other row this statement - [tagsLocsLocationView
setString:[loc locationAddress]]; is executed so it updates the text
storage of the current object - BUT because the storage is mutable
both objects' properties point to the same string address! So your
custom objects' property share the same object consequently modifying
one of them modifies all of them (due to the mutable nature of the
text view's storage). So the proper way to implement the delegate
method is something similar to this:
NSLog(@"Address changed");
NSText* t = [aNotification object];
DALocation* loc = [self currentSelectedLocation];
NSLog(@"Setting the address to %@", [t string]);
NSString* str = [NSString stringWithUTF8String:[[t string] UTF8String]];
[loc setLocationAddress:str];
This implementation uses a copy of the string so modifying the view's
text storage doesn't affect the other object. I hope this post clears
things up.
Regards,
David
On 27 Oct 2006, at 21:56, Alan Smith wrote:
I don't understand. Would you mind explaining it more thoroughly?
It should be as clear as possible for anyone searching the archives.
Congratulations!
Peace, Alan
--
// Quotes from yours truly -------------------------
"You don't forget, you just don't remember."
"Maturity resides in the mind."
"Silence is the Universe's greatest gift."
"When the World realizes that religion really is unimportant, then it
shall evolve."
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden