2011-06-15 55 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