xcode 不兼容的指针类型分配给 'UILabel *__weak' 到 'NSString *__strong'?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11486641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:53:59  来源:igfitidea点击:

incompatible pointer types assigning to 'UILabel *__weak' to 'NSString *__strong'?

iphoneobjective-ciosxcodestring

提问by Gabriel Molter

i've been doing a thing here using objective-c, but i'm a beginner in this language, and that's what i did:

我一直在这里使用objective-c做一件事,但我是这门语言的初学者,这就是我所做的:

there is a class, named "firstviewclass", that is in the control of my first view, in this view there is a textfield that the user puts a number. the textfield is in the firstviewclass.h named "setNumber". There is another class, named "secondviewclass" that is in control of the second view, in this view there is a label that is in the secondviewclass.h, and i want that this label recive the value that the user put in the textfield from the first view. my codes:

有一个名为“firstviewclass”的类,它在我的第一个视图的控制中,在这个视图中有一个用户输入数字的文本字段。文本字段位于名为“setNumber”的 firstviewclass.h 中。还有另一个名为“secondviewclass”的类,它控制着第二个视图,在这个视图中,secondviewclass.h 中有一个标签,我希望这个标签接收用户在文本字段中放置的值第一个观点。我的代码:

firstviewclass.h:

firstviewclass.h:

#import <UIKit/UIKit.h>

@interface firstviewclass : UIViewController

@property (strong, nonatomic) IBOutlet UITextField *setNumber;

- (IBAction)gotonextview:(id)sender;

 @end

secondviewclass.h:

第二视图类.h:

#import <UIKit/UIKit.h>
#import "firstviewclass.h"

@interface secondviewclass : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *labelthatrecivesthetextfieldvalue;

@end

secondviewclass.m:

secondviewclass.m:

#import "secondviewclass.h"
#import "firstviewclass.h"

@implementation secondviewclass
@synthesize labelthatrecivesthetextfieldvalue;


-(void)viewDidLoad{
    [super viewDidLoad];

    firstviewclass *object = [[firstviewclass alloc] init];

    NSString *string = [[NSString alloc] initWithFormat:@"%i", [object.setNumber.text intValue]];

    labelthatrecivesthetextfieldvalue = string; //here is where that error appears "incompatible pointer types assigning to 'UILabel *__weak' to 'NSString *__strong'"

}

@end

I've changed what you had told me to change, but when i test it on the simulator any number that i put in the textfield it appears in the label as 0... i really don't know what to do!

我已经更改了您告诉我要更改的内容,但是当我在模拟器上对其进行测试时,我在文本字段中输入的任何数字都会在标签中显示为 0...我真的不知道该怎么做!

回答by Rui Peres

It should be:

它应该是:

labelthatrecivesthetextfieldvalue.text = string;


Under the hood:

引擎盖下:

After your xib instantiated an UILabeland you get its reference using the IBOutletkeyword, you are then trying to use the UILabelpointer to point to a space in memory allocated and instantiated for a NSString, that's why you are having this problem. You probably also did get a warning about that assignment you tried to do:

在您的 xib 实例化 anUILabel并使用IBOutlet关键字获取其引用后,您将尝试使用UILabel指针指向为 a 分配和实例化的内存空间NSString,这就是您遇到此问题的原因。您可能也收到了有关您尝试执行的作业的警告:

labelthatrecivesthetextfieldvalue = string;

You should always try to fix warnings.

您应该始终尝试修复警告。

What you want:

你想要什么:

You simply want the UILabelto display the NSStringyou just created, and for that you should use the UILabel'stext property.

您只想UILabel显示NSString您刚刚创建的 ,为此您应该使用UILabel'stext 属性。

回答by bkbeachlabs

You are getting that error because you are pointing a label to a string. They are of different classes, so that's not allowed. What you really mean is that you want the text on your label to take the value of the string. So you need to set the text field of the label:

您收到该错误是因为您将标签指向字符串。他们是不同的阶级,所以这是不允许的。您真正的意思是您希望标签上的文本采用字符串的值。所以需要设置标签的文本字段:

labelthatrecivesthetextfieldvalue.text = string;

My apologies for my earlier, incorrect answer.

我为我之前的错误答案道歉。

Incidentally, you should also consider using camelCase for naming your variables. That is the convention for objective-c. ie call it labelThatRecievesTheTextFieldValueinstead. This doesnt affect the ability of your code to work, just convention.

顺便说一句,您还应该考虑使用驼峰命名法来命名您的变量。这是objective-c的约定。即labelThatRecievesTheTextFieldValue改为调用它。这不会影响您的代码工作的能力,只是约定。