【问题标题】:Works but I am not sure why, objective c有效,但我不确定为什么,目标 c
【发布时间】:2012-07-21 05:15:25
【问题描述】:

我正在制作一个计算器,并且有两个 MVC 用于数学部分,称为 CalculatorFirstViewController 和一个名为 CalculatorBrain 的类。图形部分的另一个称为 GraphViewController。

在 CalculatorController 中,我使用 mutableArray 作为计算器堆栈,并通过 segue 将其传递给图形视图控制器。 GraphView 的属性称为 graphingPoints。之后通过drawrect并调用“programToGraph”方法,该方法将创建一个点数组来绘制图形。让我感到困惑的是,我在“programGraph”中调用了一个方法“runProgram:usingVariableValues”,尽管“runProgram”只在 CalculatorBrain 中声明,这是一个单独的目标文件。为什么这个方法调用有效?

@interface CalculatorFirstViewController ()
@property (nonatomic, strong) CalculatorBrain *brain;
@end

@implementation CalculatorFirstViewController
@synthesize brain = _brain;

- (CalculatorBrain*) brain{
   if(!_brain) _brain = [[CalculatorBrain alloc] init];
   return _brain;
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

   if([segue.identifier isEqualToString:@"Graph"])
   //The call [self.brain program] returns the CalculatorStack
    [segue.destinationViewController setGraphingPoint:[self.brain program]];
  }

这里是RunProgram的声明,在Calculator MVC中使用的CalculatorBrain Object中声明。它是一个类方法。它所做的只是返回在堆栈上执行操作的值。

+ (double)runProgram:(id)program usingVariableValues:(NSDictionary*)variableValues;

这是 graphViewController 代码。

-(void) setGraphingPoint:(NSMutableArray*) graphingPoint{

if(_graphingPoint == nil) _graphingPoint = [[NSMutableArray alloc] init ];
    _graphingPoint = graphingPoint;
    //this will call drawrect
    [self.graphingView setNeedsDisplay];
}


-(id) programToGraph:(GraphingView *)sender{

    CGPoint graphPoint;
    NSMutableArray *pointValues = [[NSMutableArray alloc] init];

    for( int x =0;x<5; x++)
    {
        NSDictionary* xValue = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:x] forKey:@"X"];
        graphPoint.x =x;

     //This is the main part I dont get, If calculatorBrain is a seperate object file 
     //and I didn't import it, how am I able to call the method by just saying 
     //CalculatorBrain as the receiver? 

        graphPoint.y = [CalculatorBrain runProgram: self.graphingPoint usingVariableValues:xValue];

        [pointValues addObject:[NSValue valueWithCGPoint:graphPoint]];

}

return pointValues;

}

那么,我没有导入 CalculatorBrain 文件,也没有通过 segue 将它传递给另一个控制器,为什么我可以调用 runProgram?

【问题讨论】:

  • 你确实意识到你必须在 m 或 h 中导入这个东西,因为你可以访问 CalculatorBrain 类。或者你有一个对它的前向引用(@class),但是 CLANG 会抱怨它。
  • 我没有将任何 CalculatorFiles 导入 graphingController 但我没有将 CalculatorController 导入 CalculatorControlelr 以便能够调用 segue。我认为你必须保持 MVC 分开
  • 你有没有@class 什么?
  • 我使用@class 声明了一个协议来创建一个委托,然后让我的图形对象使用该协议来调用 ProgramToGraph,我没有在控制器中使用它

标签: objective-c xcode properties methods segue


【解决方案1】:

Objective-c 允许在不需要声明的情况下调用对象上的选择器(方法)。这是因为实际调用是在运行时查找的。这允许您的代码具有在其他库中实现的类的扩展方法,因为调用是在需要时在运行时找到的,而不是在编译时。

因此,您的调用在运行时被查找并且工作正常,因为该方法存在。在这种情况下,编译器应该给出一个警告,在编译时没有找到该方法,但仍然会成功编译。

编辑:您仍然需要使用@class 转发声明类本身。

【讨论】:

  • setter 和 getter 不是那些技术上的方法呢?如果是这样,如果我仍然可以在任何我想要的地方调用它,为什么我需要通过 segue 传递信息?
  • “Objective-c 允许在不需要声明的情况下调用对象上的选择器(方法)”绝对不正确。如果您尝试在没有-performSelector: 的情况下进行远程操作,编译器会一直抱怨到 timbuktu
  • 如果我将 GraphingViewController 导入到 CalculatorController 中以调用 segue 中的设置器,这是否允许 graphingViewController 使用 CalculatorViewController 中的方法?
猜你喜欢
  • 2010-12-13
  • 1970-01-01
  • 1970-01-01
  • 2015-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-23
  • 2014-06-11
相关资源
最近更新 更多