从不兼容的指针类型分配?

Rey*_*old 0 iphone xcode objective-c

我是iPhone开发的新手,基本上都是C语言.我理解变量的概念等等,所以我试图以基本的方式使用它们来更好地掌握这个概念.不幸的是,当我尝试做一些非常简单的事情时,我收到编译器警告:我只想分配我的5个变量值.
ViewController.h代码:
@interface MyApplicationViewController : UIViewController {

IBOutlet UITextView *variable1;
IBOutlet UITextView *variable2;
IBOutlet UITextView *variable3;
IBOutlet UITextView *variable4;
IBOutlet UITextView *variable5;


}
Run Code Online (Sandbox Code Playgroud)

[我知道理论上我可以将这些变量连接到IB中的文本视图,但我不是]

@property (nonatomic, retain) IBOutlet UITextView *variable1;  
@property (nonatomic, retain) IBOutlet UITextView *variable2;  
@property (nonatomic, retain) IBOutlet UITextView *variable3;  
@property (nonatomic, retain) IBOutlet UITextView *variable4;  
@property (nonatomic, retain) IBOutlet UITextView *variable5;  


@end  
Run Code Online (Sandbox Code Playgroud)

ViewController.m代码:

@implementation MyApplicationViewController  
    @synthesize variable1;  
    @synthesize variable2;  
    @synthesize variable3;  
    @synthesize variable4;  
    @synthesize variable5;  
    - (void)viewDidLoad {  
    variable1 = "memory text1"; [Warning]  
    variable2 = "memory text2"; [Warning]  
    variable3 = "memory text3"; [Warning]  
    variable4 = "memory text4"; [Warning]  
    variable5 = "memory text5"; [Warning]  
    }
Run Code Online (Sandbox Code Playgroud)

我没有释放我的变量,因为我想将它们保留在内存中,直到应用程序完全终止.为什么我会收到这些警告?我做错了吗?我打算在这里做的就是将变量的值(memorytext1,内存文本2等)保存在内存中.我在Stack Overflow上查看了关于此警告的其他对话,但他们的问题似乎与我的不匹配,尽管警告是相同的.请不要说太复杂,因为我还是新手.谢谢!

Vla*_*mir 5

有两个问题:

  • 第一个问题是你试图将字符串分配给你的文本字段变量 - 如果你想设置字段的文本然后你应该使用它的文本属性
  • 第二个问题(实际上给你编译器警告)是"字符串"表示c-string文字 - 你应该使用@"string"代替

所以正确的代码来设置textfield的文本应该是

variable1.text = @"text1";
Run Code Online (Sandbox Code Playgroud)