Objective c 键值编码符合性
我很愚蠢,没有按照编程的方式持续测试,所以现在我不确定错误是从哪里来的。我在做一个可编程计算器。当我运行时,它会在显示任何内容之前崩溃,并向我显示以下消息:Objective c 键值编码符合性,objective-c,ios5,xcode4.3,key-value,Objective C,Ios5,Xcode4.3,Key Value,我很愚蠢,没有按照编程的方式持续测试,所以现在我不确定错误是从哪里来的。我在做一个可编程计算器。当我运行时,它会在显示任何内容之前崩溃,并向我显示以下消息: *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<CalculatorViewController 0x6a405a0> setValue:forUndefinedKey:]: this class is not k
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<CalculatorViewController 0x6a405a0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key description.'
这是CalculatorBrain.m:
#import "CalculatorBrain.h"
@interface CalculatorBrain()
@property (nonatomic, strong) NSMutableArray *programStack;
@property (nonatomic,strong)NSDictionary *variableValues;
@end
@implementation CalculatorBrain
@synthesize programStack = _programStack;
@synthesize variableValues = _variableValues;
- (NSMutableArray *)programStack
{
if (!_programStack) _programStack = [[NSMutableArray alloc] init];
return _programStack;
}
- (id)program
{
return [self.programStack copy];
}
//Here are the two types of pushes that the ViewController can implement. First, operand pushes . . .
- (void)pushOperand:(double)operand
{
[self.programStack addObject:[NSNumber numberWithDouble:operand]];
}
//. . . and then variable pushes.
- (void) pushOperandAsVariable:(NSString *)variable
{
//Create dictionary
//Move this later on to ViewController but for now leave where it is....
NSMutableArray *variablesUsed = [[NSMutableArray alloc] init];
NSArray *objects = [[NSArray alloc] initWithObjects:[NSNumber numberWithDouble:3],[NSNumber numberWithDouble:4.1],[NSNumber numberWithDouble:-6],[NSNumber numberWithDouble:4.5298], [NSNumber numberWithDouble:3.14159], nil];
NSArray *keys = [[NSArray alloc] initWithObjects:@"x",@"y",@"z",@"foo", @"π", nil];
NSDictionary *variableValues = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
//Check program for keys
NSNumber *operand;
for (int i=0; i<keys.count; i++)
{
if ([[keys objectAtIndex:i] isEqual:variable])
[variablesUsed addObject:variable];
operand = [variableValues objectForKey:variable];
}
[self.programStack addObject:operand];
}
- (double)performOperation:(NSString *)operation
{
[self.programStack addObject:operation];
return [[self class] runProgram:self.program];
}
+ (double)popOffStack:(NSMutableArray *)stack
{
double result = 0;
id topOfStack = [stack lastObject];
if (topOfStack) [stack removeLastObject];
if ([topOfStack isKindOfClass:[NSNumber class]])
{
result = [topOfStack doubleValue];
}
//Here are the results for various operations.
else if ([topOfStack isKindOfClass:[NSString class]])
{
NSString *operation = topOfStack;
if ([operation isEqualToString:@"+"])
{
result = [self popOffStack:stack] +
[self popOffStack:stack];
}
else if ([@"*" isEqualToString:operation])
{
result = [self popOffStack:stack] *
[self popOffStack:stack];
}
else if ([operation isEqualToString:@"-"])
{
double subtrahend = [self popOffStack:stack];
result = [self popOffStack:stack] - subtrahend;
}
else if ([operation isEqualToString:@"/"])
{
double divisor = [self popOffStack:stack];
if (divisor) result = [self popOffStack:stack] / divisor;
}
else if ([operation isEqualToString:@"sin"])
{
result = sin([self popOffStack:stack]);
}
else if ([operation isEqualToString:@"cos"])
{
result = cos([self popOffStack:stack]);
}
else if ([operation isEqualToString:@"√"])
{
result = sqrt([self popOffStack:stack]);
}
else if ([operation isEqualToString:@"π"])
{
result = M_PI;
}
}
return result;
}
+ (double)runProgram:(id)program
{
//Run program.
NSMutableArray *mutableCopyOfProgram;
if ([program isKindOfClass:[NSArray class]])
{
mutableCopyOfProgram = [program mutableCopy];
return [self popOffStack:mutableCopyOfProgram];
}
else return 0;
}
@end
#导入“CalculatorBrain.h”
@接口计算器Brain()
@属性(非原子,强)NSMutableArray*programStack;
@属性(非原子,强)NSDictionary*可变值;
@结束
@实现计算器Brain
@综合程序堆栈=_程序堆栈;
@综合可变值=_可变值;
-(NSMutableArray*)程序堆栈
{
如果(!\u programStack)\u programStack=[[NSMutableArray alloc]init];
返回程序栈;
}
-(id)方案
{
返回[self.programStack copy];
}
//以下是ViewController可以实现的两种推送类型。首先,操作数推送。
-(无效)操作数:(双)操作数
{
[self.programStack addObject:[NSNumber numberWithDouble:操作数]];
}
//. . . 然后是可变推力。
-(void)pushOperationAsVariable:(NSString*)变量
{
//创建字典
//稍后将其移动到ViewController,但现在请保留其位置。。。。
NSMutableArray*variablesUsed=[[NSMutableArray alloc]init];
NSArray*对象=[[NSArray alloc]initWithObjects:[NSNumber numberWithDouble:3],[NSNumber numberWithDouble:4.1],[NSNumber numberWithDouble:-6],[NSNumber numberWithDouble:4.5298],[NSNumber numberWithDouble:3.14159],无];
NSArray*keys=[[NSArray alloc]initWithObjects:@“x”@“y”@“z”@“foo”@“π”,nil];
NSDictionary*variableValues=[[NSDictionary alloc]initWithObjects:objects forKeys:keys];
//检查程序中的键
NSNumber*操作数;
对于(int i=0;i而言,最常见的原因是在故事板或xib文件中定义了一个CalculatorViewController
对象,图形界面中有一个指向其上一个名为“description”的出口的链接。同时,您在该课程的代码中不再有说明
出口
这通常会通过在interface builder中跟踪并删除错误的引用来解决。最常见的原因是您在序列图像板或xib文件中定义了一个CalculatorViewController
对象,图形界面中的某个对象有一个指向其上名为“描述”的出口的链接。同时,您在该课程的代码中不再有说明
出口
这通常会通过在interface builder中跟踪并删除错误的引用来解决。是的。这就解决了它。我将description
重命名为descriptionLabel
,但一开始还没有删除description
。是的。这就解决了它。我将description
重命名为desScriptionLabel
但一开始还没有摆脱描述
。。。。
#import "CalculatorBrain.h"
@interface CalculatorBrain()
@property (nonatomic, strong) NSMutableArray *programStack;
@property (nonatomic,strong)NSDictionary *variableValues;
@end
@implementation CalculatorBrain
@synthesize programStack = _programStack;
@synthesize variableValues = _variableValues;
- (NSMutableArray *)programStack
{
if (!_programStack) _programStack = [[NSMutableArray alloc] init];
return _programStack;
}
- (id)program
{
return [self.programStack copy];
}
//Here are the two types of pushes that the ViewController can implement. First, operand pushes . . .
- (void)pushOperand:(double)operand
{
[self.programStack addObject:[NSNumber numberWithDouble:operand]];
}
//. . . and then variable pushes.
- (void) pushOperandAsVariable:(NSString *)variable
{
//Create dictionary
//Move this later on to ViewController but for now leave where it is....
NSMutableArray *variablesUsed = [[NSMutableArray alloc] init];
NSArray *objects = [[NSArray alloc] initWithObjects:[NSNumber numberWithDouble:3],[NSNumber numberWithDouble:4.1],[NSNumber numberWithDouble:-6],[NSNumber numberWithDouble:4.5298], [NSNumber numberWithDouble:3.14159], nil];
NSArray *keys = [[NSArray alloc] initWithObjects:@"x",@"y",@"z",@"foo", @"π", nil];
NSDictionary *variableValues = [[NSDictionary alloc] initWithObjects:objects forKeys:keys];
//Check program for keys
NSNumber *operand;
for (int i=0; i<keys.count; i++)
{
if ([[keys objectAtIndex:i] isEqual:variable])
[variablesUsed addObject:variable];
operand = [variableValues objectForKey:variable];
}
[self.programStack addObject:operand];
}
- (double)performOperation:(NSString *)operation
{
[self.programStack addObject:operation];
return [[self class] runProgram:self.program];
}
+ (double)popOffStack:(NSMutableArray *)stack
{
double result = 0;
id topOfStack = [stack lastObject];
if (topOfStack) [stack removeLastObject];
if ([topOfStack isKindOfClass:[NSNumber class]])
{
result = [topOfStack doubleValue];
}
//Here are the results for various operations.
else if ([topOfStack isKindOfClass:[NSString class]])
{
NSString *operation = topOfStack;
if ([operation isEqualToString:@"+"])
{
result = [self popOffStack:stack] +
[self popOffStack:stack];
}
else if ([@"*" isEqualToString:operation])
{
result = [self popOffStack:stack] *
[self popOffStack:stack];
}
else if ([operation isEqualToString:@"-"])
{
double subtrahend = [self popOffStack:stack];
result = [self popOffStack:stack] - subtrahend;
}
else if ([operation isEqualToString:@"/"])
{
double divisor = [self popOffStack:stack];
if (divisor) result = [self popOffStack:stack] / divisor;
}
else if ([operation isEqualToString:@"sin"])
{
result = sin([self popOffStack:stack]);
}
else if ([operation isEqualToString:@"cos"])
{
result = cos([self popOffStack:stack]);
}
else if ([operation isEqualToString:@"√"])
{
result = sqrt([self popOffStack:stack]);
}
else if ([operation isEqualToString:@"π"])
{
result = M_PI;
}
}
return result;
}
+ (double)runProgram:(id)program
{
//Run program.
NSMutableArray *mutableCopyOfProgram;
if ([program isKindOfClass:[NSArray class]])
{
mutableCopyOfProgram = [program mutableCopy];
return [self popOffStack:mutableCopyOfProgram];
}
else return 0;
}
@end