views:

948

answers:

3

I can't live another day without knowing why this is happening. I have an iPhone application using Corelocation and here is the issue:

GPSLocation.m - sends long/lat coordinates which are floats to class UserSettings.m (which is a singleton class)

_

UserSettings.m - Then cast the values into a predefined strings from a float

e.g. strLongitude = [NSString stringWithFormat:@"%f", paramLong];

I can trace out the string just fine NSLog(@"Longitude string is %@", strLongitude);

Some notes about strLongitude

  1. is in the header file
  2. property is defined as (nonatomic, retain)
  3. is synthesized

Now the problem: When I need this variable in another class I can use one of two methods of getting it. Either by a getter [UserSettings getLongitude] or access the property [UserSettings strLongitude]

Then when I then trace out this string I sometimes get a badaccess error or random garbage. If I trace out the string as a float %F it works just fine.

Why is this happening? The string defaults back to the original data type of the variable.

Any help would be appreciated.

+1  A: 

It sounds like some code is accidentally calling the string setter with a float argument.

Chuck
+2  A: 

strLongitude might need to be retained.

Jeff
it is! @property (nonatomic, retain) NSString *strLongitude;
Charles Peterson
But you need to use self: self.strLongitude = [NSString stringWithFormat:@"%f", paramLong];to invoke the setter (which does that retain). Just for kicks, try: strLongitude = ...; [strLongitude retain]; and see if there's any difference.
Jeff
BINGO! I didn't invoke the setter. self.strLongitude worked just fine. Thanks man!
Charles Peterson
I've been there... I suggest you use NSZombieEnabled when you're debugging (in Xcode: Executables, your executable, Get Info, Arguments, NSZombieEnabled = YES). It'll complain if you use any objects after they've been released. Don't forget to turn it off later, otherwise your app will have huge leaks because all your old object IDs will be assigned to NSZombie (on purpose). I use: if ( [[processInfo environment] objectForKey:@"NSZombieEnabled"] ) { NSLog(@"**** NSZombieEnabled = YES ****"); }
Jeff
Just to be clear, setting self.strLongitude is the same as calling the setter, e.g. [self setStrLongitude:<value>].
gerry3
+1  A: 

I realize that this isn't directly answering your question, but why store the co-ordinates in strings?

Why not store them as, well, floats? For example,

@interface UserSettings
{
    float longitude;
}

@property (nonatomic, assign) float longitude;

@end

Also, your UserSettings class sounds very similar to NSUserDefaults...

Oliver White
I cant do a POST for web services with a float. So right before I make this call I bring the float into the class im making the webservices call with and then convert it to a string. Yes I can replace my userSettings class with NSUserDefaults but that doesn't answer my question. This low level programming is new to me and I'm trying to understand why this problem occurs.
Charles Peterson
So far I have determined that casting is relative to the class I cast it in. If I try to access the variable from a different class it doesnt retain its casting data type but reverts back to it's original. Who knows I'm all mucked up over this. I just want to know why.
Charles Peterson