I'm having a bit of trouble with some code a developer gave to on this website. The code is meant to check for an internet connection. This is the code below:
@property (nonatomic, copy, getter = isConnected) NSString *connected;
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
NSURL *url = [NSURL URLWithString:@"http://www.apple.com/"];
NSString *s = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
dispatch_async(dispatch_get_main_queue(), ^{
// hide the activity indicator
self.connected = (s != nil);
[[UIApplication sharedApplication] endIgnoringInteractionEvents];
if (self.isConnected)
{
NSLog(@"self.connected == YES");
}
else
{
NSLog(@"self.connected == NO");
NSString *alertMessage = @"In order to load images, you need an active Internet connection. Please try again later!";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sorry!" message:alertMessage delegate:nil cancelButtonTitle:@"Cancel"otherButtonTitles:nil];
[alert show];
}
});
This code is placed in the viewDidLoad
but I keep getting 2 errors saying Implicit conversion of int to NSString is disallowed with ARC
and Incompatible integer to pointer conversion assigning to 'NSString *' from 'int'
for self.connected = (s != nil);
.
Any help would be great!
Your property connected
is of type NSString *
. You can't assign a bool to it ( self.connected = (s != nil);
).
I think what you intended to do is declare your property as BOOL
:
@property (nonatomic, assign, getter = isConnected) BOOL connected;
Under the hood, BOOL
is just an int
, so when assigning an int to an NSString *
typed variable, the compiler understand that you want to change the value of your pointer directly, which is disallowed in ARC (and is a bad idea anyway); hence the error message.
In
self.connected = (s != nil);
(s != nil)
will return 0 or 1 not an NSString object
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.