【问题标题】:getting signal SIGABRT in first IOS project, Xcode 4在第一个 IOS 项目 Xcode 4 中获取信号 SIGABRT
【发布时间】:2012-08-18 21:04:42
【问题描述】:

我正在尝试在 IO 中创建我的第一个测试应用程序,但我所做的一切都会让我遇到这个错误。我正在使用 xcode 4.4。

该应用程序非常简单。它有一个按钮,当我按下它时,必须出现一个标签和一个图像视图。

我的整个代码是这样的:

ViewController.h


//
//  ViewController.h
//  helloWorld_04
//
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController{

    IBOutlet UILabel *label;
    IBOutlet UIImageView *Kant;
}
@property (nonatomic, retain) IBOutlet UILabel *label;
@property (nonatomic, retain) IBOutlet UIImageView *Kant;
- (IBAction)buttonGuess:(id)sender;

@end

还有我的实现文件:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize label,Kant;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)dealloc {
    [label release];
    [Kant release];
    [super dealloc];
}
- (IBAction)buttonGuess:(id)sender {
    label.text=@"Hello World i am back!";
    UIImage *imageSource=[UIImage imageNamed:@"kantStair.png"];
    Kant.image=imageSource;
}
@end

我的错误日志是这样的:

2012-08-23 13:38:50.030 helloWorld_04[537:c07] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<ViewController 0x6a5e1f0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key image.'
*** First throw call stack:
(0x14b2022 0xeb2cd6 0x14b1ee1 0x9c3022 0x934f6b 0x934edb 0x94fd50 0x23771a 0x14b3dea 0x141d7f1 0x23626e 0xdc1fc 0xdc779 0xdc99b 0x3b401 0x3b670 0x3b836 0x4272a 0x2d1b 0x13386 0x14274 0x23183 0x23c38 0x17634 0x139cef5 0x1486195 0x13eaff2 0x13e98da 0x13e8d84 0x13e8c9b 0x13c65 0x15626 0x2a22 0x2995)
terminate called throwing an exception(lldb) 

它让我在该行得到了那个信号:

//
//  main.m
//  helloWorld_04
//

//

#import <UIKit/UIKit.h>

#import "AppDelegate.h"

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

我在这里阅读了几个答案,但没有找到解决我的问题的方法,这就是为什么我提出了一个新问题。我认为这可能是连接问题,所以我自己做的是将我的标签和图像“darg and drop”给“file's owner”,但仍然出现错误。

【问题讨论】:

  • 试试看,self.label.text=@"Hello World i am back!";self.Kant.image=imageSource;
  • @BornSurvivor 虽然风格不错,但问题不大。 OP 语法是可以接受的,尽管你的更好。
  • @ghostrider 这个问题无疑是你如何在 Interface Builder 中连接各种控件的问题。看起来当你第一次在 Interface Builder 中连接东西时,你使用了一个名为 image 的控件,但现在显然使用了 Kant(David H 非常正确,它应该是小写的)。
  • @Rob:感谢您提供的信息。 :)

标签: ios xcode ios5 xcode4 sigabrt


【解决方案1】:

查看您的错误消息,您似乎在 Interface Builder 中有定义为 image 的内容,但不存在。例如,也许你有类似的东西:

但您没有image 属性。你曾经有过一个,也许已经将它重命名为Kant?如果您在 Interface Builder 中定义了类似的内容,请将其删除(通过点击我突出显示的插座旁边的“x”),然后将其再次链接到您的新控件。


更新:

您绝对应该修复上面的错误,希望上面的观察可以帮助您找到它。您的代码中没有错误,但问题无疑在于您的 Interface Builder 链接。

但是,话虽如此,如果你是一个新程序员,我希望你不介意一些不请自来的风格观察。显然,鉴于这是一个风格问题,这些都是可以争论的,但我认为这些都代表了既定的或新兴的 iOS 编码标准。无论如何,我将来可能会建议:

  1. 就像 David H 建议的那样,您应该使用小写的变量名。

  2. 您可能应该有 @synthesize 语句,或者说 @synthesize label = _label,或者,如果您使用 Xcode 4.4 或更高版本,则完全省略 @synthesize 语句。 (我知道 Interface Builder 可以为您生成一个简单的 @synthesize 语句,但最好的做法是使用唯一的实例变量名称 @synthesize,这样您就不会意外地将实例变量与属性混淆。)

  3. 您应该在 initdealloc 方法中引用您的实例变量(即带有前导下划线的变量名)并在其他地方使用属性(带有前导 self.),正如 Born Survivor 所指出的那样.

  4. 您可能应该省略实例变量并让@synthesize 语句为您执行这些操作(这样如果您打错字,就不会意外地得到两个实例变量)。

因此您的代码将变为:

//  ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

// note, no braces with instance variables defined
// once upon a time that was recommended by Apple, but no longer
// just let the following @property statements and the subsequent
// @synthesize statement generate the instance variables for you.

@property (retain, nonatomic) IBOutlet UIImageView *kant; // note the lower case "k"
@property (retain, nonatomic) IBOutlet UILabel *label;

- (IBAction)buttonGuess:(id)sender;

@end

//  ViewController.m

#import "ViewController.h"

@implementation ViewController

@synthesize label = _label;  // note the @synthesize statement let's us define what the instance variable name should be, _label in this case
@synthesize kant  = _kant;

- (void)viewDidUnload
{
    [self setKant:nil];
    [self setLabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)dealloc {
    [_kant release];  // note I'm now using the instance variables that begin with the underscore
    [_label release];
    [super dealloc];
}

- (IBAction)buttonGuess:(id)sender
{
    self.label.text = @"Hello World i am back!";  // note, I'm using the properties with the preceding "self."
    UIImage *imageSource = [UIImage imageNamed:@"kantStair.png"];
    self.kant.image = imageSource;
}

@end

【讨论】:

  • 确实如此。我已重命名我的文件。不仅感谢您的声明,还感谢所有这些信息。
【解决方案2】:

在 ObjectiveC 中,总是用小写字母命名变量很重要——因为当你合成像 "foo" 这样的变量时,你会得到 "-(id)foo;// getter" 和 "-(void)setFoo: (id)val;//setter".看看 setter 如何使用大写字母。因此,您应该做的第一件事是将“康德”重命名为“康德”。 [按照惯例,只有类的首字母大写,所以你可以帮助像我这样的其他人按照惯例阅读你的代码。]

所以你要做的第一件事就是将“Kant”更改为“kant”,然后返回界面构建器视图并将新命名的变量重新连接到 UIImageView。

如果这不能解决问题,则说明您已经连接了“kant”,或者发生了其他一些奇怪的事情 - 在设置图像之前添加这行代码:

NSLog(@"kant has class %@", NSStringFromClass([kant class]) );

让我们看看它到底是什么。

【讨论】:

  • 使用小写的变量名是很好的风格,但不会导致OP遇到的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-01-23
  • 2019-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-30
  • 2015-02-27
相关资源
最近更新 更多