在xcode中调用同一个类的方法

时间:2015-01-20 07:07:43

标签: ios objective-c xcode6

我想调用一个方法“shows()”,但为什么我得到的错误是“期望的标识符或(”和“使用未声明的标识符自我”

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

3 个答案:

答案 0 :(得分:3)

您无法在类范围内调用[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...

}

答案 1 :(得分:1)

您无法在外面调用此方法。你可以在任何其他方法中调用它。就像你可以在ViewDidLoad中调用这个方法一样。

 - (void)viewDidLoad { 
       [super viewDidLoad];    
       [self shows];   
 }

答案 2 :(得分:0)

你必须将代码放在另一个方法中,它不能由它自己保持开放。

将其放入viewDidLoad:方法或其他地方