【问题标题】:Making older files compatible for ARC iOS使旧文件与 ARC iOS 兼容
【发布时间】:2012-07-01 05:11:51
【问题描述】:

为了使旧文件能够在 ARC 下编译,请设置开关 -fno-objc-arc 在构建阶段。

但只有 .m 文件列出,我可以添加 -fno-objc-arc。

但我也有一些导致 ARC 错误的 .h 文件。我需要将该开关设置为那些 .h 文件。但我没有在构建阶段看到它们。

那么,如何设置 .h 文件的开关?

这是 .h 文件的示例

\
__object__=__array__->arr[0]; for(NSUInteger i=0, num=__array__->num; i<num; i++,   __object__=__array__->arr[i])   \


typedef struct ccArray {
    NSUInteger num, max;
    id *arr;
} ccArray;

/** Allocates and initializes a new array with specified capacity */
static inline ccArray* ccArrayNew(NSUInteger capacity) {
    if (capacity == 0)
            capacity = 1; 

    ccArray *arr = (ccArray*)malloc( sizeof(ccArray) );
    arr->num = 0;
    arr->arr =  (id*) malloc( capacity * sizeof(id) );
    arr->max = capacity;

    return arr;
}

 static inline void ccArrayRemoveAllObjects(ccArray *arr);

 /** Frees array after removing all remaining objects. Silently ignores nil arr. */
 static inline void ccArrayFree(ccArray *arr)
{
    if( arr == nil ) return;

    ccArrayRemoveAllObjects(arr);

    free(arr->arr);
    free(arr);
 }

 /** Doubles array capacity */
  static inline void ccArrayDoubleCapacity(ccArray *arr)
 {
    arr->max *= 2;
    id *newArr = (id *)realloc( arr->arr, arr->max * sizeof(id) );
    // will fail when there's not enough memory
     NSCAssert(newArr != NULL, @"ccArrayDoubleCapacity failed. Not enough memory");
    arr->arr = newArr;
}

  /** Increases array capacity such that max >= num + extra. */
   static inline void ccArrayEnsureExtraCapacity(ccArray *arr, NSUInteger extra)
 {
        while (arr->max < arr->num + extra)
            ccArrayDoubleCapacity(arr);
}

/** shrinks the array so the memory footprint corresponds with the number of items */
static inline void ccArrayShrink(ccArray *arr)
{
NSUInteger newSize;

    //only resize when necessary
    if (arr->max > arr->num && !(arr->num==0 && arr->max==1))
    {
            if (arr->num!=0) 
            {
                    newSize=arr->num;
                    arr->max=arr->num; 
            }
            else 
            {//minimum capacity of 1, with 0 elements the array would be free'd by realloc
                    newSize=1;
                    arr->max=1;
            }

            arr->arr = (id*) realloc(arr->arr,newSize * sizeof(id) );
            NSCAssert(arr->arr!=NULL,@"could not reallocate the memory");
    }
} 

(id*) 导致问题。

【问题讨论】:

  • 如果 .m 具有 -fno-objc-arc 标志,则 .h 文件中不应出现 ARC 错误。
  • 有些.h文件没有对应的.m文件。
  • 即使他们没有匹配名称的 .m 文件,我希望他们需要将 #include/#import 放入真实的源文件中才能成为编译的一部分。没有?
  • 嗯,这是另一个 B.h 文件,该文件导入了具有 ARC 问题的 A.h 文件。我把开关放在 B.m 文件上。
  • 我不确定这是否真的相关,但看看这个问题:stackoverflow.com/questions/7308090/…

标签: ios automatic-ref-counting


【解决方案1】:

您可以将 .h 文件拆分为 .h/.m 文件对吗?这样你就可以把方法体放到.m文件中,用-fno-objc-arc编译,随意包含.h文件。

【讨论】:

    猜你喜欢
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多