![](/img/trans.png)
[英]Incompatible pointer types assigning to 'NSArray' to 'NSString'
[英]incompatible pointer types assigning to 'UILabel *__weak' to 'NSString *__strong'?
我一直在使用objective-c做一件事,但我是這種語言的初學者,這就是我所做的:
有一個名為“firstviewclass”的類,它控制着我的第一個視圖,在這個視圖中有一個用戶輸入數字的文本字段。 textfield在firstviewclass.h中,名為“setNumber”。 還有另一個名為“secondviewclass”的類控制着第二個視圖,在這個視圖中有一個標簽位於secondviewclass.h中,我希望這個標簽能夠重現用戶放在文本域中的值。第一個觀點。 我的代碼:
firstviewclass.h:
#import <UIKit/UIKit.h>
@interface firstviewclass : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *setNumber;
- (IBAction)gotonextview:(id)sender;
@end
secondviewclass.h:
#import <UIKit/UIKit.h>
#import "firstviewclass.h"
@interface secondviewclass : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *labelthatrecivesthetextfieldvalue;
@end
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
我已經改變了你告訴我改變的內容,但當我在模擬器上測試它在文本字段中放置的任何數字時,它在標簽中顯示為0 ...我真的不知道該怎么做!
它應該是:
labelthatrecivesthetextfieldvalue.text = string;
引擎蓋下:
在您的xib實例化UILabel
並使用IBOutlet
關鍵字獲取其引用之后,您將嘗試使用UILabel
指針指向為NSString
分配和實例化的內存空間,這就是您遇到此問題的原因。 您可能也確實收到了關於您嘗試執行的任務的警告:
labelthatrecivesthetextfieldvalue = string;
您應該始終嘗試修復警告。
你想要什么:
您只是希望UILabel
顯示您剛剛創建的NSString
,為此您應該使用UILabel's
text屬性。
您收到該錯誤是因為您將標簽指向字符串。 它們屬於不同的類別,所以這是不允許的。 你的真正含義是你希望標簽上的文字取字符串的值。 所以你需要設置標簽的文本字段:
labelthatrecivesthetextfieldvalue.text = string;
我為我早先的錯誤答案道歉。
順便提一下,您還應該考慮使用camelCase來命名變量。 這是objective-c的慣例。 labelThatRecievesTheTextFieldValue
其labelThatRecievesTheTextFieldValue
。 這不會影響代碼的工作能力,只是約定。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.