Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

ahan.tm

macrumors regular
Original poster
Jun 26, 2011
141
0
Florida
Hi,

I was wondering how I can set a float variable to a label in a Mac App in Xcode 4. I currently have
Code:
	["label name" setText:[NSString stringWithFormat:@"%.2f", "float variable"]];

inside my IBAction. Is this correct?

Thanks

Ahan M.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,740
8,416
A sea of green
Is this correct?

Does it compile? I can tell by looking at the code the answer is "No", so clearly, it's not correct.


What book or tutorial are you learning from? Be specific.
If the answer is "None", then tell us exactly what reference or other documentation you're using.

Is this one of the exercises from a book or tutorial? Which one?
 

ahan.tm

macrumors regular
Original poster
Jun 26, 2011
141
0
Florida
It does compile in xcode with the warning
Code:
Instance method '-setText:' not found (return type defaults to 'id')
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
It does compile in xcode with the warning
Code:
Instance method '-setText:' not found (return type defaults to 'id')

Treat the warning as an error. The compiler is telling you that you are sending a message that the object will not understand. Open the documentation and check the method.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,740
8,416
A sea of green
It does compile in xcode with the warning
Code:
Instance method '-setText:' not found (return type defaults to 'id')
Your code isn't even close to being correct:
Code:
["label name" setText:[NSString stringWithFormat:@"%.2f", "float variable"]];
The first thing after [ must be an object. "label name" isn't an object. It's a pointer to a C string. A C string is definitely not an object.

There are other errors in that line of code, but they won't matter until you can write the code so it sends a message to an object.

That's why I asked you to identify the book or tutorial you're learning from, or what reference doc you were using. Whatever it is, you really need to start with the basics and work up from there. It's clear from the posted code you don't yet have an adequate grasp of the fundamentals.
 

ahan.tm

macrumors regular
Original poster
Jun 26, 2011
141
0
Florida
Is the code the same for Mac and iPhone because

Code:
[altitude setText:[NSString stringWithFormat:@"%.2f", alt]];

works fine in my iphone app.

Also, are there any good tutorials out there?
 

chown33

Moderator
Staff member
Aug 9, 2009
10,740
8,416
A sea of green
Is the code the same for Mac and iPhone because

Code:
[altitude setText:[NSString stringWithFormat:@"%.2f", alt]];

works fine in my iphone app.

That's not the code you first posted. If altitude is an object that responds to setText:, and alt is a double, then that code will work.

The problem is neither of those things is true for the code you first posted:
1. "label name" is not an object.
2. "float variable" is not a double variable.
Both are C strings.

So however you got that code to work in your iphone app, it seems that you don't really understand what it says or does.


If you don't know how to find any tutorials, you should probably start at the beginning.

At the top of this forum:
https://forums.macrumors.com/forums/73/

there's a Guide link with this name:
How do I get started programming on the Mac (or iPhone)?

You should also see some other useful links.

And you can search the MR site forums and look for keywords like beginner. Or just scroll through recent postings in the forum. The question comes up every couple of days.
 

ahan.tm

macrumors regular
Original poster
Jun 26, 2011
141
0
Florida
Hey,

Thanks for taking the time to help me with my problem.

After some working, I was able to get some parts of it working. The debugger now gives an error:

Code:
[42256:903] -[NSTextField setText:]: unrecognized selector sent to instance 0x10051ee40
when using
Code:
[altitude setText:[NSString stringWithFormat:@"%.2f", alt]];

Also, any great books on Objective-C?

Thanks,
Ahan
 

larswik

macrumors 68000
Sep 8, 2006
1,552
11
Just a suggestion. If you are brand spanking new to programming start with C, not objective C. I am also learning to program and I stopped Objc - C and started C. Objective - C sits on top of C so much of that language you can use. More importantly you develop an understanding of programming. So many people follow tutorials and videos who know nothing, Monkey see Monkey do approach. Once you try to inject your own code into the tutorial to customize it you are just guessing and hoping it works, because you really have no clue what you are doing. I really started to learn to program a year ago and just 2 days ago I made my first GUI. I got the core information down first.

If you want to have a good chance at succeeding in programming, buy this book "Learn C on the Mac". For me it was a great book to get the fundamentals from, and set me up to learn Objective - C.. I don't know the author or make money from it but well worth it.
 

ahan.tm

macrumors regular
Original poster
Jun 26, 2011
141
0
Florida
Hey Guys,

Thanks for your responses. I finally figured it out using setStringValue instead of setText:

Code:
 [altitude setStringValue:[NSString stringWithFormat:@"%.2f",alt]];

Also, I have roots in C and C++ and it has been easy to adapt to Obj-C.

Thanks Again For All Your Help!!:)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.