我有一个定制的表,它可以获取对象和附加到该对象的名称(我最后调用HashTable
),它充满了逻辑门(在Gate
类下面)。我设置了HashTable
四个门是开关(我知道开关实际上不是逻辑门,但在这种情况下它们是Gate
的对象)。这些开关中的每一个都有一个名为switchIsOn
的布尔值。我有四个UISegmentedControl
s,如果值发生变化,则调用一个方法,使相应的开关switchIsOn
为真(段控制索引为1)或假(索引为0)。但是,在该方法之后,布尔值会自动恢复为NO
。这是代码:
HashTable标题:
#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;
-(BOOL)hasObjectWithName:(NSString*)name;
-(void)removeObjectFromIndex:(int)index;
-(void)removeObjectWithName:(NSString*)name;
-(BOOL)isEmpty;
-(void)dealloc;
@end
主:
#import "HashTable.h"
@implementation HashTable
NSMutableArray* values;
NSMutableArray* markers;
-(id)initWithLength:(int)capacity
{
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++)
{
NSString* dummy = [markers objectAtIndex:i];
if ([[markers objectAtIndex:i] isEqualToString:name]) {return [values objectAtIndex:i];}
}
return @"Not found";
}
-(BOOL)hasObjectWithName:(NSString*)name
{
for (int i = 0; i < [markers count]; i++)
{
if ([[markers objectAtIndex:i] isEqualToString:name]) {return YES;}
}
return NO;
}
-(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
和Gate类:
头:
#import <Foundation/Foundation.h>
@interface Gate : NSObject
{
NSString* type;
Gate* input1;
Gate* input2;
BOOL oneInput;
BOOL noInput;
BOOL switchIsOn;
}
-(id)initAsSWITCH;
-(void)switchOn:(BOOL)yesOrNo;
@property(nonatomic, retain) NSString* type;
@property(nonatomic, retain) Gate* input1;
@property(nonatomic, retain) Gate* input2;
@property(readwrite) BOOL oneInput;
@property(readwrite) BOOL noInput;
@property(readwrite) BOOL switchIsOn;
@end
主:
#import "Gate.h"
@implementation Gate
@synthesize type;
@synthesize input1, input2;
@synthesize oneInput, noInput, switchIsOn;
-(id)initAsSWITCH
{
self = [super init];
if (self)
{
type = [[NSString alloc] initWithString:@"SWITCH"];
oneInput = NO;
noInput = YES;
switchIsOn = NO;
}
return self;
}
-(void)switchOn:(BOOL)yesOrNo
{
switchIsOn = yesOrNo;
}
-(void)dealloc
{
[type release];
[input1 release];
[input2 release];
[super dealloc];
}
@end
然后是ViewController中首先出现问题的部分:
-(IBAction)valueChanged
{
if (switch00.selectedSegmentIndex > 0) {[[table getObjectWithName:@"S1"] switchOn:YES];}
else {[[table getObjectWithName:@"S1"] switchOn:NO];}
if (switch01.selectedSegmentIndex > 0) {[[table getObjectWithName:@"S2"] switchOn:YES];}
else {[[table getObjectWithName:@"S2"] switchOn:NO];}
if (switch10.selectedSegmentIndex > 0) {[[table getObjectWithName:@"S3"] switchOn:YES];}
else {[[table getObjectWithName:@"S3"] switchOn:NO];}
if (switch11.selectedSegmentIndex > 0) {[[table getObjectWithName:@"S4"] switchOn:YES];}
else {[[table getObjectWithName:@"S4"] switchOn:NO];}
Gate* dummy = [table getObjectWithName:@"S1"]; //These always come up as switchIsOn = NO, even though the switchOn method changes both of their booleans to YES.
Gate* dummy2 = [table getObjectWithName:@"S2"];
}
这不是switchOn方法的问题,因为我在调试器中检查了它,它可以工作。问题出在valueChanged方法中。这是怎么回事?
编辑:在弄乱了布尔值和调试器之后,我发现布尔switchIsOn
一旦踢出if then循环就会变成NO
这很重要吗?
编辑:我刚尝试添加NSNumber switchValue
。在开关的初始化方法中,intValue
为0,如果switchOn
方法的输入为YES,则它变为1。同样的问题,在方法启动后变回0。