在非ARC项目中使用ARC库 [英] Using ARC library in non-ARC project

查看:171
本文介绍了在非ARC项目中使用ARC库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中未使用ARC技术.我正在尝试向其中添加 SBJson 库.我为所有带有SBJson前缀的文件设置了-fobjc-arc标志,但是在编译过程中,ARC语义问题(指向没有明确所有权的非const类型'id'的指针)出现在一个与SBJSon不相关的类头中.为什么不起作用?

ARC technology isn't being used in my project. I'm trying to add SBJson library to it. I set -fobjc-arc flag for all files which have SBJson prefix, but during compilation process ARC semantic issue (Pointer to non-const type 'id' with no explicit ownership) appears in one class header, which is not related to SBJSon. Why doesn't it work?

有错误的行:

    id *_controls;

由于项目的商业许可,我无法向您显示我的代码,感谢您的理解.

I cannot show you my code because of commercial license of project, I appreciate your understanding.

当我使用该库的旧版本(不带ARC)时,项目编译正常进行.

Project compilation goes normally when I use older version of this library (without ARC).

推荐答案

问题是您正在通过包含将ARC违反结构混合到ARC文件中.您不能将-fno-objc-arc应用于单个头文件,因为那没有意义.

The issue is that you are mixing an ARC violating construct into ARC'd files through inclusion. You can't apply -fno-objc-arc to a single header file because that doesn't make sense.

由于该文件位于头文件中,因此您将需要使用#if编译指示根据正在编译ARC还是非ARC .m文件在行为之间进行切换.

Since it is in a header file, you will either need to use an #if pragma to switch between behaviors depending on whether an ARC or non-ARC .m file is being compiled.

但是,更好的解决方案是完全消除ARC和非ARC的问题.有了该声明,它几乎必须是一个实例变量(尽管它可以在结构中).

A better solution, however, is to eliminate the issue entirely for both ARC and non-ARC. Given that declaration, it pretty much has to be an instance variable (though it could be in a struct).

如果它是一个ivar,则完全摆脱声明.如果不需要在公共API中公开它,则可以通过@property公开它,也可以将其移动到.m文件中.给定类型后,它实际上应该是带有公共API的私有实现细节,这使得访问内容的指针魔术性降低了.

If it is an ivar, get rid of the declaration entirely. Either expose it through @property or move it to the .m file if it does not need to be exposed in your public API. Given the type, it really should be a private implementation detail with public API that makes accessing the contents a bit less pointer-magic.

通常不建议使用C语言数组(指针数组)存储Objective-C类型.如果_controls确实需要作为可公开访问的东西公开(对项目中的其他类公开),则重构代码以使用集合类(即,通常使用内部仅供使用的NSArray*吸气剂>后备存储-例如UIView上的subviews.

In general, using C language arrays -- arrays of pointers -- to store Objective-C types is highly discouraged. If _controls does need to be exposed as a publicly accessible thing (public to the other classes in your project), then refactor your code to use a collection class (i.e. typically an exposed NSArray* getter with an internal-only NSMutableArray* backing store -- like subviews on UIView, for example).

这篇关于在非ARC项目中使用ARC库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆