可能重复:
分配给模型类中的属性的值在视图/控制器类中消失
这是在类型 y 的对象上找不到属性 x的后续行动,关于我得到的“在类型 y 的对象上找不到属性 x”错误。这是问题的更完整版本;我真的希望这是我的错误,而不是 XCode 中的错误,因为我对此很陌生,我依靠我得到的错误来帮助我学习。
问题出(IBAction)testPressed
在 CalculatorViewController.m(代码如下)中调用的方法中,该方法使用名为whatHappenedSinceLastClear
. 该属性在 CalculatorBrain.m 的接口中声明并在实现中合成,但 , 行中(IBAction)testPressed
的行NSMutableArray *program = brain.whatHappenedSinceLastClear;
产生错误property 'whatHappenedSinceLastClear' not found on object of type 'CalculatorBrain *'.
如果我在 CalculatorBrain.h 中声明 'whatHappenedSinceLastClear',错误就会消失,正如我在上一篇文章的答案中所建议的那样,但另一位海报指出它不应该有所作为,这只是一种解决方法。在查看stackoverflow之后,我尝试清理并尝试删除派生数据,但我仍然收到错误消息。
这是我的(非常未完成的)代码,其中一些我知道的方法在长度上是无关紧要的。
计算器视图控制器.h
#import <UIKit/UIKit.h>
@interface CalculatorViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *display;
@property (weak, nonatomic) IBOutlet UILabel *history;
@end
CalculatorViewController.m(剪掉了一些明显无关紧要的方法)
#import "CalculatorViewController.h"
#import "CalculatorBrain.h"
@interface CalculatorViewController ()
@property (nonatomic) BOOL userIsEnteringNumber;
@property (nonatomic) BOOL numberIsNegative;
@property (nonatomic,strong) CalculatorBrain *brain;
@property (nonatomic) NSArray *arrayOfDictionaries;
@property (nonatomic) NSDictionary *dictionary;
@end
@implementation CalculatorViewController
@synthesize display = _display;
@synthesize history = _history;
@synthesize userIsEnteringNumber = _userIsEnteringNumber;
@synthesize numberIsNegative;
@synthesize brain = _brain;
@synthesize arrayOfDictionaries;
@synthesize dictionary;
-(CalculatorBrain *)brain
{
if (!_brain) _brain = [[CalculatorBrain alloc] init];
return _brain;
}
- (IBAction)digitPressed:(UIButton *)sender
{
NSString *digit = sender.currentTitle;
//self.display.text = @"0";
//Enter digit if it wouldn't create a two-decimal-point case.
NSRange range = [self.display.text rangeOfString:@"."];
if (range.location==NSNotFound || (![digit isEqualToString:@"."]))
{
if (self.userIsEnteringNumber)
{
self.display.text = [self.display.text stringByAppendingString:digit];
self.history.text = [self.display.text stringByAppendingString:@" "];
}
else
{
self.history.text = [self.history.text stringByAppendingString:digit];
self.history.text = [self.history.text stringByAppendingString:@" "];
if (![sender.currentTitle isEqualToString:@"."])
{
self.display.text = digit;
}
else
{
}
self.userIsEnteringNumber = YES;
}
}
}
- (IBAction)operationPressed:(UIButton *)sender
{
NSDictionary *dict = [self loadDictionary];
if (self.userIsEnteringNumber) [self enterPressed];
NSString *operation = sender.currentTitle;
double result = [self.brain performOperation:operation];
self.display.text = [NSString stringWithFormat:@"%g",result];
self.history.text = [self.history.text stringByAppendingString:sender.currentTitle];
self.history.text = [self.history.text stringByAppendingString:@" = "];
[self.brain pushOperand:operation withDictionary:dict];
}
-(NSDictionary *)loadDictionary
{
NSDictionary *dict = self.dictionary;
NSArray *variableArray = [[NSArray alloc] init];
variableArray = [NSArray arrayWithObjects:@"x",@"y",@"z",@"foo",@"π",nil];
NSArray *valueArray = [[NSArray alloc] init];
valueArray = [NSArray arrayWithObjects:[NSNumber numberWithDouble:3],[NSNumber numberWithDouble:4.1],[NSNumber numberWithDouble:-6],[NSNumber numberWithDouble:8.2],[NSNumber numberWithDouble:3.14159], nil];
dict = [[NSDictionary alloc] initWithObjects:valueArray forKeys:variableArray];
return dict;
}
- (IBAction)variablePressed:(UIButton *)sender
{
NSDictionary *dict = [self loadDictionary];
NSString *var = sender.currentTitle;
[self.brain convertVariable:var usingDictionary:dict];
self.display.text = [NSString stringWithFormat:@"%@",var];
self.history.text = [self.history.text stringByAppendingString:sender.currentTitle];
[self.brain pushOperand:[dict objectForKey:var] withDictionary:dict];
}
-(IBAction)testPressed:(id)sender
{
CalculatorBrain *brain = self.brain;
brain = [[CalculatorBrain alloc] init];
NSMutableArray *program = brain.whatHappenedSinceLastClear;
double blah = [brain runProgram:program];
self.history.text=@"";
self.history.text=[self.history.text stringByAppendingFormat:@"%f",blah];
NSLog(@"%f",blah);
}
- (IBAction)enterPressed
{
[self.brain pushOperand:[NSNumber numberWithDouble:[self.display.text doubleValue]] withDictionary:self.dictionary];
self.userIsEnteringNumber = NO;
}
@end
CalculatorBrain.h
#import <Foundation/Foundation.h>
@interface CalculatorBrain : NSObject
-(void)pushOperand:(id)operand withDictionary:(NSDictionary *)dictionary;
-(double)performOperation:(NSString *)operation;
-(void)clearStack;
-(void)deleteLastDigit;
-(double)convertVariable:(NSString *)variable usingDictionary:dictionaryContainingValuesYouWantToUse;
@property (readonly) id thisMeansAProgramCanGetAndSet;
+ (NSSet *)variablesUsedInProgram:(id)whatHappenedSinceLastClear;
-(double)runProgram:(id)whatHappenedSinceLastClear;
- (double)runProgram:(id)whatHappenedSinceLastClear usingVariableValues:(NSDictionary *)variableValues;
-(NSString *)descriptionOfProgram:(id)whateverSpecificProgramYouNeedToDescribe;
@end
和 CalculatorBrain.m(同样被剪断了明显无关紧要的方法)。
#import "CalculatorBrain.h"
@interface CalculatorBrain ()
-(id)copyOfWhatHappenedSinceLastClear;
@property (nonatomic, strong) NSMutableArray *whatHappenedSinceLastClear;
-(double)popOffProgramStack:(NSMutableArray *)particularStackYouNeedToPopOff;
-(double)runProgram:(id)whateverSpecificProgramYouNeedToRun;
-(NSString *)descriptionOfProgram:(id)whateverSpecificProgramYouNeedToDescribe;
@end
@implementation CalculatorBrain
@synthesize whatHappenedSinceLastClear = _whatHappenedSinceLastClear;
@synthesize thisMeansAProgramCanGetAndSet = _thisMeansAProgramCanGetAndSet;
-(id)copyOfWhatHappenedSinceLastClear
{
return [self.whatHappenedSinceLastClear copy];
}
-(double)runProgram:(id)whatHappenedSinceLastClear
{
NSMutableArray *mutableCopyOfWhatHappenedSinceLastClear;
if ([whatHappenedSinceLastClear isKindOfClass:[NSArray class]]) {
mutableCopyOfWhatHappenedSinceLastClear = [whatHappenedSinceLastClear mutableCopy];
}
return [self popOffProgramStack:mutableCopyOfWhatHappenedSinceLastClear];
}
- (double)runProgram:(id)whatHappenedSinceLastClear usingVariableValues:(NSDictionary *)variableValues
{
return 0;
}
- (double)popOffProgramStack:(NSMutableArray *)particularStackYouNeedToPopOff
{
double result = 0;
id topOfStack = [particularStackYouNeedToPopOff lastObject];
if (topOfStack) [particularStackYouNeedToPopOff removeLastObject];
if ([topOfStack isKindOfClass:[NSNumber class]])
{
result = [topOfStack doubleValue];
}
else if ([topOfStack isKindOfClass:[NSString class]])
{
NSString *operation = topOfStack;
if ([operation isEqualToString:@"+"]) {
result = [self popOffProgramStack:particularStackYouNeedToPopOff] +
[self popOffProgramStack:particularStackYouNeedToPopOff];
} else if ([@"*" isEqualToString:operation]) {
result = [self popOffProgramStack:particularStackYouNeedToPopOff] *
[self popOffProgramStack:particularStackYouNeedToPopOff];
} else if ([operation isEqualToString:@"-"]) {
double subtrahend = [self popOffProgramStack:particularStackYouNeedToPopOff];
result = [self popOffProgramStack:particularStackYouNeedToPopOff] - subtrahend;
} else if ([operation isEqualToString:@"/"]) {
double divisor = [self popOffProgramStack:particularStackYouNeedToPopOff];
if (divisor) result = [self popOffProgramStack:particularStackYouNeedToPopOff] / divisor;
}
}
return result;
}
-(NSMutableArray *)whatHappenedSinceLastClear
{
if(!_whatHappenedSinceLastClear) _whatHappenedSinceLastClear = [[NSMutableArray alloc] init];
return _whatHappenedSinceLastClear;
}
-(void)clearStack
{
self.whatHappenedSinceLastClear = [[NSMutableArray alloc] init];
}
-(void)pushOperand:(id)operand withDictionary:(NSDictionary *)dictionary
{
NSArray *operations = [[NSArray alloc] initWithObjects:@"+",@"-",@"/",@"*",@"sin",@"cos",@"√", nil];
NSNumber *operandAsNumberObject;
NSString *operationAsObject;
if (![operand isKindOfClass:[NSString class]])
{
operandAsNumberObject = operand;
[self.whatHappenedSinceLastClear addObject:operandAsNumberObject];
}
else if ([operations containsObject:operand])
{
operationAsObject = operand;
[self.whatHappenedSinceLastClear addObject:operationAsObject];
}
else
{
operandAsNumberObject = [dictionary objectForKey:operand];
[self.whatHappenedSinceLastClear addObject:operandAsNumberObject];
}
}
-(double)popOperand
{
NSNumber *operandAsObject = [self.whatHappenedSinceLastClear lastObject];
if (operandAsObject) [self.whatHappenedSinceLastClear removeLastObject];
return [operandAsObject doubleValue];
}
- (double)performOperation:(NSString *)operation
{
[self.whatHappenedSinceLastClear addObject:operation];
return [self runProgram:self.copyOfWhatHappenedSinceLastClear];
//when it was a class method code was: return [[self class] runProgram:self.copyetc.]
}
@end
那么有没有人看到什么(手指交叉,这是一个基本的,愚蠢的错误)我做错了可以解释发生了什么?