Re: What difference exist from where a method will be called?
Re: What difference exist from where a method will be called?
- Subject: Re: What difference exist from where a method will be called?
- From: Raimond Hettrich <email@hidden>
- Date: Thu, 08 Sep 2016 13:27:27 +0200
Hi Jens,
thank You.
Because I’m not able to understand how a second instance is created, I started a test project.
It is a document-based application with a cell-based tableview, an add- and an remove-button, a custom view which is subclassed with the „DelegateClass“ and a testButton.
The tableViews content is managed by an arrayController. It's array is created in the document class and the instance variables which are holding the values of the tableViews columns are created in the Person class.
Because I need the tableViews data in another class I created a protocol in the document class which holds the tableViewArray. The Document class is also a delegate of the tableView. And with every change in the tableView the array will be sent to the DelegateClass which conforms to the protocol.
So with every change in the tableView the DelegateClass getArray-method gets the tableViewArray and in this method I set my delegateArray to the tableViewArray. So the printout shows that the delegateArray holds correctly the tableViewArray.
A next method in the DelegateClass the showArray-method is just printing out the delegateArray. And this method is called by two different ways. The first is just a button action and the second is a call from the drawRect-method.
If I call the showArray-method with a button press, the method shows the array correctly. If the showArray-method is called by the drawRect-method the array is (null).
Log after the first row is added in the tableView:
2016-09-08 13:06:51.178 Test[9055:5158221] tableViewSelectionDidChange
2016-09-08 13:06:51.179 Test[9055:5158221] DelegateArray: (
"<Person: 0x600000038960>"
)
Log after the button press:
2016-09-08 13:12:51.794 Test[9062:5159862] showArray: (
"<Person: 0x600000035640>"
)
Log after I’m just resizing the view so that I force the drawRect-method to run and call the showArray-method.
2016-09-08 13:12:58.802 Test[9062:5159862] showArray: (null)
2016-09-08 13:12:58.834 Test[9062:5159862] showArray: (null)
2016-09-08 13:12:58.842 Test[9062:5159862] showArray: (null)
2016-09-08 13:12:58.865 Test[9062:5159862] showArray: (null)
2016-09-08 13:12:58.885 Test[9062:5159862] showArray: (null)
2016-09-08 13:12:58.994 Test[9062:5159862] showArray: (null)
Log after another button press:
2016-09-08 13:15:29.563 Test[9062:5159862] showArray: (
"<Person: 0x600000035640>"
)
Here the code:
//
// Document.h
// Test
//
#import <Cocoa/Cocoa.h>
@protocol DocumentProtocol <NSObject>
- (void)getArray:(NSMutableArray *)array;
@end
@interface Document : NSDocument {
NSMutableArray *tableViewArray;
}
@property (weak) id<DocumentProtocol> documentDelegate;
@property (copy) NSMutableArray *tableViewArray;
@end
//
// Document.m
// Test
//
#import "Document.h"
@interface Document ()
@end
@implementation Document
@synthesize documentDelegate;
@synthesize tableViewArray;
- (instancetype)init {
self = [super init];
if (self) {
tableViewArray = [[NSMutableArray alloc]init];
}
return self;
}
-(void)tableViewSelectionDidChange:(NSNotification *)notification{
NSLog(@"tableViewSelectionDidChange");
[documentDelegate getArray:tableViewArray];
}
+ (BOOL)autosavesInPlace {
return YES;
}
- (NSString *)windowNibName {
// Override returning the nib file name of the document
// If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
return @"Document";
}
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError {
// Insert code here to write your document to data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning nil.
// You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
[NSException raise:@"UnimplementedMethod" format:@"%@ is unimplemented", NSStringFromSelector(_cmd)];
return nil;
}
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError {
// Insert code here to read your document from the given data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning NO.
// You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead.
// If you override either of these, you should also override -isEntireFileLoaded to return NO if the contents are lazily loaded.
[NSException raise:@"UnimplementedMethod" format:@"%@ is unimplemented", NSStringFromSelector(_cmd)];
return YES;
}
@end
//
// Person.h
// Test
//
#import <Foundation/Foundation.h>
@interface Person : NSObject {
NSString *name;
int time;
}
@property (copy) NSString *name;
@property int time;
@end
//
// Person.m
// Test
//
#import "Person.h"
@implementation Person
@synthesize name;
@synthesize time;
- (id)init {
self = [super init];
if (self) {
name = @"Hello";
time = 10;
}
return self;
}
@end
//
// DelegateClass.h
// Test
//
#import <Cocoa/Cocoa.h>
#import "Document.h"
@interface DelegateClass : NSView <DocumentProtocol> {
NSMutableArray *delegateArray;
__weak IBOutlet Document *documentOutlet;
}
@end
//
// DelegateClass.m
// Test
//
#import "DelegateClass.h"
@implementation DelegateClass
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
[self showArray];
}
- (id)init {
self = [super init];
if (self) {
delegateArray = [[NSMutableArray alloc]init];
}
return self;
}
- (void)awakeFromNib {
[documentOutlet setDocumentDelegate:self];
}
- (void)getArray:(NSMutableArray *)array {
delegateArray = array;
NSLog(@"DelegateArray: %@", delegateArray);
}
- (void)showArray {
NSLog(@"showArray: %@", delegateArray);
}
- (IBAction)testButton:(id)sender {
[self showArray];
}
@end
I hope there is someone out there who has the endurance to read this mail. :)
Thank You!
Raimond
> Am 07.09.2016 um 23:44 schrieb Jens Alfke <email@hidden>:
>
> Hi Raimond,
>
> Please don’t email me off-list. Keep the conversation on the mailing list.
>
>> On Sep 7, 2016, at 2:24 PM, Raimond Hettrich <email@hidden <mailto:email@hidden>> wrote:
>>
>> It looks like the method always prints out the same instance of the array, but if it will be called from drawRect it’s printing out another instance.
>
> It’s almost certainly a bug in your code. Maybe there are two instances of your view and you haven’t realized it. You’re going to have to do some debugging.
>
> —Jens
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden