Michael Mangold
Michael Mangold

Reputation: 1801

Parsing a UILabel

I have a UILabel for a calculator (this is for the CS193P graphing calculator). I would like to parse the current operand, which would be the rightmost value following an operator. For example, X+3 would need to parse the 3.

What is the easiest way to do this? Also, in trying to assign values back-and-forth between an NSString and this UILabel, I've received casting errors. Could someone explain the rationale behind having UILabel and NSString requiring explicit casting? It seems to me they should both be treated as strings.

Upvotes: 0

Views: 136

Answers (2)

thomas
thomas

Reputation: 5649

Casting-problem:

I hope you've written myLabel.text = myString and not myLabel=myString

Parsing-problem:

I think the best way to do this is by creating a Syntax-Tree and then working only with the syntax-tree. To create this I would just iterate through the text beginning at the front and read the text char by char.

Upvotes: 0

Ugur Kumru
Ugur Kumru

Reputation: 986

You need to use .text property of UILabel which is an NSString. Therefore you don't need casting while using text property.

Also you can use characterAtIndex: method of NSStrings with the length method of your label's text.

like this [yourLabel.text characterAtIndex:([yourLabel.text length]-1)];

Upvotes: 2

Related Questions