PDA

View Full Version : NSMutableArray Noob Prob




Yoda101B
Mar 7, 2009, 07:09 PM
Hi guys,

I'm tearing my hair out! I know I'm missing something silly but I just can work it out - any help would be very gratefully received!

Basically I have a class 'Node'


@interface Node : NSObject {

NSString *name;

NSMutableArray *inLets;

UIImageView *view;
}

@property (nonatomic, retain) NSMutableArray *inLets;
@property (nonatomic, retain) UIImageView *view;
@end


Node.M

@implementation Node

@synthesize name, isRunning, hasError,inLets;

-(id) init {

if (self = [super init]) {

hasError = NO;

self.inLets = [NSMutableArray array];
self.outLets = [[NSMutableArray alloc] init];

}
return self;
}




And then I have a MainViewController to display the nodes, and remembers which one is selected...

@interface MainViewController : UIViewController {


NodeView *currentSelection;


}

@property (nonatomic, retain) NodeView *currentSelection;



MainViewController.M

-(void)viewDidLoad {

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(nodeSelected:object:) name:@"nodeselected" object:nil];

}

-(void) nodeSelected:(NSNotification *)aNotification object:(id)object{

if (nodeSelected != nil) {
nodeSelected.isSelected = NO;
} else {

nodeSelected = [[NodeView alloc] init];
}

NodeView *NewCurrentSelection = [[NodeView alloc] init];
NewCurrentSelection = [aNotification object];

nodeSelected = nil;
nodeSelected = NewCurrentSelection;
nodeSelected.isSelected = YES;


bottomHudViewController.nameLabel.text = nodeSelected.owner.name;

[bottomHudViewController showBottomHudView:(YES)];
}


The NodeView class inherits from the UIImageView class and just posts a Notification when it's clicked on....


My problem is to do with NewCurrentSelection, for some reason no matter what I do the debugger always sees the inLets array changing to a CALayerArray - which obviously no good to me?

What am I doing wrong????


Thanks very much in advance!!

Yoda



dejo
Mar 7, 2009, 07:21 PM
What is the relationship between Node and NodeView? Because the top part of your posting shows us the definition of the class Node and then the MainViewController only refers to NodeView.

Yoda101B
Mar 8, 2009, 04:49 AM
In MainViewController.M

- (void) createNodeViewFromNode:(Node *)node {

NodeView *nodeView = [[NodeView alloc] initWithImage: [UIImage imageNamed:@"NodeLozNormal.png"]];
[nodeView setUserInteractionEnabled:YES];

node.view = nodeView;
nodeView.owner = node;


[self.view addSubview:nodeView];

[nodeView release];

}

But I'm not sure if thats related to my problem.... Thanks in advance

dejo
Mar 8, 2009, 10:04 AM
Also, these lines of code seem curious (and are the cause of a memory leak):
NodeView *NewCurrentSelection = [[NodeView alloc] init];
NewCurrentSelection = [aNotification object];
You instantiate a new NodeView and then throw that away as you assign it aNotification's object.

Yoda101B
Mar 8, 2009, 10:33 AM
Yeah they are a bit odd and have changed to...

if (nodeSelected != nil) {
nodeSelected.isSelected = NO;
}

[nodeSelected release];

nodeSelected = [[aNotification object] retain];

nodeSelected.isSelected = YES;



I think thats right... to behonest I'm still scratching my head over when I should retain, or copy....

I've finally sorted out my other problem (after a long night!!) Looks like I accidently was alloc++init the array twice!

Thanks Though....