Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 11, 2013, 11:54 PM   #1
hego555
macrumors newbie
 
Join Date: Aug 2008
Creating Object From String

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!
hego555 is offline   0 Reply With Quote
Old Feb 12, 2013, 12:03 AM   #2
ConCat
Banned
 
Join Date: Jul 2012
Location: In an ethereal plane of existence.
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.

Last edited by ConCat; Feb 12, 2013 at 12:09 AM.
ConCat is offline   0 Reply With Quote
Old Feb 12, 2013, 12:40 AM   #3
ytk
macrumors regular
 
Join Date: Jul 2010
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: ).
ytk is offline   0 Reply With Quote
Old Feb 12, 2013, 12:55 AM   #4
hego555
Thread Starter
macrumors newbie
 
Join Date: Aug 2008
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;
        }
hego555 is offline   0 Reply With Quote
Old Feb 12, 2013, 03:59 AM   #5
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by hego555 View Post
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.
gnasher729 is offline   0 Reply With Quote
Old Feb 12, 2013, 09:52 AM   #6
ElectricSheep
macrumors 6502
 
Join Date: Feb 2004
Location: Wilmington, DE
Send a message via AIM to ElectricSheep
Quote:
Originally Posted by hego555 View Post
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;
        }
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.
__________________
15'' MBP (early 2011) | i7 3770k Hackintosh | i7 Mac Mini (late 2012) | iPhone 5 | iPad 3 (2012) | iPad mini | MacOS X 10.9.2
ElectricSheep is offline   1 Reply With Quote
Old Feb 12, 2013, 01:16 PM   #7
hego555
Thread Starter
macrumors newbie
 
Join Date: Aug 2008
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.
hego555 is offline   0 Reply With Quote
Old Feb 12, 2013, 08:11 PM   #8
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
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.
ArtOfWarfare is offline   0 Reply With Quote
Old Feb 12, 2013, 11:44 PM   #9
ytk
macrumors regular
 
Join Date: Jul 2010
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.
ytk is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Resolved: How do I convert an NSData object to an NSDictionary object, and vice versa? moonman239 iPhone/iPad Programming 6 May 2, 2014 02:17 PM
Format string is not a string literal. larswik Mac Programming 5 Jan 17, 2014 11:45 PM
Resolved: Best way to access all properties of an object whose names begin with a string? moonman239 iPhone/iPad Programming 7 Nov 18, 2013 04:04 PM
Search a string for a String of a specific format. ncaissie iPhone/iPad Programming 19 Nov 15, 2013 11:42 AM
Creating a String with multiple lines LT21j iPhone/iPad Programming 5 Jun 6, 2013 10:50 AM

Forum Jump

All times are GMT -5. The time now is 07:59 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC