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

hervey

macrumors newbie
Original poster
Sep 23, 2009
19
0
This is the essence of what I attempting: a way to change sign to negative for the southern hemisphere, see commented code at bottom. Obviously, I'm just learning Cocoa/ObjC.

The lat/lon are in an SQLite database.


#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSString *stringLat1 = @"N37-21.2"; //string: sample latitude from database

NSString *hemi = [stringLat1 substringWithRange:NSMakeRange( 0, 1) ]; // string: Hemisphere
NSString *degLat1 = [stringLat1 substringWithRange:NSMakeRange( 1, 2) ]; // string: degree of latitude
NSString *minLat1 = [stringLat1 substringWithRange:NSMakeRange( 4, 4) ]; // string: minute of latitude

float intMinLat1 = [minLat1 floatValue]/60; // int: minute of latitude
float intDegLat1 = [degLat1 floatValue]; //int: degree of latitude
float intPt1 = intMinLat1 + intDegLat1; // int: complete latitude

float point1Lat;
point1Lat = intPt1; /* the completed latitude (37.353333) for the northern hemisphere, a positive number vice a negative number for the southern hemi. */

NSLog(@"Pt1 hemisphere is %@, lat is %@ degrees, lon is %@ minutes, a %f decimal value. The point1Lat is %f",hemi,degLat1, minLat1, intMinLat1, point1Lat);

[pool drain];
return 0;
}

/*

if (hemi == @"N")
{
point1Lat = intPt1;
}
else if (hemi == @"S")
{
point1Lat = intPt1 - 2 * intPt1;
}
*/
 
That's not how NSString comparison works in objective-C. In languages like C++ you can "overload" the == operator so it does the proper thing... but in Objective-C you must do:
if ([@"N" isEqualToString:hemi]) {
...
}

-Lee
 
Lee has the right of it, but here's an even simpler way to accomplish what you're doing:

Code:
if ([hemi hasPrefix:@"N"]) {
    ...
 
Also,

Code:
point1Lat = intPt1 - 2 * intPt1;

is not how I would negate a number.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.