开发者

Issue initializing an object of a custom class

开发者 https://www.devze.com 2023-04-11 08:10 出处:网络
I\'m trying to make a custom HashTable for my program. Yes, I know there is already a HashTable class in Xcode, but for this case I have a custom one. It\'s supposed to be simple, but when I\'m trying

I'm trying to make a custom HashTable for my program. Yes, I know there is already a HashTable class in Xcode, but for this case I have a custom one. It's supposed to be simple, but when I'm trying to use it in my view controller, the debugger shows its value to be "0x0", even after calling the initialization method. Here's the code:

//header file HashTable.h

#import <Foundation/Foundation.h>


@interface HashTable : NSObject 
{   
}

-(void)initWithLength:(int)capacity;
-(void)add:(NSObject*)object withName:(NSString*)name;
-(id)getObjectFromIndex:(int)index;
-(id)getObjectWithName:(NSString*)name;

@end


//main file HashTable.m

#import "HashTable.h"

@implementation HashTable

NSMutableArray* values;
NSMutableArray* markers;

-(id)initWithLength:(int)capacity  //Apparently, this never gets called
{
    self = [super init];
    if (self) 
    {
        values = [[NSMutableArray alloc] initWithCapacity:capacity];
        markers = [[NSMutableArray alloc] initWithCapacity:capacity];
    }
    return self;
}

-(void)add:(NSObject*)object withName:(NSString*)name
{
    [values addObject:object];
    [markers addObject:name];
}

-(id)getObjectFromIndex:(int)index
{
    return [values objectAtIndex:index];
}

-(id)getObjectWithName:(NSString*)name
{
    for (int i = 0; i < [markers count]; i++) 
    {
        if ([[markers objectAtIndex:i] isEqualToString:name]) {return [values objectAtIndex:i];}
    }
    return [NSObject new];
}

-(void)removeObjectFromIndex:(int)index
{
    [values removeObjectAtIndex:index];
    [markers removeObjectAtIndex:index];
}

-(void)removeObjectWithName:(NSString*)name
{
    for (int i = 0; i < [markers count]; i++) 
    {
        if ([[markers objectAtIndex:i] isEqualToString:name]) 
        {
            [values removeObjectAtIndex:i];
            [markers removeObjectAtIndex:i];
            return;
        }
    }
}

-(BOOL)isEmpty
{
    return [values count] == 0;
}

-(void)dealloc
{
    [values release];
    [markers release];
    [super dealloc];
}

@end

Then I have the segments of the view controller that uses HashTable:

//header file

#import <UIKit/UIKit.h>

#import "HashTable.h"

@interface Circuitry_LabViewController : UIViewController 
{
    HashTa开发者_StackOverflowble* table;
}

@property(nonatomic, retain) HashTable* table;

@end

//main file

#import "Circuitry_LabViewController.h"

@implementation Circuitry_LabViewController

@synthesize table;

- (void)viewDidLoad 
{
    [table initWithLength:10];
    [super viewDidLoad];
}

I can't see what I'm missing here. Can anyone help?


You meant to do this in -viewDidLoad:

table = [[HashTable alloc] initWithLength:10];

Something's telling me that you're doing it wrong.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号