2012-04-16 178 views
-1

在我的應用程序中,我需要調用一個函數,該函數依次調用許多函數。問題是我打電話給getweather函數,它啓動startprocess,然後過程完成。 processCompleted方法由rssparser調用,該值在processCompleted方法的末尾可用。等待函數返回值

-(void) getWeather: (NSDictionary *) dictionary { 
    _rssParser = [[BlogRssParser alloc]init]; 
    self.rssParser.address = addressInterestedIn; 
    self.rssParser.delegate = self; 
    [[self rssParser]startProcess]; 
} 

//Delegate method for blog parser will get fired when the process is completed 

-(void)processCompleted 
{ 
    NSLog(@"the rssItems array is %@", [[[self rssParser]rssItems] description]); 
    int woeid = [[[[self rssParser] rssItems] objectAtIndex:0] intValue]; 
    // get weather update from yahoo 
    NSLog(@"temperature option %d", [[[NSUserDefaults standardUserDefaults] objectForKey:@"temperature"] intValue]); 
    SCYahooWeatherParser *parser = [[SCYahooWeatherParser alloc] initWithWOEID:woeid weatherUnit: [[[NSUserDefaults standardUserDefaults] objectForKey:@"temperature"] intValue]]; 
    //parse the returned xml from yahoo 
    SCWeather *result = [parser parse]; 
    [parser release]; 
    NSLog(@"the conditionDataDict is %@", [result.conditionDataDict description]); 
} 

如何獲得由processCompleted方法的返回值,因爲我已經叫getWeather功能。

回答

1

在Objective C,就像在任何結構化語言,當你調用返回像

NSInteger x = [self yourFunction]; 
[self somethingElse:x]; 

不執行下一個指令,直到函數返回值的值的函數。換句話說,在yourFunction結束併爲x返回一個值之前,somethingElse不會被執行。

+0

是否已編輯該問題。請檢查一下。 – 2012-04-16 09:46:21

+0

我的答案保持不變,請仔細閱讀,您沒有使用任何選擇器或後臺線程,因此總是獲取返回的值,無需同步這些函數。 – 2012-04-16 13:52:11

0

如果你的函數鏈被稱爲主線程,您的應用程序應該自動在繼續執行下一個操作之前,等待進程結束。然後,你不需要做任何事情來獲得你的目標。

請注意,如果您的程序非常繁重,您的應用程序將凍結,直到結束。這是一個很好的自定義執行過程,可以阻止你的應用程序在另一個線程。