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 Jun 23, 2011, 05:22 PM   #1
larswik
macrumors 65816
 
Join Date: Sep 2006
Convert to NSString.

Last night I tried my hand at Cocoa with a simple hello world button that displays that text in a NSTextFieldCell, neat.

I then thought I would try to have code generate a random number and store it in a int variable and the display it in the text field instead. I got an error saying to the effect it was unable to assign an int to a pointer of NSString. Strings and int's are different types, I get this. But then I found this code online

Code:
NSString* str = [NSString stringWithFormat:@"%d", theRandomRollValue];
I am wondering. Is this common type casting code that is widely used? Or is there a better way I should write this code? Here is the original code bellow

Code:
#import <Foundation/Foundation.h>

@interface theDice : NSObject {
    
    int theRandomRollValue;
    IBOutlet NSTextFieldCell *lable;
}
- (IBAction)theButton:(id)sender;

@end
Code:
#import "theDice.h"

@implementation theDice

- (IBAction)theButton:(id)sender {
    theRandomRollValue = arc4random() %100 +1;
 
     NSString* str = [NSString stringWithFormat:@"%d", theRandomRollValue];
    [lable  setStringValue:str];
}
@end
larswik is offline   0 Reply With Quote
Old Jun 23, 2011, 05:23 PM   #2
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
stringWithFormat: is fine and I would consider that normal.
robbieduncan is offline   0 Reply With Quote
Old Jun 23, 2011, 05:24 PM   #3
balamw
Moderator
 
balamw's Avatar
 
Join Date: Aug 2005
Location: New England, USA
FWIW It's really not that different from an sprintf that you might use in straight C.

B
__________________
MBA (13" 1.7 GHz 128GB), UMBP (15" SD 2.8 GHz), UMB (13" 2.4 GHz), iMac (17" Yonah), 32GB iPad 3 WiFi+LTE, 64 GB iPad WiFi, 32 GB iPhone 5, Airport Extreme
balamw is offline   0 Reply With Quote
Old Jun 23, 2011, 05:45 PM   #4
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
One other way to do it is to use NSNumberFormatter like so:

Code:
NSNumber* num = [NSNumber numberWithInt: theRandomRollValue];
NSString* str = 
  [NSNumberFormatter 
    localizedStringFromNumber:num
    numberStyle:NSNumberFormatterDecimalStyle
  ];
It's more complex then using NSString stringWithFormat. But it has the advantage of formatting the number according to the user's Language & Text settings, such as putting in commas/points as appropriate.
jiminaus is offline   0 Reply With Quote
Old Jun 23, 2011, 06:17 PM   #5
larswik
Thread Starter
macrumors 65816
 
Join Date: Sep 2006
Cool, thanks. I am to the point now where I want to learn and also experiment. Experimenting helps me grasp the concepts I read about more.

Dealing with any kind of text box on screen they display always string value? So everything needs to be converted to a string value to be displayed properly?

Thank you.

-Lars
larswik is offline   0 Reply With Quote
Old Jun 23, 2011, 06:41 PM   #6
jiminaus
macrumors 65816
 
Join Date: Dec 2010
Location: Sydney
Quote:
Originally Posted by larswik View Post
Dealing with any kind of text box on screen they display always string value? So everything needs to be converted to a string value to be displayed properly?
Yes. Until you learn bindings and value transformers, which will be some time yet.
jiminaus is offline   0 Reply With Quote
Old Jun 23, 2011, 08:08 PM   #7
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by larswik View Post
Code:
NSString* str = [NSString stringWithFormat:@"%d", theRandomRollValue];
I am wondering. Is this common type casting code that is widely used?
FWIW, that's not type casting. It's a conversion, in the same way that sprintf() or sscanf() performs a conversion.


I will also point out that NSTextField has a number of type conversion methods, which it inherits from NSControl. See the latter's class reference doc and look at -intValue and -setIntValue: methods. Also look at similar methods for float, double, etc. These methods are all conversions, not type casting.
chown33 is offline   0 Reply With Quote
Old Jun 23, 2011, 08:30 PM   #8
larswik
Thread Starter
macrumors 65816
 
Join Date: Sep 2006
Quote:
Originally Posted by chown33 View Post
-intValue and -setIntValue: methods.
I did see those in a youtube videos I was watching. I first thought they meant "The initial values" when it was set up, and not 'Intiger values".

Thanks!

-Lars
larswik is offline   0 Reply With Quote
Old Jun 24, 2011, 01:02 AM   #9
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by larswik View Post
I did see those in a youtube videos I was watching. I first thought they meant "The initial values" when it was set up, and not 'Intiger values".
Hence the value of reading class reference docs, rather than relying on youtube videos and inference.
chown33 is offline   0 Reply With Quote
Old Jun 24, 2011, 05:26 AM   #10
balamw
Moderator
 
balamw's Avatar
 
Join Date: Aug 2005
Location: New England, USA
Quote:
Originally Posted by chown33 View Post
Hence the value of reading class reference docs, rather than relying on youtube videos and inference.
Quote:
You don't remember Cocoa; you look it up!
Anonymous Cocoa programmer quoted at the beginning of "Cocoa in a Nutshell".

B
__________________
MBA (13" 1.7 GHz 128GB), UMBP (15" SD 2.8 GHz), UMB (13" 2.4 GHz), iMac (17" Yonah), 32GB iPad 3 WiFi+LTE, 64 GB iPad WiFi, 32 GB iPhone 5, Airport Extreme
balamw 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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Converting float to NSString liptonlover iPhone/iPad Programming 7 Jan 14, 2009 12:23 PM
NSValueTransformer Converting BOOL to NSString stadidas Mac Programming 4 Jan 8, 2009 12:17 PM
Convert NSMutableData to NSString arme iPhone/iPad Programming 6 Sep 24, 2008 03:29 AM
Convert NSMutableData to NSString arme iPhone/iPad Programming 1 Sep 23, 2008 08:02 AM
Convert float to NSString and vice versa? ArtOfWarfare iPhone/iPad Programming 7 Sep 20, 2008 03:37 PM


All times are GMT -5. The time now is 09:43 AM.

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

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