Creating Object From String

Discussion in 'Mac Programming' started by hego555, Feb 11, 2013.

  1. macrumors newbie

    Joined:
    Aug 17, 2008
    #1
    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

    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!
     
  2. ConCat, Feb 11, 2013
    Last edited: Feb 11, 2013

    macrumors 6502a

    ConCat

    Joined:
    Jul 27, 2012
    Location:
    In an ethereal plane of existence.
    #2
    The proper format for an NSString literal is
    Code:
    @"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.
     
  3. ytk
    macrumors regular

    Joined:
    Jul 8, 2010
    #3
    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: ).
     
  4. thread starter macrumors newbie

    Joined:
    Aug 17, 2008
    #4
    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

    Code:
    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;
            }
     
  5. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #5
    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.
     
  6. macrumors 6502

    ElectricSheep

    Joined:
    Feb 18, 2004
    Location:
    Wilmington, DE
    #6
    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:

    Code:
    - (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.
     
  7. thread starter macrumors newbie

    Joined:
    Aug 17, 2008
    #7
    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.
     
  8. macrumors 603

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #8
    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.
     
  9. ytk
    macrumors regular

    Joined:
    Jul 8, 2010
    #9
    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.
     

Share This Page