2

Firstly, sorry for my English... I want to write a program which is going to calculate distances between two cities. For example, in UITextField we write Paris, and in the second UITextField we write the second , "London. And We have a base of longitude s and latitude s of all Cities. Our program has distance formula which uses these four numbers. We know this formula.

When the user insert a name in UITextField i want to TAKE THIS .text AND COMPARE IT WITH OUR BASE. How to do that?? I can do a program like this but it`s ....stupid:

@interface City : UIViewController{

    IBOutlet UITextField *city1;

    IBOutlet UITextField *city2;
}

@property (nonatomic, retain) IBOutlet UITextField *city1;
@property (nonatomic, retain) IBOutlet UITextField *city2;

-(void) calculate;

@end




#import "City.h"

@implementation City

@synthesize city1, city2;

-(void) viewDidLoad
{
    // to check changes in textfields i`m using NSTimer, I know it`s stupid but I haven`t learned how to make it in different way

    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(calculate) userInfo:nil repeats:YES];

}

-(void) obliczenia
{
    double distance;


    if([city1 is EqualToString:@"Paris"] && [city2 is EqualToString:@"London"])
    {

        double Paris_lat = x1;
        double Paris_lon = y1;
        double London_lat = x2;
        double London_lon = y2;

        distance =...; // we know the formula for distance but this is not important for now.
              // distance shows in some label but this is not a point of my problem. 

    }
}

It runs but when we have few cities. But when we have thousand cities, writing code would be a nonsens.

I am starting with iPhone programming. Thank you for patience and please, help me. It s very important but I can't find a solution.

Abizern
  • 146,289
  • 39
  • 203
  • 257
Milosz
  • 21
  • 2

3 Answers3

4

Take a look at this guide: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/PropertyLists/AboutPropertyLists/AboutPropertyLists.html

Basically, put all of your data into a property list, like so:

<dict>
    <key>Paris</key>
    <dict>
        <key>x</key>
        <real>20.2</real>
        <key>y</key>
        <real>30.4</real>
    </dict>
...
</dict>

And then load it like this:

NSDictionary * data = [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"NAMEOFPLIST" ofType:@"plist"]];
NSDictionary * paris = [data objectForKey:@"Paris"];
float xparis = [[paris objectForKey:@"x"] floatValue];
float yparis = [[paris objectForKey:@"y"] floatValue];

In your case you'd do something like this:

NSDictionary * data = [[NSDictionary alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"NAMEOFPLIST" ofType:@"plist"]];
NSDictionary * city1Data = [data objectForKey:city1.text];
NSDictionary * city2Data = [data objectForKey:city2.text];
if (city1Data != nil && city2Data != nil) {
     // Do what you need...
}

And then do what you need with the data.

Philippe Sabourin
  • 8,066
  • 3
  • 31
  • 46
  • Thank you Philippe! I think this solution is what I need. I tried this and (also i tried to understand everything about plist) (propably) we can not use but only (which is float) or . I`m right,aren`t I? And we also can not use : double xparis = [paris objectForKey:@"x"]; but float xparis - [[paris objectForKey:@"x"] floatValue]; – Milosz Jan 03 '12 at 18:29
  • Yeah i was guessing for the double thing, sounds right. You can convert it to a float that way, yes. I'll edit my answer. – Philippe Sabourin Jan 03 '12 at 19:12
  • Also, you understand that you don't specifically name paris anywhere in your code right? You'd say NSDictionary * city = [data objectForKey:city1]; and check if its nil. If it isn't you know it's a legit city. I'll add this too. – Philippe Sabourin Jan 03 '12 at 19:14
1

You are probably better off using a picker view to display the available cities, or use a live search in your textview to autocomplete, or provide selections for your cities.

If you have a constrained set of inputs (in this case, the names of the cities for which you have long, lat values) it's a better idea to constrain the user's input to these values.

Abizern
  • 146,289
  • 39
  • 203
  • 257
0

You can create a Array with all Country Names and compare each Country Name in the Array with your input.

  NSArray *countries = [NSArray arrayWithObjects:@"Berlin", @"Rom", nil];

  for (NSString *c in countries) {
    if ([city1 isEqualToString:c]) {
      //do something
    }
  }
CarlJ
  • 9,461
  • 3
  • 33
  • 47