PDA

View Full Version : Creating Object From String




hego555
Feb 11, 2013, 10:54 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 dynamically

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!



ConCat
Feb 11, 2013, 11:03 PM
The proper format for an NSString literal is
@"string here"
The one you used is a C const char *

Also, you don't ever want to call NSClassFromString usually. What are you trying to do here exactly?

C# is a very different language from Objective-C. You may want to check up on the Objective-C videos on iTunes U from Stanford. They're quite good and should get you on the right track.

ytk
Feb 11, 2013, 11:40 PM
I think you're a bit confused here. You seem to be trying to use NSClassFromString to generate a “generic” object, then casting that object to an NSTextField. NSClassFromString returns the class that is named in the string, so you would pass it something like "NSString" or "NSView" (and then call alloc on the returned class). But as ConCat mentioned, you probably don't want to be using it, because if you know what the class type is at compile time then you should just use that.

Also, are you aware that NSTextField is a UI element? It's not the same thing as a string. Usually you would just add an NSTextField in Interface Builder (now part of Xcode), but you can generate one programmatically and add it to a view that you have an outlet for if you like. Just remember that an NSTextField contains an NSString (which holds the contents of the field), and this NSString must be set once the NSTextField is instantiated by using the appropriate setter method (in this case, setStringValue: ).

hego555
Feb 11, 2013, 11:55 PM
I dont know if I made it clear what I am trying to do, I was probably vague and my code made it worse.

Picture a window like this

0 0 0 0 0 0 0 0 0
+- +- +- +- +- +- +- +-

Now I dont want to have a method for each + and for each -

So instead I gave the +'s and -'s names

Like A1 A2 M1 M2

and I also gave the Labels names L1 L2

So I have two methods, 1 for Add, and one for Subtract

I use the sender object to get the number and then I put a L in front of it to know which label.

But I cant seem to figure out how to do this.

Here is my C# code with comments added

private void Add(object sender, EventArgs e)
{
PictureBox pic = (PictureBox)sender; //Which add was clicked
int number = int.Parse(pic.Name.Substring(3));//take the number
TextBox tb = (TextBox)this.Controls["textBox" + number];//Make a object with the number

//Rest isn't important
int num = g[number];
num++;
g[number] = num;
String output = num.ToString();
if (tb != null)
tb.Text = output;
}

gnasher729
Feb 12, 2013, 02:59 AM
I dont know if I made it clear what I am trying to do, I was probably vague and my code made it worse.

To be honest, you need to learn Objective-C and how to use Cocoa first. Otherwise you have not the slightest chance to produce a working program. There are plenty of good books about Objective-C, there is the iPhone programming course at Stanford that you can take for free (it is on iTunes U), there are plenty of resources at www.developer.com.

But if you cannot write an Objective-C/Cocoa program on its own, translating C# to Objective-C isn't going to work.

ElectricSheep
Feb 12, 2013, 08:52 AM
I dont know if I made it clear what I am trying to do, I was probably vague and my code made it worse.

Picture a window like this

0 0 0 0 0 0 0 0 0
+- +- +- +- +- +- +- +-

Now I dont want to have a method for each + and for each -

So instead I gave the +'s and -'s names

Like A1 A2 M1 M2

and I also gave the Labels names L1 L2

So I have two methods, 1 for Add, and one for Subtract

I use the sender object to get the number and then I put a L in front of it to know which label.

But I cant seem to figure out how to do this.

Here is my C# code with comments added

private void Add(object sender, EventArgs e)
{
PictureBox pic = (PictureBox)sender; //Which add was clicked
int number = int.Parse(pic.Name.Substring(3));//take the number
TextBox tb = (TextBox)this.Controls["textBox" + number];//Make a object with the number

//Rest isn't important
int num = g[number];
num++;
g[number] = num;
String output = num.ToString();
if (tb != null)
tb.Text = output;
}

In Objective-C/Cocoa, you would use actions and targets to accomplish this. Your private void Add(object sender, EventArgs e); would looks like a this:


- (IBAction)add:(id)sender
{
// Code to to manipulate the text boxes here.
}


Implement this method in some kind of controller object, and connect the target actions of your add buttons to this selector. NSButton objects inherit from NSCell and you can use tags to identify them. Set a unique tag value for each buttons, and you can find out which button sent the action with [sender tag].

If you want to programatically add or remove arrays of controls (say you want to dynamically add or remove players from your scoreboard), you might be interested in looking up NSMatrix and the Matrix Programming Guide.

hego555
Feb 12, 2013, 12:16 PM
I think I am just going to use a array to store the score and then use a refresh method to update the labels. It's worked so far.

ArtOfWarfare
Feb 12, 2013, 07:11 PM
What is your goal here? If you want this app to use and aren't so concerned with learning things, it seems to me this is an extraordinarily easy app to make without writing a single line of code.

Drag and drop in your 9 fields.
Drag and drop in 9 steppers.

Use cocoa bindings to bind each field to each stepper.

All of that can be done in your interface file (XIB) alone.

ytk
Feb 12, 2013, 10:44 PM
Heck, it's even easier than that. Just drag out an NSTextField and an NSStepper. Give the NSTextField a title of “0”. Set the appropriate parameters for your NSStepper (minimum and maximum values). Ctrl-drag from the stepper to the text field, and assign the takeIntValueFrom: action. Select both items and duplicate them as many times as you want. Done.