我正在學習如何編程,並從Objective C開始。我試圖準確理解當從一個方法內分配對象時會發生什麼。目標C:從方法返回對象
-(Fraction *) add: (Fraction *) f
{ //'result' will store the result of the addition
Fraction *result = [[Fraction alloc]init];
result.numerator = (numerator*f.denominator + denominator*f.numerator);
result.denominator = denominator*f.denominator;
[result reduce];
return result;
}
我知道我可以創建一個對象來存儲「結果」返回,當它在,
tempStorageObject = [aFraction add: bFraction];
的,我是那麼釋放它負責,但是當我穿上」發生了什麼t存儲它,如:
[aFraction add: bFraction];
NSLog(@"%i/%i", result.numerator, result.denominator); //result object not declared in main
我被告知我有一個未聲明的標識符。我明白了,但是使用我的'add'方法後會發生什麼'結果'。它在哪裏?我應該不能訪問它的變量,因爲它是在方法中創建並返回的?顯然不是,但我不清楚爲什麼。我試過重新閱讀我的書並搜索這個論壇,但我找不到明確的答案。謝謝。 (第一篇文章)
+1的真棒回答。 – esqew