0

How can i call method with arguments into another method.

I have a problem in objective c class. My code is

- (void)locationUpdate:(CLLocation *)location {

    location.coordinate.longitude];    
    googleUrl=[[NSString alloc]initWithFormat:@"https://maps.googleapis.com/maps/api/place /search/xml?location=%f,%f&radius=500&name=the%20money&sensor=false&  key=AIzaSyCcC9pmri9XGOgyhjoHQq37cmcfgsfb6bBZe80",location.coordinate.latitude,location.coordinate.longitude];

}



-(void)ParseXML_of_Google_PlacesAPI {

    NSURL *googlePlacesURL = [NSURL  URLWithString:googleUrl];

    NSData *xmlData = [NSData dataWithContentsOfURL:googlePlacesURL];
}

I want to put googleUrl value in parseXML method

sidyll
  • 57,726
  • 14
  • 108
  • 151
Mann
  • 5,477
  • 6
  • 45
  • 57

2 Answers2

3

You can change the signature of your parseXML_of_Google_PlacesAPI method as follows:

-(void) ParseXML_of_Google_PlacesAPI: (NSString*) googleUrl {...}

Furthermore, modify the implementation of the method as:

NSURL *googlePlacesURL = [NSURL URLWithString:googleUrl];
return [NSData dataWithContentsOfURL:googlePlacesURL];

Then, you might call the method as follows:

// your previous code with the location
NSData* googleData = [self ParseXML_of_Google_PlacesAPI:googleUrl];

Couple of points:
- The convention for method names is that it starts with a lowercase letter.
- On the higher level, what you are trying to do is to encapsulate certain functionality within a method (parseXML). This is a very good practice as it will make your code more readable. One thing to be careful about is to pick good method names; I would pick getXMLDataOfURL:(NSString*) url as the method name. That would clearly identify what you are trying to achieve in this method.
- A healthy discussion for best practices regarding methods can be found here.

Community
  • 1
  • 1
Guven
  • 2,280
  • 2
  • 20
  • 34
  • 3
    Method names starting with `get…` usually take a pointer parameter that they use to store a result, cf. [`-[NSData getBytes:range:]`](http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/Reference/Reference.html#//apple_ref/doc/uid/20000172-CIACCFDG). – gcbrueckmann Aug 11 '11 at 20:22
  • Thanks @gcbrueckmann, this was something that I didn't know before. Will update my code as well! – Guven Sep 25 '11 at 21:06
2

Your question is somewhat unclear. I'm assuming that you're asking how you can pass the googleUrl value from the locationUpdate method to the ParseXML_of_Google_PlacesAPI method.

If that's the case, then you'll need to add a NSString parameter to the signature of the latter method.

-(void) ParseXML_of_Google_PlacesAPI:(NSString *) googleUrl { ... }

You can then invoke this method by using the following syntax from the locationUpdate method:

[self ParseXML_of_Google_PlacesAPI:googleUrl];

Does that help?

(btw, if you do this, there's probably no need to set googleUrl as an ivar/property. Just declare it as a NSString in the scope of the locationUpdate method.)

csano
  • 13,266
  • 2
  • 28
  • 45