我正在尝试为我的程序制作自定义HashTable。是的,我知道Xcode中已经有一个HashTable类,但是对于这种情况我有一个自定义类。它应该很简单,但是当我尝试在我的视图控制器中使用它时,即使在调用初始化方法之后,调试器也将其值显示为“0x0”。这是代码:
//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
然后我有视图控制器的段使用HashTable:
//header file
#import <UIKit/UIKit.h>
#import "HashTable.h"
@interface Circuitry_LabViewController : UIViewController
{
HashTable* 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];
}
我看不到我在这里失踪的东西。有人可以帮忙吗?
答案 0 :(得分:1)
您打算在-viewDidLoad:
table = [[HashTable alloc] initWithLength:10];
有人告诉我你做错了。