gpt4 book ai didi

ios - 在第一个 IOS 项目 Xcode 4 中获取信号 SIGABRT

转载 作者:行者123 更新时间:2023-12-01 19:17:43 24 4
gpt4 key购买 nike

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

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

我的整个代码是这样的:

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”,但仍然出现错误。

最佳答案

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

Sample IB screen

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

更新:

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

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

  • 就像 David H 建议的那样,您应该使用小写的变量名。
  • 你可能应该有 @synthesize声明 @synthesize label = _label ,或者,如果您使用的是 Xcode 4.4 或更高版本,只需省略 @synthesize完全声明。 (我知道Interface Builder 可以为您生成一个简单的@synthesize 语句,但最好的做法是@synthesize 使用唯一的实例变量名称,这样您就不会意外地将实例变量与属性混淆。)
  • 您应该在 init 中引用您的实例变量和 dealloc正如 Born Survivor 所指出的,方法(即带有前导下划线的变量名)和其他地方使用属性(带有前导 self. )。
  • 您可能应该省略实例变量并让 @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

    关于ios - 在第一个 IOS 项目 Xcode 4 中获取信号 SIGABRT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12090093/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com