objective-c中使用的具体实现在哪里?
Where is the specific implementation used in objective-c?
大家好,我是 objective c 的新手。以下代码不是我的。我只是想了解它是如何工作的。我有一个 ViewController,在 .h 文件中有这个 属性。
@property (nullable, nonatomic, copy) dispatch_block_t logHandler;
在 .m 文件中,使用以下代码按下按钮时将调用 logHandler。
- (IBAction)login:(id)sender {
if (nil != self.logHandler) {
self.logHandler();
}
}
然后调用存在于另一个 class NSObject 文件中的 logHandler
在 .h 文件中
@interface LogFlow : NSObject<TheFlowController>
@end
在 .m 文件中
- (UIViewController *)rootViewController {
LogViewController *viewController = LogViewController.newInstance;
viewController.logHandler = ^{
UIViewController *logController = [self startNewLogFlow];
[self.navigationController pushViewController:logController animated:YES];
};
return viewController;
}
我不明白为什么 logHandler 存在于另一个 class 中以及为什么从这个特定的 class 中调用它,以及如何从另一个 class 中调用此代码没有使用任何进口?我试图了解何时使用这种实现以及如何使用它。任何帮助表示赞赏。
您在 rootViewController
中看到的结构:
^{
UIViewController *logController = [self startNewLogFlow];
[self.navigationController pushViewController:logController animated:YES];
};
这就是 Objective-C 中所谓的“块”。您可能会在其他语言中找到对它的其他引用,称为“匿名函数”或“闭包”。这些名称也适用于此。
这将创建一个只是函数的对象,但该函数没有名称。您还可以将未命名的函数分配给变量并从变量中调用它——这就是这里发生的事情。匿名函数,块,被分配给 viewController
对象的 logHandler
实例变量。稍后一些其他代码可以通过变量调用该函数,如您在 login:
示例中所见。
这是一个更简单的块 Objective-C:
int squareFunction(int x) {
return x * x;
}
void playWithSquares(void);
void playWithSquares(void) {
int nine = squareFunction(3);
int alsoNine = (^(int x){
return x * x;
})(3);
}
squareFunction
的声明创建了一个计算两个整数平方的命名函数。
您还看到了表达式:
^(int x){
return x * x;
};
这还会创建一个计算整数平方的函数,但不会将该函数绑定到名称。由于它没有名称,我们通过将其包装在括号中然后将参数传递给它来立即调用该函数 (<anonymous function expression>)(3)
我们可以将匿名函数存储在一个变量中:
typedef int (^SquaresBlock)(int);
SquaresBlock myBlock = ^(int x){
return x * x;
};
稍后使用 squaresBlock(3)
调用它
块在 Cocoa 对 Objective-C 的使用中非常重要,因此您应该了解更多有关它们的信息。
大家好,我是 objective c 的新手。以下代码不是我的。我只是想了解它是如何工作的。我有一个 ViewController,在 .h 文件中有这个 属性。
@property (nullable, nonatomic, copy) dispatch_block_t logHandler;
在 .m 文件中,使用以下代码按下按钮时将调用 logHandler。
- (IBAction)login:(id)sender {
if (nil != self.logHandler) {
self.logHandler();
}
}
然后调用存在于另一个 class NSObject 文件中的 logHandler
在 .h 文件中
@interface LogFlow : NSObject<TheFlowController>
@end
在 .m 文件中
- (UIViewController *)rootViewController {
LogViewController *viewController = LogViewController.newInstance;
viewController.logHandler = ^{
UIViewController *logController = [self startNewLogFlow];
[self.navigationController pushViewController:logController animated:YES];
};
return viewController;
}
我不明白为什么 logHandler 存在于另一个 class 中以及为什么从这个特定的 class 中调用它,以及如何从另一个 class 中调用此代码没有使用任何进口?我试图了解何时使用这种实现以及如何使用它。任何帮助表示赞赏。
您在 rootViewController
中看到的结构:
^{
UIViewController *logController = [self startNewLogFlow];
[self.navigationController pushViewController:logController animated:YES];
};
这就是 Objective-C 中所谓的“块”。您可能会在其他语言中找到对它的其他引用,称为“匿名函数”或“闭包”。这些名称也适用于此。
这将创建一个只是函数的对象,但该函数没有名称。您还可以将未命名的函数分配给变量并从变量中调用它——这就是这里发生的事情。匿名函数,块,被分配给 viewController
对象的 logHandler
实例变量。稍后一些其他代码可以通过变量调用该函数,如您在 login:
示例中所见。
这是一个更简单的块 Objective-C:
int squareFunction(int x) {
return x * x;
}
void playWithSquares(void);
void playWithSquares(void) {
int nine = squareFunction(3);
int alsoNine = (^(int x){
return x * x;
})(3);
}
squareFunction
的声明创建了一个计算两个整数平方的命名函数。
您还看到了表达式:
^(int x){
return x * x;
};
这还会创建一个计算整数平方的函数,但不会将该函数绑定到名称。由于它没有名称,我们通过将其包装在括号中然后将参数传递给它来立即调用该函数 (<anonymous function expression>)(3)
我们可以将匿名函数存储在一个变量中:
typedef int (^SquaresBlock)(int);
SquaresBlock myBlock = ^(int x){
return x * x;
};
稍后使用 squaresBlock(3)
块在 Cocoa 对 Objective-C 的使用中非常重要,因此您应该了解更多有关它们的信息。