ARC不允许将int隐式转换为NSString,并且不允许将整数转换为指向NSString的整数到指针转换

时间:2014-08-04 09:22:39

标签: objective-c nsstring

我在使用开发人员在本网站上提供的一些代码时遇到了一些麻烦。该代码旨在检查互联网连接。这是下面的代码:

@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];
    }
});

此代码放在viewDidLoad中,但我在Implicit conversion of int to NSString is disallowed with ARC时遇到Incompatible integer to pointer conversion assigning to 'NSString *' from 'int'self.connected = (s != nil);两个错误。

任何帮助都会很棒!

2 个答案:

答案 0 :(得分:1)

您的媒体资源connected的类型为NSString *。你不能给它分配一个bool(self.connected = (s != nil);)。

我认为您打算将您的财产声明为BOOL

@property (nonatomic, assign, getter = isConnected) BOOL connected;

在幕后,BOOL只是int,因此在为NSString *类型变量分配int时,编译器会理解您要直接更改指针的值,这在ARC中是不允许的(反正是一个坏主意);因此错误信息。

答案 1 :(得分:0)

self.connected = (s != nil);

(s != nil)将返回0或1而不是NSString对象