开发者

Objective c calling method with argument into another method [closed]

开发者 https://www.devze.com 2023-03-28 01:31 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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 dataWithC开发者_如何学ContentsOfURL:googlePlacesURL];
}

I want to put googleUrl value in parseXML method


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.


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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号