Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-21-2011, 10:24 AM   #1 (permalink)
Registered Member
 
Join Date: Nov 2011
Posts: 30
adamwaite is on a distinguished road
Default NSNumber and float conversion?

I am trying to set an NSNumber as a float value that has been loaded using NSUserDefaults but i'm getting an error. I can't understand why...?

Here's my code:

Code:
[settingsData.sensitivitySettingValue floatValue] = [[NSUserDefaults standardUserDefaults] floatForKey:@"sensitivityKey"];
It works if I use a float on its own (i.e float myFloat = [[NSUserDefaults standardUserDefaults] floatForKey:@"sensitivityKey"]

, but I want the value available across all view controllers so I have defined it in a model class as an NSNumber.

So, please would anyone be able to advise me on why that statement wont work?
adamwaite is offline   Reply With Quote
Old 11-21-2011, 10:30 AM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Hint: [NSNumber numberWithFloat:blah];
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 11-21-2011, 11:27 AM   #3 (permalink)
Registered Member
 
Join Date: Nov 2011
Posts: 30
adamwaite is on a distinguished road
Default

I found a decent answer so going to post it up in case it may help someone else one day.....

Taking settingsData as an object of your datamodel class where sensitivitySettingValue is a variable declared as NSNumber using something as

Code:
[settingsData.sensitivitySettingValue floatValue]
actually fetches its value rather than setting a float value to it . You can either use the dot notation such as

Code:
settingsData.sensitivitySettingValue = [NSNumber numberWithFloat:[[NSUserDefaults standardUserDefaults] floatForKey:@"sensitivityKey"]];
That works.
adamwaite is offline   Reply With Quote
Old 11-21-2011, 12:40 PM   #4 (permalink)
Registered Member
 
Join Date: Nov 2011
Posts: 30
adamwaite is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
Hint: [NSNumber numberWithFloat:blah];
Why wont this output 5 to the console? It is outputting 0?!

Code:
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    float five = 5;
    
    settingsData.sensitivitySettingValue = [NSNumber numberWithFloat:five];
    
    float someFloat = [settingsData.sensitivitySettingValue floatValue];
    
    NSLog(@"%f", someFloat);

}
adamwaite is offline   Reply With Quote
Old 11-21-2011, 01:01 PM   #5 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

My guess is that settingsData is nil.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 11-21-2011, 01:58 PM   #6 (permalink)
Registered Member
 
Join Date: Nov 2011
Posts: 30
adamwaite is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
My guess is that settingsData is nil.
Ah that sounds like it must be it. What do you mean when you say it is nil? How do I changed that? Do I have to alloc init it? Sorry, complete newbie with model classes.
adamwaite is offline   Reply With Quote
Old 11-21-2011, 02:07 PM   #7 (permalink)
Registered Member
 
Join Date: Nov 2011
Posts: 30
adamwaite is on a distinguished road
Default

ah yes, allocating it worked.
adamwaite is offline   Reply With Quote
Old 11-21-2011, 02:26 PM   #8 (permalink)
Registered Member
 
Join Date: Nov 2011
Posts: 30
adamwaite is on a distinguished road
Default

Thanks BrianSlick, got everything working now as it should. Learned a lot with your comments and feedback.
adamwaite is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
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
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 390
14 members and 376 guests
7twenty7, chiataytuday, Clouds, dedeys78, Duncan C, e2applets, EvilElf, iekei, ipodphone, jeroenkeij, leostc, mbadegree, Murphy, QuantumDoja
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,125
Posts: 402,910
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:33 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0