2013-04-04 69 views
-2

我創建了一個獲取變量內容的getter,但它不起作用。我的變量有問題

下面的代碼:

-(void)documentURLReceived:(NSURL *)url{ 

    _getUrl = [[NSURL alloc] init]; 
    _getUrl = url; 
    NSLog(@"Result: %@", _getUrl); 
} 
-(void)getUrl{ 
    NSLog(@"getUrl: %@", _getUrl); 
} 

在這裏,結果在康壽:

結果:http://www.google.com

的getURL(空)

我不明白爲什麼!

這裏我的財產:

@property (strong, nonatomic) NSURL *getUrl; 

感謝您的幫助:)

+1

,你叫[自我的getURL]方法? – DharaParekh 2013-04-04 11:58:18

+1

ARC或MRC? ?? – 2013-04-04 11:58:47

+1

檢查命名約定和內存管理的蘋果文檔。兩者都很重要。 – vikingosegundo 2013-04-04 12:00:32

回答

2

如果你使用的是ARC和最新的Xcode - 擺脫getURL方法 - 你不應該覆蓋這個,除非你需要做自定義邏輯,此外,你的getURL方法什麼也沒有返回並且是一個void方法 - 所以變量是最有可能設置的,但是你的方法覆蓋了自動生成的getter,並且什麼都不返回。

在Xcode

做了測試。在頭文件我有:

@property (nonatomic, strong) NSURL *getURL; 

,然後在實現文件我有:

- (void)documentURLReceived:(NSURL *)url 
{ 
    _getURL = url; 
    NSLog(@"%@",_getURL); 
} 

,其輸出:

2013-04-04 23:29:27.681 VitalityDesignTestSuite [90 518:C07] http://www.google.com

okie道基我給你寫了一個例子:

http://bit.ly/10yWb36

使用那些你可以去:

MyViewController *mvc = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil]; 
//Now you can set the myURL variable directly 
[mvc setMyURL:[NSURL URLWithString:@"http://www.google.com"]]; 
NSLog(@"Direct Setting: %@",mvc.myURL); 

//OR you can call the method your wrote 
[mvc documentDidReceiveURL:[NSURL URLWithString:@"http://www.google.com"]]; 
NSLog(@"Selector Setting: %@",mvc.myURL); 

輸出:

2013-04-04 23:39:09.407 VitalityDesign [92197:c07]直接設置:http://www.google.com 2013-04-04 23:39:09。408 VitalityDesign [92197:C07]選擇器設置:http://www.google.com

希望這有助於

+0

好的..... ..... – 2013-04-04 12:06:50

+0

我試着用XCode自動生成的方法,但它不起作用:/ – Lapinou 2013-04-04 12:19:52

+0

@Lapinou:嘗試改變方法的名稱' - (void)getUrl {'to' - ( void)getUrlMethod {'然後說 – 2013-04-04 12:21:15

-3

首先你得到的網址什麼?

如果沒有, - >在它

的工作,如果是的話,儘量做到以下幾點:

_getUrl = [[NSURL alloc] init]; 
_getUrl = url; 
[_getUrl retain]; 
NSLog(@"Result: %@", _getUrl); 

欣賞節目!

+0

在第二行上確定新值時,您(和OP)正在泄漏在第一行創建的空網址。 – 2013-04-04 12:01:28

+0

我確實知道@DavidRönnqvist,但有時候這個技巧對我很有用。 – 2013-04-04 12:02:26

+0

@ForamMukundShah,你應該調查爲什麼這個技巧適合你。因爲它只是錯誤 – vikingosegundo 2013-04-04 12:03:41