A little help? (xCode and Obj C) - MacRumors Forums
Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Apr 30, 2006, 02:33 PM   #1
Mikey82
macrumors newbie
 
Join Date: Apr 2006
A little help? (xCode and Obj C)

Hi Guys,

Im getting started with xcode and objective-c.

I did 1st year at uni doing programming but I left after the first year. I wasnt very good and it was a while ago...but I thought it would be fun to give some programming a go again.

If I add to variables together... previousNumber and currentNumber, both declared as floats.

Say previousNumber is 6 and current is 0.1, and i display the answer in a textfield created with the xcode interface builder. I'm get 6.10000 and then a load of numbers.

Is this because I have to limit the amount of characters displayed? How would I do that?

Also when I build an app which is to be bootable on another machine how do I do that. I build the project. Theres an app in the build folder but I guess it needs some of the other files in there.

Thanks in advance

Mikey
Mikey82 is offline   0 Reply With Quote
Old Apr 30, 2006, 03:10 PM   #2
HexMonkey
Administrator
 
HexMonkey's Avatar
 
Join Date: Feb 2004
Location: New Zealand
Quote:
Originally Posted by Mikey82
If I add to variables together... previousNumber and currentNumber, both declared as floats.

Say previousNumber is 6 and current is 0.1, and i display the answer in a textfield created with the xcode interface builder. I'm get 6.10000 and then a load of numbers.

Is this because I have to limit the amount of characters displayed? How would I do that?
Floating point numbers are not stored in their exact forms, only as approximations, which is why you get some strange numbers on the end. I won't get into the details but if you're interested read the Wikipedia article on it. You can round the number when you display it like this (this example rounds to one decimal place):
Code:
float aFloat = previousNumber + current;
[aTextField setStringValue:[NSString stringWithFormat:@"%.1f", aFloat]];
Quote:
Originally Posted by Mikey82
Also when I build an app which is to be bootable on another machine how do I do that. I build the project. Theres an app in the build folder but I guess it needs some of the other files in there.
When you build for other computers you need to use the Deployment build configuration. Choose Project > Set Active Build Configuration > Deployment. At other times you should stick with the Development configuration since the debugger doesn't work with Deployment.
HexMonkey is offline   0 Reply With Quote
Old Apr 30, 2006, 04:16 PM   #3
Mikey82
Thread Starter
macrumors newbie
 
Join Date: Apr 2006
Excellent thanks.!

Is there a way to set the format using setFloatValue? although that'll work fine with the setStringValue.

Mikey
Mikey82 is offline   0 Reply With Quote
Old Apr 30, 2006, 05:25 PM   #4
cazlar
macrumors 6502
 
Join Date: Oct 2003
Location: Sydney, Australia
I think you could also use setFloatValue: then you could just use IB to set the formatting of the textfield to limit it to 2 or 3 digits?
cazlar is offline   0 Reply With Quote
Old May 1, 2006, 08:12 AM   #5
Mikey82
Thread Starter
macrumors newbie
 
Join Date: Apr 2006
Ok thanks.

Is that at outlets section in the header file?


Also, if I have a function. I have problems if Im using int and float together.

ie if I multiply the float variable by the int variable and use retval (which I set as a float) it only returns the whole number, not the decimal part.

Thanks again for the help guys.

I've tried as a project to make a calculator, which isnt as easy as i thought. It works fine apart from inputing decimals. The codes ugly though.......

I'm going to try getting dictionarys to work next.! I want different dictionaries in an array. I find it quite difficult, especially the memory management side. Though theres a nice tutorial about making an address book at the o'reilly site where he does just that though.

Mikey
Mikey82 is offline   0 Reply With Quote
Old May 1, 2006, 11:02 AM   #6
gekko513
macrumors 601
 
gekko513's Avatar
 
Join Date: Oct 2003
Quote:
Originally Posted by Mikey82
ie if I multiply the float variable by the int variable and use retval (which I set as a float) it only returns the whole number, not the decimal part.
A multiplication between an int and a float will be done as a floating point multiplication (unless the float is cast to an int first), and if the result is assigned to a float, then the decimal part should be in there.

There must be something else that causes it. Try to log the different variables at different stages, watch what happens in the debugger or post the method/function here for someone to look at.
__________________
Someone get Nekkid™, now!
gekko513 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
Getting started with XCode/Obj-C but have some outdated books. MikeonTV iPhone/iPad Programming 8 Apr 8, 2014 03:11 PM
Moving from xcode 3.2 to xcode 4.5.2 compilation error for 'abs' macro in stdlib.h hrishidev Mac Programming 7 Jun 2, 2013 10:36 AM
Interested in learning Obj-C/Xcode what are some good resources? hugodrax Mac Programming 3 Nov 10, 2012 09:07 PM
What to learn aside Obj C Will0827 iPhone/iPad Programming 8 Sep 27, 2012 03:07 PM
Need help with indexing (Java or Obj-C) iVikD Mac Programming 5 Jul 5, 2012 05:43 PM

Forum Jump

All times are GMT -5. The time now is 08:56 AM.

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

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