简体   繁体   中英

“Use of undeclared identifier”

I'm pretty new to Objective C. Keep getting this error

"Use of undeclared identifier"

for this line of code:

NSString *textOut1 = textOut.text;

Obviously I am just trying to pull the textbox text. I have declared the following in my h file

@property (weak, nonatomic) IBOutlet UITextField *textOut;

So I cannot understand why I keep getting that damn error. Thanks!

Use this:

NSString *textOut1 = _textOut.text;

Checkout the extra underscore I added in front of textOut

Read the paragraph titled Most Properties Are Backed by Instance Variables in this apple documentation page.

To access the property textOut of an object referred to by the variable someVariable , you write someVariable.textOut . So to access the property textOut of the object that is self , you write self.textOut . You omitted the owner — self in this case.

If you are not (@synthesize) synthezising the property yourself then it will be synthesized with the instance variable having a Underscore at the start of the name.

try :

NSString *textOut1 = _textOut.text;

self.textOut.text is what you need.

A property is basically something like a class variable in Java that requires a public getter and setter to work with. So you cannot use it in a .m file directly, but need to utilize the getter and setter. Objective C on iOS actually provides you with these methods by itself. Hence, when you create a property, under the hood, you have code that does a getter and setter by itself.

You can set the value as :

_textOut = BLAHBLAH

And get it as :

[self textOut.text] 

or
self.textOut.text

or even _textOut.text (this is technically the local variable that is referenced by textOut )

Or in your case,

`NSString *textOut1 = [self textOut.text];`

or NSString *textOut1 = self.textOut.text;

or NSString *textOut1 = _textOut.text; // This is probably the least recommended one.

Though I believe the Stanford class says it is always better to do [self textOut] .

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