1 Replies - 17612 Views - Last Post: 26 February 2013 - 08:59 PM

#1 hego555  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-February 13

Creating Object From String

Posted 11 February 2013 - 06:06 PM

I am new to Obj-C and as my first project I decided to make a Mac version of a program I had made before in C#

Basically the program is a counter or score keeper or whatever you want to call it!

There are 9 + and - buttons each pair connected to a label

So instead of having 18 Methods I made 2 which work dynmaiccly

Now I am trying to do the same thing in Obj-C

Here is my code

NSString *which = "L1"; //Label 1
NSTextField *label = (NSTextField *)NSClassFromString(which)]);



I get a Expected identifier error, I do not know really what to do!

Thank You!

Is This A Good Question/Topic? 0
  • +

Replies To: Creating Object From String

#2 kristianl1415  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-January 13

Re: Creating Object From String

Posted 26 February 2013 - 08:59 PM

First of all I would use the UILabel class over the UITextField class since the text field is for user editable text (I don't think NSTextField is a class so that could be where the error is).

NSTextField *label = (NSTextField *)NSClassFromString(which)]);


Should be

UILabel *label = [[UILabel alloc] init]; // I've always used Interface Builder so not sure how to init a label properly
[label setText:which]; // this line sets the text of the label to whatever you want


It looks like you were trying to cast the string into a text field, which wouldn't work because the text field (again should be a label) HAS a property called text, and can't be casted to another object type. Here is the UILabel class reference for more.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1