Help me - App crashes when printing global nsstring variable
I have a method named -( void ) loadUrl:(NSString *) SearchStr
.In that i assign value to the global nsstring
variable. Then i call Apple's network reachability class methods using [self checkReach]
.
In that function i can able to print that nsstring variable. and again the
- (void) checkNetworkStatus:(NSNotification *)notice;
metod is called.
In that function i cant able to print that nnstring varibale. This crashes my application.
I dont know, why this global variable is not accesible in the - (void) checkNetworkStatus:(NSNotification *)notice;
method?
Any helps would be greatly appreciated!
Thanks for your time.
Find the code below;-
#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开发者_Go百科(@"%@ 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];
}
}
You should retain the string value. Do this in loadUrl:
self.myEscapedUrlString = SearchStr;
instead of
myEscapedUrlString = SearchStr;
And it is preferable to declare NSString
properties to be copy
rather than retain
. This way even if the original string were mutable, our instance will remain unchanged.
精彩评论