【问题标题】:NSLog not working when I make a method call in objective c当我在目标 c 中进行方法调用时,NSLog 不起作用
【发布时间】:2013-04-30 01:27:26
【问题描述】:

我正在开发我的第一个 iOS 应用。我确定我在这里做错了什么,但我似乎无法找出问题所在。

我有一个包含一些 IBActions 的应用程序,它做两件事:

1) 通过 NSLog 输出一些简单的文本(让我知道操作有效) - 这可以正常工作(它通过 NSLog 输出我的文本)

2) 调用自定义类中的方法,该方法也应输出 NSLog 语句。 - 这不起作用(没有通过 NSLog 输出文本)

我还在为在哪里创建我的类的实例以便在我的代码中的其他地方可以访问它们而苦苦挣扎。

代码如下:

//
//  ViewController.h
//  OrcAndPie
//
//  Created by me on 4/27/13.
//  Copyright (c) 2013 me. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "Player.h"
#import "Orc.h"

@interface ViewController : UIViewController {
    // Variables that you want to access globally go here?
    Player *wizard;
    Orc *grunty;

}

-(IBAction)takePie:(id)sender;
-(IBAction)castFireball:(id)sender;
-(IBAction)attack:(id)sender;



@end

和

//
//  ViewController.m
//  OrcAndPie
//
//  Created by me on 4/27/13.
//  Copyright (c) 2013 me. All rights reserved.
//

#import "ViewController.h"
#import "Player.h"

@interface ViewController ()

@end

@implementation ViewController

-(IBAction)takePie:(id)sender{
    // Player attempts to take the pie
    // If Orc's health is > 0, don't let the player take the pie
    // If the Orc's health is <= zero, let the player take the pie
    NSLog(@"IBAction - player attempted to take the pie.");
    [wizard takePie];

}

-(IBAction)castFireball:(id)sender{
    NSLog(@"IBAction - player cast fireball.");
    [wizard castFireball];
}
-(IBAction)attack:(id)sender{
    NSLog(@"IBAction - player attacked.");
    [wizard attackOrc];

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    Player *wizard = [[Player alloc]init];
    Orc *grunty = [[Orc alloc]init];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

和

//
//  Player.h
//  OrcAndPie
//
//  Created by me on 4/27/13.
//  Copyright (c) 2013 me. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Player : NSObject

@property int maxHealth;
@property int currentHealth;
@property int armorClass;
@property int meleeToHitModifier;


-(void)setHeathBar;
-(void)attackOrc;
-(void)castFireball;
-(void)takePie;


@end

和

//
//  Player.m
//  OrcAndPie
//
//  Created by me on 4/27/13.
//  Copyright (c) 2013 me. All rights reserved.
//

#import "Player.h"

@implementation Player

@synthesize maxHealth;
@synthesize currentHealth;
@synthesize armorClass;
@synthesize meleeToHitModifier;

-(void)setHealthBar {
    NSLog(@"Player health is 15");
    return;

}

-(void)attackOrc {
    // roll a d20
    // add the "to hit" modifier
    // compare the result to the orc's armor class
    NSLog(@"Player - Player attached the Orc");
    return;

}
-(void)castFireball{
    NSLog(@"Player - Player casts Fireball");
    return;

}
-(void)takePie{
    NSLog(@"Player - Player attempts to take pie");
    return;

}

@end

注意:我还定义了一个 Orc 类,但它看起来与显示的播放器类完全相同。

提前感谢您的帮助!

-- 艾迪

【问题讨论】:

  • 您是否在代码中安装了断点,以确保您到达您认为您要去的地方?
  • (请注意,上面没有初始化向导实例变量的代码,并且使用 nil 对象指针进行调用在 Objective-C 中是无操作的。)
  • 我在课堂上设置了一些断点,但似乎没有到达。
  • 你没有初始化wizard(即使你认为你是)。
  • 我想在一个有意义的地方初始化向导,这样所有的 IBAction 都可以调用正确的方法。我应该在哪里初始化向导?

标签: ios xcode nslog


【解决方案1】:

您的 ViewController.h(您应该使用更独特的名称来称呼它,仅供参考)应该如下所示:

//
//  ViewController.h
//  OrcAndPie
//
//  Created by me on 4/27/13.
//  Copyright (c) 2013 me. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "Player.h"
#import "Orc.h"

@interface ViewController : UIViewController

@property Player *wizard;
@property Orc *grunty;

-(IBAction)takePie:(id)sender;
-(IBAction)castFireball:(id)sender;
-(IBAction)attack:(id)sender;

@end

然后在您的 ViewController.m 中,您的 viewDidLoadMethod 应该如下所示:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.wizard = [[Player alloc]init];
    self.grunty = [[Orc alloc]init];

}

这至少应该确保您有一个可以调用方法的 Player 实例化实例,因此应该可以访问您的 NSLog。

【讨论】:

  • 终于向下滚动页面看到你的答案。谢谢,给我几分钟试试看。
  • 我应该补充一点,当您现在在 ViewController.m 中引用您的向导时,您应该将其称为 self.wizard,例如 [self.wizard castFireball]。
  • 所以,一切顺利,但现在我的具有[wizard takePie]; 等的 IBActions 抛出一个错误:`Use of undeclared identifier 'wizard';你的意思是“_wizard”吗? - 哈,刚刚看到你上面的评论,nvm。
  • 嘿。乐意效劳。当您刚接触 iOS 开发时,它看起来完全是随机的。
  • 不需要使用属性。 OP 使用实例变量的原始方案应该可以工作,如果他只初始化了正确的变量。
猜你喜欢
  • 2012-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多