【问题标题】:Objective C: Releasing int/float properties目标 C:释放 int/float 属性
【发布时间】:2011-07-12 07:53:42
【问题描述】:

我在理解某些事情时遇到了一些麻烦。我的应用中有很多属性:

@property (nonatomic, retain) AVAudioPlayer *audioPlayer;
@property (readwrite) NSInteger buttonCount;
@property (nonatomic, retain) NSString *soundSelected;
@property (readwrite) float fadeDecrease;
@property (readwrite) float fadeDelay;

这些显然都是在我的 .m 文件中合成的。 然而,虽然 audioPlayer 和 soundSelected 在 dealloc 中可以正常释放,但 int buttonCount 会给出以下警告: “无效的接收器类型'NSInteger'” 浮点数实际上使编译器哭泣: “无法转换为指针类型”

这与它们不是对象类型和/或未保留的事实有关吗? 他们不被释放可以吗?

谢谢。

【问题讨论】:

    标签: objective-c memory-management properties floating-point int


    【解决方案1】:

    NSIntegerfloat 一样,不是Objective-C 类型,也不遵循通常的保留/释放模型。他们只是原始人。为属性赋值就足够了。

    @property (readwrite, assign) NSInteger buttonCount;
    

    应该是你所需要的。

    NSNumber 但是确实遵循通常的保留/释放周期,因此请相应地添加属性。

    【讨论】:

      【解决方案2】:

      这是否与 事实上它们不是对象类型 和/或不保留?他们不被释放可以吗?

      是的。您只能释放具有保留计数的对象。 int、float 和 NSInteger 等原始数据类型不需要保留/释放,因为它们不是指向内存其他部分的指针。

      如果您想了解有关内存管理的更多信息,请尝试查看此文档页面:

      http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html%23//apple_ref/doc/uid/10000011-SW1

      【讨论】:

        猜你喜欢
        • 2011-08-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多