ARC forbids explicit message send of 'retain&#

2019-01-13 19:10发布

I'm using this very simple code from the Apple Guide:

NSMutableData *receivedData;

// Create the request.
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]
                        cachePolicy:NSURLRequestUseProtocolCachePolicy
                    timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
    // Create the NSMutableData to hold the received data.
    // receivedData is an instance variable declared elsewhere.
    receivedData = [[NSMutableData data] retain];
} else {
    // Inform the user that the connection failed.
}

But for the line receivedData = [[NSMutableData data] retain]; Xcode gives me an error: PushController.m:72:25: ARC forbids explicit message send of 'retain'

How to deal with it? I'm using the Xcode 4.4.1

3条回答
我欲成王,谁敢阻挡
2楼-- · 2019-01-13 19:42

You are currently using the ARC to reference count for you. (ARC is "Automatic Reference Counting", a new feature to iOS 5). Therefore you do not need to manually retain or release. You can either remove your retain calls all together or turn off ARC by doing the following:

Click on the name of the project on the navigation view in the left side, go to Targets -> Build Phases and add -fno-objc-arc to the "compiler flags" for any relevant files.

See here for info on removing.

See here for basic info on ARC.

查看更多
疯言疯语
3楼-- · 2019-01-13 19:50

Turning ARC on or off is a project level setting, if you need to have code that works in both modes you need to use

#if __has_feature(objc_arc)
//dont do a release or a retain or autorelease
#else
//do the release
#endif
查看更多
一纸荒年 Trace。
4楼-- · 2019-01-13 19:55

I solved the problem as below. The code is for Objective-C.

  1. Whichever file you wrote a method for getting images from CIImage to CGImageRef:

    CGImageRef cgImage = [_ciContext createCGImage:currentImage fromRect:[currentImage extent]];
    

    make that file as non ARC. Go to Project -> BuildPhase -> ComplieSources -> Your File -> add "-fno-objc-arc" to your file.

  2. If you have .pch file in your project, make the following line comment:

    #if !__has_feature(objc_arc)
    #error This file must be compiled with ARC.
    #endif
    
  3. Go to the method which is used for creating images using the following function:

    CGImageRef cgImage = [_ciContext createCGImage:currentImage fromRect:[currentImage extent]];
    

    Declare _ciContext like this:

    1. In the .h file, declare:

      @property (strong, nonatomic)   CIContext* ciContext;
      
    2. In your method, create the context:

      EAGLContext *myEAGLContext = [[EAGLContext alloc]
              initWithAPI:kEAGLRenderingAPIOpenGLES2];
      _ciContext = [CIContext contextWithEAGLContext:myEAGLContext      options:nil];
      
    3. Use the _ciContext for creating images.

    4. Write the following method in the same file:

       -(void)dealloc
       {
          [super dealloc];
          [EAGLContext setCurrentContext:nil];
       }
      
查看更多
登录 后发表回答