method returning wrong variable

This class I'm working with has three instance variables I'm interested in, NSmutablearrays xArray yArray and zArray, there are also other NSmutablearrays, a, bArray and c. I just added accessors

- (NSMutableArray *) xArray {
   return xArray;
}

ditto for y and z. However, it's returning yArray, zArray and bArray for some reason. Why is that happening?


Okay, I just changed the names of the methods to GetXArray and everything seems to be returning the right variables. Now I'm really confused. How the heck did this happen and how do I prevent this from happening in the future?

Answers


This is fairly straight forward setting/getter stuff:

XYZArray.h

#import <Cocoa/Cocoa.h>
@interface XYZArray : NSObject {
  NSMutableArray *x;
  NSMutableArray *y;
  NSMutableArray *z;
}

-(NSMutableArray*) x;
-(NSMutableArray*) y;
-(NSMutableArray*) z;

-(void) setX:(NSMutableArray*)newArray;
-(void) setY:(NSMutableArray*)newArray;
-(void) setZ:(NSMutableArray*)newArray;
@end

XYZArray.m

#import "XYZArray.h"
@implementation XYZArray
- (id)init
{
    if((self = [super init]))
    {
      x = [[NSMutableArray alloc] init];
      y = [[NSMutableArray alloc] init];  
      z = [[NSMutableArray alloc] init];  
    }
    return self;
}

- (void) dealloc
{
  [x release];
  [y release];
  [z release];
  [super dealloc];
}


-(NSMutableArray*) x
{
  return x;
}

-(NSMutableArray*) y
{
  return y;
}

-(NSMutableArray*) z
{
  return z;
}

-(void) setX:(NSMutableArray*)newArray
{
  [newArray retain];
  [x release];
  x = newArray;
}

-(void) setY:(NSMutableArray*)newArray
{
  [newArray retain];
  [y release];
  y = newArray;
}

-(void) setZ:(NSMutableArray*)newArray
{
  [newArray retain];
  [z release];
  z = newArray;
}
@end

Better still, if you are using Objective-C 2.0 you can do all of this with properties and not write any code at all. Read this for an introduction, http://macdevelopertips.com/objective-c/objective-c-properties-setters-and-dot-syntax.html.


Need Your Help

Java instance variable visibility (ThreadLocal)

java garbage-collection visibility thread-local

In the class ReentrantReadWriteLock is the following curious comment:

Use SharePoint Search to crawl Project Server project metadata?

sharepoint sharepoint-2007 moss project-server-2007

Our environment consists of Project Server 2007 and MOSS 2007. We have around 750 projects and lots of "Enterprise Custom Fields" set up to track all of the metadata associated with a project.