草庐IT

ios - 由于我的 'NSInvalidArgumentException' 中的未捕获异常 "game"而终止应用程序

coder 2024-01-24 原文

我正在关注在线类(class)并获得库存,因为即使按照步骤(或者我可能错过了一些?),我也会出错。我在谷歌和这里进行了几次搜索,但由于我是 IOS 开发的新手,即使有其他人的回答,我也能解决我的问题..

这里是错误:

2015-06-12 23:03:17.477 Pirate Game[6511:1193126] -[RBTile setWeapon:]: unrecognized selector sent to instance 0x78830dc0
2015-06-12 23:03:17.481 Pirate Game[6511:1193126] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[RBTile setWeapon:]: unrecognized selector sent to instance 0x78830dc0'
*** First throw call stack:
(
0   CoreFoundation                      0x00874746 __exceptionPreprocess + 182
1   libobjc.A.dylib                     0x004fda97 objc_exception_throw + 44
2   CoreFoundation                      0x0087c705 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
3   CoreFoundation                      0x007c3287 ___forwarding___ + 1047
4   CoreFoundation                      0x007c2e4e _CF_forwarding_prep_0 + 14
5   Pirate Game                         0x00014a94 -[RBFactory tiles] + 388
6   Pirate Game                         0x00011c6f -[ViewController viewDidLoad] + 143
7   UIKit                               0x00d97da4 -[UIViewController loadViewIfRequired] + 771
8   UIKit                               0x00d98095 -[UIViewController view] + 35
9   UIKit                               0x00c89e85 -[UIWindow addRootViewControllerViewIfPossible] + 66
10  UIKit                               0x00c8a34c -[UIWindow _setHidden:forced:] + 287
11  UIKit                               0x00c8a648 -[UIWindow _orderFrontWithoutMakingKey] + 49
12  UIKit                               0x00c989b6 -[UIWindow makeKeyAndVisible] + 80
13  UIKit                               0x00c2ded8 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 3217
14  UIKit                               0x00c31422 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1639
15  UIKit                               0x00c4a93e __84-[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:]_block_invoke + 59
16  UIKit                               0x00c3004a -[UIApplication workspaceDidEndTransaction:] + 155
17  FrontBoardServices                  0x031d6c9e __37-[FBSWorkspace clientEndTransaction:]_block_invoke_2 + 71
18  FrontBoardServices                  0x031d672f __40-[FBSWorkspace _performDelegateCallOut:]_block_invoke + 54
19  FrontBoardServices                  0x031e8d7c __31-[FBSSerialQueue performAsync:]_block_invoke_2 + 30
20  CoreFoundation                      0x00796050 __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 16
21  CoreFoundation                      0x0078b963 __CFRunLoopDoBlocks + 195
22  CoreFoundation                      0x0078b7bb __CFRunLoopRun + 2715
23  CoreFoundation                      0x0078aa5b CFRunLoopRunSpecific + 443
24  CoreFoundation                      0x0078a88b CFRunLoopRunInMode + 123
25  UIKit                               0x00c2fa02 -[UIApplication _run] + 571
26  UIKit                               0x00c33106 UIApplicationMain + 1526
27  Pirate Game                         0x000148da main + 138
28  libdyld.dylib                       0x02c00ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

所以我可以从我的搜索中了解到,我正在尝试发送不同类型的东西。我对所有 3 个自定义类都有同样的问题。

//
//  RBFactory.m
//  Pirate Game
//
//  Created by Richard Berube on 2015-06-06.
//  Copyright (c) 2015 Richard Berube. All rights reserved.
//

#import "RBFactory.h"
#import "RBTile.h"

@implementation RBFactory

-(NSArray *)tiles{


    RBTile *tile1 = [[RBTile alloc] init];
    tile1.story = @"Captain, we need a fearless leader such as you to undertake a voyage. You must stop the evil pirate Boss before he steals any more plunder. Would you like a blunted sword to get started?";
    tile1.backgroundImage = [UIImage imageNamed:@"PirateStart.png"];
    CCWeapon *bluntedSword = [[CCWeapon alloc]init];
    bluntedSword.name = @"Blunted sword";
    bluntedSword.damage = 12;
    NSLog(@"%@", bluntedSword);
    tile1.weapon = bluntedSword;
    tile1.actionButtonName =@"Take the sword";

我测试了断点和崩溃发生

tile1.weapon = bluntedSword;

NSLog 返回:

2015-06-12 23:03:14.602 Pirate Game[6511:1193126] <CCWeapon: 0x786a6f50>

这是瓷砖类

//
//  RBTile.h
//  Pirate Game
//
//  Created by Richard Berube on 2015-06-06.
//  Copyright (c) 2015 Richard Berube. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "CCWeapon.h"
#import "CCArmor.h"

@interface RBTile : NSObject

@property (nonatomic, strong) NSString *story;
@property (strong, nonatomic) UIImage *backgroundImage;
@property (nonatomic, strong) NSString *actionButtonName;
@property (strong, nonatomic) CCWeapon *weapon;
@property (strong, nonatomic) CCArmor *armor;
@property (nonatomic) int healthEffect;

@end

.m 文件为空

//
//  RBTile.m
//  Pirate Game
//
//  Created by Richard Berube on 2015-06-06.
//  Copyright (c) 2015 Richard Berube. All rights reserved.
//

#import "RBTile.h"

@implementation RBTile

@end

最后一个是自定义类,实际上是其中一个,因为所有 3 个都导致相同的问题..

//
//  CCWeapon.h
//  Pirate Game
//
//  Created by Richard Berube on 2015-06-10.
//  Copyright (c) 2015 Richard Berube. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface CCWeapon : NSObject

@property (strong,nonatomic) NSString *name;
@property (nonatomic) int damage;

@end

如果能提供一点帮助,我们将不胜感激!我现在有货 2 天了。我可以重新开始所有视频(在线类(class)),但这不会教我如何调试 ..

也许最后有用的信息可能是我使用了 Xcode 6.3.1

谢谢!

更新 1:最终项目是这个 https://github.com/codecoalition/Pirate-Adventure-Assignment/tree/master/Pirate%20Adventure

我的RBtile是github项目中的CCtile

更新 2:这是我的 View Controller

//
//  ViewController.m
//  Pirate Game
//
//  Created by Richard Berube on 2015-06-06.
//  Copyright (c) 2015 Richard Berube. All rights reserved.
//

#import "ViewController.h"
#import "RBFactory.h"
#import "RBTile.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    RBFactory *factory = [[RBFactory alloc]init];
    self.tiles = [factory tiles];
    self.character = [factory character];
    self.currentPoint = CGPointMake(0, 0);
    [self updateTile];
    [self updateButtons];
    [self updateCharacterStatsForArmor:nil withWeapons:nil withHealthEffect:0];
}

-(void)updateTile{
    RBTile *tileModel = [[self.tiles objectAtIndex:self.currentPoint.x] objectAtIndex:self.currentPoint.y];
    self.storyLabel.text = tileModel.story;
    self.backgroundImageView.image = tileModel.backgroundImage;
    self.healthLabel.text = [NSString stringWithFormat:@"%i", self.character.health];
    self.damageLabel.text = [NSString stringWithFormat:@"%i", self.character.damage];
    self.armorLabel.text = self.character.armor.name;
    self.weaponLabel.text = self.character.weapon.name;
    [self.actionButton setTitle:tileModel.actionButtonName forState:UIControlStateNormal];
}

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

- (IBAction)actionButtonPressed:(UIButton *)sender {

    RBTile *tile = [[self.tiles objectAtIndex:self.currentPoint.x] objectAtIndex:self.currentPoint.y];
    [self updateCharacterStatsForArmor:tile.armor withWeapons:tile.weapon withHealthEffect:tile.healthEffect];
    [self updateTile];
}

- (IBAction)northButtonPressed:(UIButton *)sender {

    self.currentPoint = CGPointMake(self.currentPoint.x, self.currentPoint.y +1);
    [self updateButtons];
    [self updateTile];
}

- (IBAction)southButtonPressed:(UIButton *)sender {

    self.currentPoint = CGPointMake(self.currentPoint.x, self.currentPoint.y -1);
    [self updateButtons];
    [self updateTile];

}

- (IBAction)eastButtonPressed:(UIButton *)sender {

    self.currentPoint = CGPointMake(self.currentPoint.x + 1, self.currentPoint.y);
    [self updateButtons];
    [self updateTile];
}

- (IBAction)westButtonPressed:(UIButton *)sender {

    self.currentPoint = CGPointMake(self.currentPoint.x - 1, self.currentPoint.y);
    [self updateButtons];
    [self updateTile];
}

- (IBAction)restartButtonPressed:(UIButton *)sender {
}

-(void)updateButtons{
    self.westButton.hidden = [self tilesExistAtPoint:CGPointMake(self.currentPoint.x -1, self.currentPoint.y)];
    self.eastButton.hidden = [self tilesExistAtPoint:CGPointMake(self.currentPoint.x+1, self.currentPoint.y)];
    self.northButton.hidden = [self tilesExistAtPoint:CGPointMake(self.currentPoint.x, self.currentPoint.y+1)];
    self.southButton.hidden = [self tilesExistAtPoint:CGPointMake(self.currentPoint.x, self.currentPoint.y-1)];
}

-(BOOL)tilesExistAtPoint:(CGPoint)point{
    if (point.y >= 0 && point.x >=0 && point.x < [self.tiles count] && point.y < [[self.tiles objectAtIndex:point.x] count]) {
        return NO;
    }
    else{
        return YES;
    }
}

-(void)updateCharacterStatsForArmor:(CCArmor *)armor withWeapons:(CCWeapon *)weapon withHealthEffect:(int)healtEffect{

    if (armor != nil ) {
        self.character.health = self.character.health - self.character.armor.health +armor.health;
        self.character.armor = armor;
    }
    else if (weapon != nil){
        self.character.damage = self.character.damage - self.character.weapon.damage + weapon.damage;
        self.character.weapon = weapon;
    }
    else if (healtEffect != 0){
        self.character.health = self.character.health + healtEffect;
    }
    else {
        self.character.health = self.character.health + self.character.armor.health;
        self.character.damage = self.character.damage + self.character.weapon.damage;
    }
}

@end

另一个更新:

我能够通过断点找到我的一些属性没有显示的问题。

实际上应该是这样的:

现在我知道,当我尝试传递 tile1.weapon = bluntedSword; 时,它不起作用,因为 tile1.weapon 不存在。但是如果你看一下我的 RBTile.h 我所有的属性(property)都在那里..

最佳答案

我正在学习相同的类(class),我的代码与您的相同,并且可以正常工作。 Tile.m 文件是空的。

在最近的 Xcode 版本中,synthesize 语句并不总是必需的。我认为,一些罕见的情况仍然需要它们。

我认为您在调试窗口中看不到其他属性的原因是创建它们的代码尚未运行。

尝试注释掉与 bluntedSword 相关的行,看看其他属性是否设置成功,或者它们是否也导致应用程序崩溃。

要尝试的另一件事是重新键入导致问题的行(而不是复制和粘贴)和相关行,我知道这听起来很愚蠢。有时这会奏效,因此值得一试。

祝你好运!

关于ios - 由于我的 'NSInvalidArgumentException' 中的未捕获异常 "game"而终止应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30814763/

有关ios - 由于我的 'NSInvalidArgumentException' 中的未捕获异常 "game"而终止应用程序的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby - 其他文件中的 Rake 任务 - 2

    我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时

  3. ruby-on-rails - Ruby net/ldap 模块中的内存泄漏 - 2

    作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代

  4. ruby - 在 Ruby 程序执行时阻止 Windows 7 PC 进入休眠状态 - 2

    我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0

  5. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  6. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  7. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  8. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  9. ruby - 如何指定 Rack 处理程序 - 2

    Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack

  10. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

随机推荐