帮帮我 - 打印全局nsstring变量时应用程序崩溃

时间:2011-06-15 05:02:35

标签: iphone ios nsstring

我有一个名为-( void ) loadUrl:(NSString *) SearchStr的方法。在那里我为全局nsstring赋值

变量。然后我使用[self checkReach]调用Apple的网络可达性类方法。

在该函数中,我可以打印该nsstring变量。

- (void) checkNetworkStatus:(NSNotification *)notice;称为metod。

在该功能中,我无法打印该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];

    }




}

1 个答案:

答案 0 :(得分:2)

您应该保留字符串值。在loadUrl:

中执行此操作
self.myEscapedUrlString  = SearchStr; 

而不是

myEscapedUrlString  = SearchStr;

最好将NSString属性声明为copy而不是retain。这样即使原始字符串是可变的,我们的实例也将保持不变。