2010-12-18 49 views
0

我完全不熟悉iPhone开发,基本上任何C语言。我理解变量等的概念,所以我试图以基本方式使用它们来更好地理解这个概念。不幸的是,当我尝试做一些非常简单的事情时,我收到了编译器警告:我只想分配我的5个变量值。
ViewController.h代码:
@interface MyApplicationViewController : UIViewController {从不兼容的指针类型分配?

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


} 

[我知道,理论上我可以连接这些变量的文本在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 

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

我不取消分配我的变量,因为我想让他们在内存中unti l应用程序被完全终止。为什么我会收到这些警告?我做错了什么?我打算在这里做的是将变量的值(memorytext1,memory text 2等)保存在内存中。我已经看过Stack Overflow上有关此警告的其他对话,但他们的问题似乎与我的不符,尽管警告是一样的。请不要说太复杂,因为我还是这个新手。谢谢!

回答

5

有是两个问题:

  • 1日的问题是,你要分配字符串文本字段变量 - 如果你想设置字段的文本,那么你应该使用它的文本属性
  • 第二个问题(实际上为您提供了编译器警告)是“串”是指C-字符串文字 - 你应该使用@“字符串”,而不是

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

variable1.text = @"text1"; 
+0

所以,即使我没有文字视图,这仍然工作?我的意思是,这些值是否仍然存储在内存中? – Reynold 2010-12-18 18:45:59

+0

如果你只想存储字符串值,那么使用NSString对象:NSString * stringVar; ... self.stringVar = @“memory text1”; – Vladimir 2010-12-18 19:12:37

0

您在文本开头缺少@

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

他缺少*很多*以上。 – bbum 2010-12-18 18:30:30

+0

为什么选择投票?那是正确的答案。 – WrightsCS 2010-12-18 18:30:39

+0

哦,好吧,文字 – WrightsCS 2010-12-18 18:31:08

0

弗拉基米尔是正确的,假设你正在使用NIB文件到指定的UITextView性能。如果你没有使用NIB,你的变量将是零,你的字符串将不会被分配到任何地方。

相关问题