使用未声明的标识符 - Xcode [英] Use of undeclared identifier - Xcode
问题描述
我首先使用
@property (weak, nonatomic) IBOutlet UIWebView *webView;
然后用
@property (weak, nonatomic) IBOutlet UIWebView *webView;
然后我创建了一个函数来使用用户将其转换为参数的字符串来创建网页.功能:
I then created a function to create a webpage using a string which the user would cast as an argument. The function:
void createWebpage(NSString *webString) {
NSURL *url = [NSURL URLWithString:webString];
NSURLRequest *requestUrl = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestUrl];
}
以及它被调用的地方.
- (void)viewDidLoad {
createWebpage(@"http://www.google.com");
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
但是,在函数的最后一行 [webView loadRequest:requestUrl];
中,webView 产生错误Use of undeclared identifier 'webView'.这是为什么,我该如何解决是吗?感谢所有帮助.
However, in the last line of the function, [webView loadRequest:requestUrl];
, webView produces the error "Use of undeclared identifier 'webView'. Why is this, and how can I fix it? All help appreciated.
推荐答案
您正在声明一个在对象中可用的属性.但是您声明了一个简单的 C 方法:
You are declaring a property which is a available in an object. But you are declaring a simple C method:
void createWebpage(NSString *webString) {
NSURL *url = [NSURL URLWithString:webString];
NSURLRequest *requestUrl = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestUrl];
}
此方法将在全局上下文"中执行,而不是在对象上执行.所以你不能访问对象属性.
This method will be executed in a "global context" but not on the object. So you can't access the objects properties.
改为使用方法:
- (void) createWebpage:(NSString *)webString {
NSURL *url = [NSURL URLWithString:webString];
NSURLRequest *requestUrl = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:requestUrl];
}
当你访问一个属性时,你必须使用 self
来引用当前对象.
And you have to use self
to refer to the current object when you're accessing a property.
然后你可以调用这个方法:
You can then call this method:
[self createWebpage:@"http://www.google.com"];
I really recommend you to read this: https://developer.apple.com/library/mac/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/Introduction/Introduction.html
这篇关于使用未声明的标识符 - Xcode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!