iphone - ARC 警告 - "Not safe to remove an unused autorelease"

标签 iphone objective-c ios xcode

我收到以下警告,但我不知道从哪里开始,有什么想法吗?

NSObject+Be.m:36:3: [rewriter] it is not safe to remove an unused 'autorelease' message; its receiver may be destroyed immediately
NSObject+Be.m:35:40: [rewriter] NSInvocation's getReturnValue is not safe to be used with an object with ownership other than __unsafe_unretained
NSObject+Be.m:36:4: ARC forbids explicit message send of 'autorelease'

代码来自这里.... https://github.com/tylerneylon/moriarty/blob/c0d6daf65d86c22b8e5853aef00980f059c92fbc/NSObject%2BBe.m

#import "NSObject+Be.h"

@interface BeProxy : NSProxy {
  id target;
}

+ (BeProxy *)beProxyForClass:(Class)class;

- (void)forwardInvocation:(NSInvocation *)anInvocation;
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector;

@end

@implementation BeProxy

+ (BeProxy *)beProxyForClass:(Class)class {
  BeProxy *beProxy = [BeProxy alloc];
  beProxy->target = [class alloc];
  return beProxy;
}
//

// We assume the method called is an init method.  The return value
// may be a new value for self.
- (void)forwardInvocation:(NSInvocation *)anInvocation {
  [anInvocation setTarget:target];
  [anInvocation invoke];
  id object;
  [anInvocation getReturnValue:(void *)&object];   //HERE
  [object autorelease];                            //HERE
  [self release];
}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
  return [target methodSignatureForSelector:aSelector];
}

@end

//    


@implementation NSObject (Be)

+ (id)be {
  return [BeProxy beProxyForClass:[self class]];
}

+ (id)beInit {
  return [[[self class] new] autorelease];
}

- (id)beCopy {
  return [[self copy] autorelease];
}

@end

最佳答案

来自文档:

NSObject+Be This category is designed to help with memory management. Specifically, this makes it easy to only work with autoreleased objects outside of a small number of ownership-allowed methods.

所以发布的代码是为了帮助手动保留计数内存管理。那就不要用 ARC 编译它。

关于iphone - ARC 警告 - "Not safe to remove an unused autorelease",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12678588/

相关文章:

iphone - 设备选择了错误的视网膜图像

objective-c - 如果在for循环中声明?

objective-c - FBSDK 共享对话框无法在 IOS 11 上运行

objective-c - 从未调用可达性通知

iOS 应用使用 ShareKit 发布到 Facebook,为什么需要隐私政策?

iphone - 更改工具栏的色调

iphone - 使用按钮设置 AVAudioPlayer 音量

jquery - 当图像具有焦点时如何显示 iPhone 键盘?

ios - 在 App Store 中提交和验证应用程序

iphone - 如何将两个 UIBarButtonItem 添加到 UINavigationItem?