2011-06-15 63 views
0

崩潰時我有一個名爲-(void) loadUrl:(NSString *) SearchStr。在方法,我將值分配到全局的NSString幫我 - 應用程序打印全局的NSString變量

變量。然後我使用[self checkReach]調用Apple的網絡可達性類方法。

在該函數中,我可以打印該nsstring變量。並再次調用metod被調用的

- (void) checkNetworkStatus:(NSNotification *)notice;

在那個函數中,我無法打印nnstring varibale。 這使我的應用程序崩潰。

我不知道,爲什麼這個全局變量在- (void) checkNetworkStatus:(NSNotification *)notice;方法中不可訪問?

任何幫助將不勝感激!

謝謝你的時間。

找到下面的代碼; -

#in the header file. 
@property(nonatomic, retain) NSString* myEscapedUrlString; 

#in the m file 
@synthesize myEscapedUrlString; 

-(void) loadUrl:(NSString *) SearchStr 
{ 
    myEscapedUrlString = SearchStr; 

    NSLog(@"%@ ###########",myEscapedUrlString); 

    [self checkReach]; 
} 

-(void)checkReach 
//---------------- 
{ 
    NSLog(@"%@ nnnnnnnnnnnnnnnnn",myEscapedUrlString); 

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(load_View) name: UIApplicationDidBecomeActiveNotification object:nil]; 

    // check for internet connection 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil]; 

    internetReachable = [[Reachability reachabilityForInternetConnection] retain]; 
    [internetReachable startNotifier]; 

    // check if a pathway to a random host exists 
    hostReachable = [[Reachability reachabilityWithHostName: @"www.testn.com"] retain]; 
    [hostReachable startNotifier]; 

} 

- (void) checkNetworkStatus:(NSNotification *)notice; 
//--------------------------------------------------- 
{ 
     NSLog(@"%@ nnnnnnnnnnnnnnnnn",myEscapedUrlString); 
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus]; 

    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus]; 

    [[NSNotificationCenter defaultCenter] removeObserver:self 
                name:kReachabilityChangedNotification object:nil]; 


    if (internetStatus == NotReachable) 
    { 
     NSLog(@"The internet is down."); 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Network Status" 
                 message:@"Internet connection is not availbale. Check your connections." delegate:self 
               cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; 

     [alert show]; 
     [alert release]; 


    } 
    else if (hostStatus == NotReachable) 
    { 
     NSLog(@"A gateway to the host server is down."); 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Network Status" 
                 message:@"Cannot connect to Aghaven server. Please try again later." delegate:self 
               cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; 

     [alert show]; 
     [alert release]; 

    } 




} 
+0

查看日誌,並請告訴我們它崩潰的原因 – 2011-06-15 05:32:13

回答

2

您應保留字符串值。爲此在loadUrl:

self.myEscapedUrlString = SearchStr; 

,而不是

myEscapedUrlString = SearchStr; 

而且最好是申報NSString性質是copy而非retain。這種方式即使原始字符串是可變的,我們的實例將保持不變。

+0

- 非常感謝您的幫助,真的......你可以請讓我知道什麼是可變的意義? – user198725878 2011-06-15 05:41:04

+0

'NSString'實例不能更改。 'NSString'的可變版本是'NSMutableString',它是'NSString'的子類。這個類的實例的內容可以改變。這與'NSString'不同,您將在其中創建一個新實例並指向它。 – 2011-06-15 05:44:28