On May 11, 2009, at 10:24 PM, Patrick Mau wrote:

Hallo Chris

The NIB loading guide states that custom objects, like your model object,
are created using 'initWithCoder' and not plain 'init'.

For many objects that is the case, but instances of "Custom View" and "Custom Object" will be created with initWithFrame:, and init.

Here's a link to the documentation:
http://developer.apple.com/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html#//apple_ref/doc/uid/10000051i-CH4-SW18

Good Luck -
Jon Hess


I would advise you to implement 'awakeFromNib' in your view and use
NSLog() to print your datasource and its buffer or simply to set
a breakpoint there.

Regards
Patrick

On 12.05.2009, at 03:29, Chris Carson wrote:


Hello,

I've been scratching my head trying to get a basic delegate/data source Cocoa/AppKit program working and feel that I'm misunderstanding something basic (I've included the code is at the end).

I set a breakpoint within the if statement in the init method of MyModel and see it called more than once. The second time the debugger throws a EXC_BAD_ACCESS. This message goes away if I comment out the two lines in stepAnimation that call the getBuffer and getLength methods and then modify the buffer. All this makes me think that somehow the instance of MyModel is being deallocated as soon as the data source message returns, but before the data is copied into the pointBuffer.

I created an NSView and NSObject in Interface Builder, assigning the MyView subclass to the NSView object and MyModel to the NSObject object. Then I connected the dataSource outlet on MyView to th MyModel object.

What am I missing? How can I get the data from MyModel to be viewable my MyView?

Sidenote: Ultimately I'm trying to plot a real-time frequency spectrum. My plan is to pull in data over a CFMessagePort in the model and have the view access it view the data source.

Help is much appreciated!

Chris

main.m
----------

int main(int argc, char *argv[]) {
  // Launch Cocoa application
  return NSApplicationMain(argc, (const char **) argv);
}

MyView.m:
----------
@implementation MyView

- (id)initWithFrame:(NSRect)frame {
  self = [super initWithFrame:frame];
  if (self) {
      pointPath = [NSBezierPathbezierPath];
      pointBuffer = malloc (sizeof(NSPoint) * 100);
      [NSTimerscheduledTimerWithTimeInterval:0.1
          target:self
          selector:@selector(stepAnimation:)
          userInfo:nil
          repeats:YES];
  }
  return self;
}

- (void)drawRect:(NSRect)rect {
  [pointPath removeAllPoints];
[pointPathappendBezierPathWithPoints:(NSPointArray) pointBuffercount:100];
  [pointPathstroke];
}

- (void)setDataSource:(id)inDataSource {
  dataSource = inDataSource;
}

- (id)dataSource {
  returndataSource;
}

- (void)stepAnimation:(NSTimer *)timer {
  float*inBuffer;
  int i, length;


  // Fetch buffer pointer
  inBuffer = [[self dataSource] getBuffer:self];
  length   = [[self dataSource] getLength:self];

  // Copy to point buffer
  for (i = 0; i < length; i++) {
      pointBuffer[i].y = inBuffer[i];
      pointBuffer[i].x = i;
  }

  // Mark display for painting
  [selfsetNeedsDisplay:YES];
}

- (void)dealloc {
  free(pointBuffer);
  [super dealloc];
}

@end

MyView.h
----------
@interface MyView : NSView {
  IBOutletid dataSource;
  NSBezierPath *pointPath;
  NSPoint *pointBuffer;
}

- (void)setDataSource:(id)inDataSource;
- (id)dataSource;
- (void)stepAnimation:(NSTimer *)timer;

@end

@interface NSObject(MyViewDataSource)

- (float *)getBuffer:(MyView *)view;
- (int)getLength:(MyView *)view;

@end

MyModel.m
----------
@implementationMyModel

- (id)init {
  self= [superinit];
  if (self) {
      buffer = malloc (sizeof(float) * 100);
      length = 100;
  }
  return self;
}

- (float *)getBuffer:(GraphView *)view {
  returnbuffer;
}

- (int)getLength:(GraphView *)view {
  returnlength;
}

- (void)dealloc {
  free (buffer);
  [super dealloc];
}

@end

MyModel.h
----------
@interface MyModel : NSObject {
  float *buffer;
  int length;
}

- (float *)getBuffer:(GraphView *)view;
- (int)getLength:(GraphView *)view;

@end




_______________________________________________

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/pmau%40me.com

This email sent to p...@me.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/jhess%40apple.com

This email sent to jh...@apple.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

Reply via email to