【问题标题】:define a struct in iOS6 with ARC使用 ARC 在 iOS6 中定义一个结构
【发布时间】:2012-12-20 15:01:14
【问题描述】:

简单问题:我学习了 C++ 和 struct 的语法。所以我想在我的 iOS 应用程序中使用结构来存储数据,但是 ARC 不允许通常使用结构,我的问题有什么解决方案吗?

struct fruit {
    int a;
};

_

{
struct fruit apple;
apple.a = 1;
return 0;
}

例如是否可以使用 NSMutableArray 解决此问题? 顺便说一句,我必须在哪里定义? .m 文件还是 .h 文件?

问候 CTS

【问题讨论】:

    标签: objective-c struct automatic-ref-counting


    【解决方案1】:

    当然,即使在 ARC 中,也允许在 Objective-C 中使用结构。您指定的结构应该绝对没有问题:

    struct fruit {
        int a;
    };
    

    ARC 出现的唯一问题是包含 Objective-C 对象的结构,因为 ARC 不知道它必须如何管理这些对象。但是你可以使用__unsafe_unretained,让编译器知道“你知道,你在做什么”:

    struct foo {
        __unsafe_unretained NSString *bar;
    };
    

    bar 在这种情况下只是一个指针,而不是强引用,由 ARC 管理。 Apple 建议在这些情况下尽可能使用简单的类。

    【讨论】:

      猜你喜欢
      • 2022-01-02
      • 1970-01-01
      • 2015-05-24
      • 1970-01-01
      • 1970-01-01
      • 2016-08-17
      • 1970-01-01
      • 2014-04-17
      • 1970-01-01
      相关资源
      最近更新 更多