简体   繁体   中英

Assignment from incompatible pointer type?

I am completely new to iPhone development and basically any C language at all. I understand the concept of variables and so forth, so I'm trying to use them in basic way to get a better grasp on the concept. Unfortunately, I'm getting compiler warnings when I try to do something very simple: I just want to assign my 5 variables values.
ViewController.h code:
@interface MyApplicationViewController : UIViewController {

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


}

[I know that theoretically I could connect those variables to text views in IB, but I'm not]

@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  

ViewController.m code:

@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]  
    }

I do not deallocate my variables because I want to keep them in memory until the application is completely terminated. Why am I getting these warnings? Am I doing anything wrong? All I'm intending to do here is save the variables' values (memorytext1, memory text 2, etc.) in the memory. I've looked at the other conversation about this warning on Stack Overflow, but their problem didn't seem to match mine, although the warning was the same. Please don't say anything too complicated, because I am still new to this. Thanks!

There're 2 problems:

  • 1st problem is that you're trying to assign string to your text fields variables - if you want to set field's text then you should use its text property
  • 2nd problem (that actually gives you compiler warning) is that "string" means c-string literal - you should use @"string" instead

So correct code to set textfield's text should be

variable1.text = @"text1";

You are missing the @ at the beginning of the text!

variable1.text = @"memory text1";
variable2.text = @"memory text2"; 
variable3.text = @"memory text3"; 
variable4.text = @"memory text4"; 
variable5.text = @"memory text5"; 

Vladimir is correct, assuming you are using a NIB file to assign the UITextView properties. If you are not using a NIB your variables will be nil and your strings will not be assigned anywhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM