Mann
Mann

Reputation: 5507

Problem in passing arguments between functions iphone

In my application I have location function:

- (void)locationUpdate:(CLLocation *)location {    
  lati = [[NSString alloc]initWithFormat:@"%f", location.coordinate.latitude] ;
  longi = [[NSString alloc ]initWithFormat:@"%f", location.coordinate.longitude ];    
  [self ParseXML_of_Google_PlacesAPI:googleUrl];
}

...and I have another function to which I want to pass lati and longi:

-(void)ParseXML_of_Google_PlacesAPI:(NSString *)_googleUrl { 
  //use lati and longi in this line 
  googleUrl=[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/xml?location=%f,%f&radius=500&name=asda&sensor=false&key=mykey",lati,longi];
  NSURL *googlePlacesURL = [NSURL  URLWithString:googleUrl ];
  NSData *xmlData = [NSData dataWithContentsOfURL:googlePlacesURL];
  xmlDocument = [[GDataXMLDocument alloc]initWithData:xmlData options:0 error:nil];
  NSArray *arr = [xmlDocument.rootElement elementsForName:@"result"];
  placesOutputArray=[[NSMutableArray alloc]init];
  for(GDataXMLElement *e in arr ){        
          [placesOutputArray addObject:e];
  }
}

How can I do that? I just need the values of lati and longi in the ParseXML_of_Googe_PlacesApi: method. Everything else is working fine

Upvotes: 0

Views: 104

Answers (4)

TechZen
TechZen

Reputation: 64428

If you don't want to rewrite the method or you need to use the variables in more than one method, make lati and longi properties of the class this code appears in.

In the .h file you would define:

@property(nonAtomic, retain) NSString *lati;
@property(nonAtomic, retain) NSString *longi;

.. and in the .m file you would:

@synthesize lati, longi;

Then you would use them like:

- (void)locationUpdate:(CLLocation *)location {
  self.lati = [[NSString alloc]initWithFormat:@"%f", location.coordinate.latitude] ;
  self.longi = [[NSString alloc ]initWithFormat:@"%f", location.coordinate.longitude ];    
  [self ParseXML_of_Google_PlacesAPI:googleUrl];
}

... and:

-(void)ParseXML_of_Google_PlacesAPI:(NSString *)_googleUrl {  
  googleUrl=[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/place/search/xml?location=%f,%f&radius=500&name=asda&sensor=false&key=mykey",self.lati,self.longi];
  NSURL *googlePlacesURL = [NSURL  URLWithString:googleUrl ];
  NSData *xmlData = [NSData dataWithContentsOfURL:googlePlacesURL];
  xmlDocument = [[GDataXMLDocument alloc]initWithData:xmlData options:0 error:nil];
  NSArray *arr = [xmlDocument.rootElement elementsForName:@"result"];
  placesOutputArray=[[NSMutableArray alloc]init];
  for(GDataXMLElement *e in arr ){        
          [placesOutputArray addObject:e];
  }
}

Upvotes: 2

Lily Ballard
Lily Ballard

Reputation: 185671

Rewrite your method as

- (void)parseXMLOfGooglePlacesAPI:(NSString *)url latitude:(NSString *)latitude longitude:(NSString *)longitude;

and then call it as

[self parseXMLOfGooglePlacesAPI:googleURL latitude:late longitude:longi];

Upvotes: 2

Nathanial Woolls
Nathanial Woolls

Reputation: 5291

Change your declaration to:

-(void)ParseXML_of_Google_PlacesAPI:(NSString *)_googleUrl withLat:(NSString*)lati andLong:(NSString*)longi {

Then change your calling code:

[self ParseXML_of_Google_PlacesAPI:googleUrl withLat:lati andLong:longi];

Upvotes: 3

user281300
user281300

Reputation: 1457

You need to modify the ParseXML_of_Google_PlacesAPI:(NSString *)_googleUrl method to take other arguments:

i.e )ParseXML_of_Google_PlacesAPI:(NSString *)_googleUrl long:(float) longitude lat:(float) latitude

You can use float if they floats or replace the float with NSString if they are nsstrings like in your method.

You can then use the nsstringformat to reformat your URL.

Upvotes: 2

Related Questions