if statement - inop.

Discussion in 'Mac Programming' started by hervey, Sep 23, 2009.

  1. hervey macrumors newbie

    Joined:
    Sep 23, 2009
    #1
    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;
    }
    */
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    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
     
  3. hervey thread starter macrumors newbie

    Joined:
    Sep 23, 2009
  4. kpua macrumors 6502

    Joined:
    Jul 25, 2006
    #4
    Lee has the right of it, but here's an even simpler way to accomplish what you're doing:

    Code:
    if ([hemi hasPrefix:@"N"]) {
        ...
    
     
  5. hervey thread starter macrumors newbie

    Joined:
    Sep 23, 2009
    #5
    kpua -

    Thanks, also worked great; clean.

    - Herv
     
  6. Sander macrumors 6502

    Joined:
    Apr 24, 2008
    #6
    Also,

    Code:
    point1Lat = intPt1 - 2 * intPt1;
    is not how I would negate a number.
     

Share This Page