从 xcode 中的同一个 class 调用方法
calling a method from same class in xcode
我想调用方法 "shows()" 但为什么我收到 "expected identifier or ( " 和 "use of undeclared identifier self"
的错误
ViewController.m
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
NSDictionary *inventory;
}
- (void)shows;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
inventory = @{@"Rahul":[NSNumber numberWithInt:11],
@"iOS":[NSNumber numberWithInt:22]};
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)shows
{
NSLog(@"%@",inventory);
}
[self shows];
@end
你必须把代码放在另一个方法中,它不能自己打开。
将其放入您的 viewDidLoad:
方法或其他地方
你不能在外面调用这个方法。您可以在另一个方法中调用它。就像你可以在 ViewDidLoad
.
中调用这个方法
- (void)viewDidLoad {
[super viewDidLoad];
[self shows];
}
您不能在 class 范围内调用 [view shows];
,您需要在方法中调用它,例如 viewDidLoad
像这样称呼它:
- (void)viewDidLoad {
[super viewDidLoad];
inventory = @{@"Rahul":[NSNumber numberWithInt:11],
@"iOS":[NSNumber numberWithInt:22]};
// Do any additional setup after loading the view, typically from a nib.
[self shows];//SHOWS call moved here...
}
我想调用方法 "shows()" 但为什么我收到 "expected identifier or ( " 和 "use of undeclared identifier self"
的错误ViewController.m
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
NSDictionary *inventory;
}
- (void)shows;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
inventory = @{@"Rahul":[NSNumber numberWithInt:11],
@"iOS":[NSNumber numberWithInt:22]};
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)shows
{
NSLog(@"%@",inventory);
}
[self shows];
@end
你必须把代码放在另一个方法中,它不能自己打开。
将其放入您的 viewDidLoad:
方法或其他地方
你不能在外面调用这个方法。您可以在另一个方法中调用它。就像你可以在 ViewDidLoad
.
- (void)viewDidLoad {
[super viewDidLoad];
[self shows];
}
您不能在 class 范围内调用 [view shows];
,您需要在方法中调用它,例如 viewDidLoad
像这样称呼它:
- (void)viewDidLoad {
[super viewDidLoad];
inventory = @{@"Rahul":[NSNumber numberWithInt:11],
@"iOS":[NSNumber numberWithInt:22]};
// Do any additional setup after loading the view, typically from a nib.
[self shows];//SHOWS call moved here...
}