xcode 警告 - 不兼容的指针类型用“UITextField”类型的表达式初始化“NSString *__strong”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9775269/
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-14 23:42:42  来源:igfitidea点击:

Warning - incompatible pointer types initializing 'NSString *__strong' with an expression of type 'UITextField'

objective-cxcodeios5

提问by rock

Hi guys, I got this code and XCode warns me of "incompatible pointer types initializing NSString *__strong with an expression of type UITextField".

大家好,我得到了这段代码,XCode 警告我“不兼容的指针类型用 UITextField 类型的表达式初始化 NSString *__strong”。

NSString *name = (UITextField *)searchText.text;

but this one is fine

但这个很好

NSString *name2 = [(UITextField *)searchText text];

and this one is fine too

这个也不错

NSString *name3 = [(UITextField *)searchText.text mutableCopy];

I have two questions:

我有两个问题:

  1. I am confused with obj.*and [obj *]
  2. Why is the "mutableCopy" correct is this case?
  1. 我很困惑obj.*[obj *]
  2. 为什么在这种情况下“mutableCopy”是正确的?

I don't know how to search in Apple developer documentation for these questions; please help.

我不知道如何在 Apple 开发人员文档中搜索这些问题;请帮忙。

回答by Joachim Isaksson

In the first version, due to operator precedence, you're casting searchText.textto a UITextField*, what you want to do is probably to cast searchText;

在第一个版本中,由于运算符优先级,您要强制转换searchText.text为 a UITextField*,您想要做的可能是强制转换searchText;

NSString *name = ((UITextField *)searchText).text;

In the second version you don't have the dot, so the compiler understands your cast to be casting searchText to UITextFieldand send the text message to it. In other words, exactly right.

在第二个版本中,您没有点,因此编译器将您的强制转换理解为将 searchText 强制转换为UITextField并将文本消息发送给它。换句话说,完全正确。

The last case is a bit tricky since it involves both runtime and compile time. As I understand it;

最后一种情况有点棘手,因为它涉及运行时和编译时间。据我了解;

  • You cast searchText.textto a UITextField*. The runtime still knows that the object is an NSString and the mutableCopymessage that exists on both will go to the correct method [NSString mutableCopy] anyway and create/return a mutable copy of the NSString so runtime it works out ok.
  • Since mutableCopyreturns id(referencing a NSMutableString), the assignment to an NSString is ok by the compiler (id can be assigned to anything), so compile time it's ok.
  • 你投射searchText.text到一个UITextField*. 运行时仍然知道该对象是一个 NSString 并且mutableCopy存在于两者上的消息无论如何都会转到正确的方法 [NSString mutableCopy] 并创建/返回 NSString 的可变副本,因此运行时它可以正常工作。
  • 由于mutableCopy返回id(引用 NSMutableString),编译器对 NSString 的赋值是可以的(id 可以赋值给任何东西),所以编译时就可以了。

A question, what is searchTextoriginally? That the last version compiled without warning indicates that it's already an UITextField*, or at least a type that can take the textmessage. If so, you should be able to just do;

一个问题,searchText原来是什么?没有警告编译的最后一个版本表明它已经是一个UITextField*,或者至少是一个可以接受text消息的类型。如果是这样,你应该能够做到;

NSString *name3 = [searchText.text mutableCopy];

回答by Cthutu

In the second and third examples, the cast just operates on searchText. So with these you are sending a message to a UITextField object.

在第二个和第三个示例中,演员表只对 searchText 进行操作。因此,通过这些,您将向 UITextField 对象发送消息。

In the first one, the cast applies to the whole of searchText.text. Assigning a UITextField object to a NSString variables is not what you want. The code you're looking for is:

在第一个中,强制转换适用于整个 searchText.text。将 UITextField 对象分配给 NSString 变量不是您想要的。您正在寻找的代码是:

NSString *name = ((UITextField *)searchText).text;

The mutableCopy message returns a copy of your string as a NSMutableString object, which can be assigned to a NSString as NSMutableString derives from it. In this case using the 'copy' message is just as good.

mutableCopy 消息将字符串的副本作为 NSMutableString 对象返回,该对象可以分配给 NSString 作为 NSMutableString 派生自它。在这种情况下,使用“复制”消息同样好。

Hope that helps.

希望有帮助。