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

Tiwaz

macrumors newbie
Original poster
Nov 19, 2009
5
0
So I'm trying to set up data persistence by writing values to a mutable array (NSStrings and floats, all input as NSString). The problem I'm getting is when I try to load them. Loading an NSString into another NSString works fine, but loading an NSString into a float gives me an error. Here's my code:

Saving:
Code:
[array addObject: name]; //name is an NSString
[array addObject: [NSString stringWithFormat:@"%f", stat]]; //stat is a float

Loading:
Code:
name = [array objectAtIndex:1];
stat = [[array objectAtIndex:3] floatValue]; //ERROR

Help? Everywhere I look tells me to use floatValue...

Thanks,
-T
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
First off,
Code:
[NSString stringWithFormat:@"%f", stat]
removes any precision of the float when converted to string. So, if stat contained 1.25, the string will be @"1".
 

Tiwaz

macrumors newbie
Original poster
Nov 19, 2009
5
0
kainjow:
Code:
@interface{
float *stat;
}

@property (nonatomic) float *stat;
..in the .h file


dejo:
Do you have any recommendations for not losing the floatyness?

Thank you both!
-T
 

Tiwaz

macrumors newbie
Original poster
Nov 19, 2009
5
0
When I do that I get a bunch of other errors from other methods and it doesn't actually fix the original error, could you perhaps explain the reasoning behind your suggestion? Thanks again!

-T
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Did you update the @property too, to not be a pointer?

Obj-C objects are always used as pointers. However, floats and other primitives (int, char, etc) normally aren't unless you specifically need to deal with them as pointers (usually in functions that modify their values).

If you're still having errors post the first error and the code for it.
 

Tiwaz

macrumors newbie
Original poster
Nov 19, 2009
5
0
dejo: I'm not at my work computer right now, but I will definitely switch *nonatomic* to *assign*. I have no idea what those do yet, but I will look them up soon. NSNumber sounds like a much better way to go... I'll try that out soon!

kainjow: Yeah, I did update the @property as well. I'm actually working with a team of other students, so I'll have to see how removing the pointer aspect will affect future coding, but assuming you're right in that they're generally not pointers it shouldn't be a problem.

I'll try to get an updated error message up here soon. I really appreciate the help!

-T
 

bredell

macrumors regular
Mar 30, 2008
127
1
Uppsala, Sweden
I'm a bit confused about your indices. When you add the objects to the array you add name first and then stat. But when you access the objects from the array you use index 1 and 3. If these are the only two objects in the array you should use index 0 and 1. But maybe you're not showing all additions to the array.

Anyway, the exact runtime error message should give you a clue as to what is wrong.
 

Tiwaz

macrumors newbie
Original poster
Nov 19, 2009
5
0
Alright, here's what I have now:

Error message: incompatible types in assignment

Code:
[array addObject: [NSNumber numberWithFloat:*stat]];
//saves the float as an NSNumber

stat = [[array objectAtIndex:2] floatValue]; //Try to load
//Error here^
//This is the same as before
//PS: I'm not showing all the entries to the save log, there are many.  This is just an example of one that does not work, hence index of 2...

When I change my variable to be a non-pointer here's the errors I get:
Code:
[array addObject: [NSNumber numberWithFloat:*stat]];
//Error message here: invalid type argument of "unary *"


stat = [[array objectAtIndex:2] floatValue];
//No errors here, but errors do crop up in other methods using the variable...

Any more suggestions? I'm really stumped. By the way, I changed the @property to
Code:
@property (assign) float *stat;
What does the "assign" do. I do not understand the reason for that line of code. I do know that I need it, however!

Thanks,
-T
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Code:
[array addObject: [NSNumber numberWithFloat:*stat]];
//saves the float as an NSNumber
...
By the way, I changed the @property to
Code:
@property (assign) float *stat;
Why are you using "*stat" all over the place? The "*" is not even necessary under these circumstances even if stat was an object, which it's not.

Anyways, I feel like I'm starting to sound like a broken record with the number of times I've suggested this but: Time to step back from the real coding and go (re)learn the basics of Objective-C and OOP before you continue.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.