Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Weird NSInvalidArgumentException

Weird NSInvalidArgumentException
Thread Tools
RDG
Fresh-Faced Recruit
Join Date: Feb 2007
Status: Offline
Reply With Quote
Jun 1, 2009, 04:20 PM
 
I'm getting this error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[CALayerArray drawInContext:]: unrecognized selector sent to instance 0xd1b2f0'

only when I call a method of the class TBLevel in the drawRect: method of TBView.

Here's the code:

TBLevel.h
Code:
#import <Foundation/Foundation.h> #ifndef _TB_OBJ #define _TB_OBJ typedef struct { CGImageRef image; BOOL isObject; } TBObject; #endif @interface TBLevel : NSObject { int _width; int _height; int _playerX; int _playerY; TBObject **cells; CGContextRef context; } + (id)levelWithWidth:(int)width height:(int)height playerX:(int)playerX andPlayerY:(int)playerY; - (id)initWithWidth:(int)width height:(int)height playerX:(int)playerX andPlayerY:(int)playerY; - (void)insertObject:(TBObject)obj atPoint:(CGPoint)loc; - (void)drawInContext:(CGContextRef)ctxt; - (int)width; - (int)height; @end
TBlevel.m
Code:
#import "TBLevel.h" @implementation TBLevel + (id)levelWithWidth:(int)width height:(int)height playerX:(int)playerX andPlayerY:(int)playerY { return [[[TBLevel alloc] initWithWidth:width height:height playerX:playerX andPlayerY:playerY] autorelease]; } - (id)initWithWidth:(int)width height:(int)height playerX:(int)playerX andPlayerY:(int)playerY { _width = width; _height = height; _playerX = playerX; _playerY = playerY; cells = (TBObject **)malloc(sizeof(TBObject *) * width); for (int i = 0; i < width; i++) { cells[i] = (TBObject *)malloc(sizeof(TBObject) * height); } return self; } - (void)drawInContext:(CGContextRef)ctxt { context = ctxt; for (int i = 0; i < _width; i++) { for (int j = 0; j < _height; j++) { CGImageRef image = cells[i][j].image; CGRect imgRect = CGRectMake(i * 32, j * 32, 32, 32); CGContextDrawImage(context, imgRect, image); CGImageRelease(image); } } } - (void)insertObject:(TBObject)obj atPoint:(CGPoint)loc { cells[(int)loc.x][(int)loc.y] = obj; } - (int)width { return _width; } - (int)height { return _height; } - (void)dealloc { free(cells); cells = NULL; CGContextRelease(context); [super dealloc]; } @end
TBView.m
Code:
#import "TBView.h" @implementation TBView - (id)initWithCoder:(NSCoder *)coder { if (self = [super initWithCoder:coder]) { level = [TBLevel levelWithWidth:10 height:10 playerX:2 andPlayerY:1]; TBObject obj; obj.image = [UIImage imageNamed:@"test.png"].CGImage; obj.isObject = YES; [level insertObject:obj atPoint:CGPointMake(0, 1)]; //Notice the above method call (it doesn't generate an exception) playerView = [[TBPlayerView alloc] initWithFrame:CGRectMake(144, 144, 32, 32)]; tallObjectsView = [[TBTallObjectsView alloc] initWithFrame:self.frame]; [self addSubview:playerView]; [self addSubview:tallObjectsView]; } return self; } - (void)drawRect:(CGRect)rect { [[UIColor blackColor] set]; UIRectFill(rect); [level drawInContext:UIGraphicsGetCurrentContext()]; //This is the method call that generates the exception, if I put [level drawInContext:NULL] in the initWithCoder: method, it works [playerView setNeedsDisplay]; [tallObjectsView setNeedsDisplay]; } - (void)dealloc { [super dealloc]; } @end
Notice the comments in TBView.m.
If I call a method of level from initWithCoder: nohthing happens, but if I call it from drawRect: I get an exception.

Am I missing something?
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 05:19 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2