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

标签 iphone ios nsstring

我有一个名为 -( void ) loadUrl:(NSString *) SearchStr 的方法。在其中我将值分配给全局 nsstring

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

在那个函数中,我可以打印那个 nsstring 变量。又是

- (void) checkNetworkStatus:(NSNotification *)notice; 方法被调用。

在该函数中,我无法打印那个 nnstring 变量。 这会使我的应用程序崩溃。

我不知道,为什么这个全局变量在 - (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];

    }




}

最佳答案

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

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

代替

myEscapedUrlString  = SearchStr;

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

关于iphone - 帮助我 - 打印全局 nsstring 变量时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6353236/

相关文章:

iphone - 为 C++ 库制作 Objective-C 包装器

Objective-C NSString pathExtension for example.tar.gz?

ios - app delege didfinishlaunching 在 iOS 状态恢复期间未调用

ios - 如何在应用程序中存储图像(来自画廊/相机)以便稍后调用?

ios - 将一个 NSString 常量分配给另一个

iphone - 是否可以在 iPhone 应用程序中覆盖 sqlite 数据库

iphone - 在 iPhone 上下载、保存和播放 mp3

jquery - 在iPhone上使用JW Player,音频文件只能在quicktime播放器中播放

c# - 如何使用 Xamarin 中的 CallDirectoryExtension 创建一个提供来电显示的应用程序?

ios - map View - 移动 map View 后的当前位置