【问题标题】:How to write the properties for variables in c while converting from objective c从目标c转换时如何在c中编写变量的属性
【发布时间】:2012-08-11 06:46:12
【问题描述】:

我正在移植一个名为sneakyness / SneakyInput 的项目,该项目托管在https://github.com/sneakyness/SneakyInput 的github 上。它在 cocos2d-iphone 的 objective-c 中。我想将它移植到 c++ 以在 cocos2d-x 中使用。 在 SneakyJoystick.h 中 属性如 @property (nonatomic, readonly) CGPoint stickPosition;哪里 CGPoint stickPosition 是一个已定义的变量。我通常将此变量定义为CCPoint stickPosition; 但我对 @property (nonatomic, readonly) CGPoint stickPosition; 我是应该用c++代码写还是不写。

【问题讨论】:

    标签: c++ objective-c visual-c++ cocos2d-iphone cocos2d-x


    【解决方案1】:

    如果您有 C++ 代码,您可以保持原样 (C++) 并与 Objective-C 进行交互而不会出现问题。

    如果您打算将所有内容重写为 Objective-C,那么应该将目标 C 属性替换为 C++ 中的属性:

    C++ 中的属性

    private: 
        int x; 
    public:  
        int getX()
        {
            return x;
        }
        void setX(int value) 
        {
            x = value;
        }   
    

    Objective-C 中的属性

    @property(nonatomic) int x;
    
    @synthesize x;
    

    请记住,Objective-C 中的属性声明分为两个文件:@property 进入头文件 (.h),而 @synthesize 进入实现文件 (.m)。

    要更好地了解@property@synthesize 的工作原理,请查看apple declared properties documentation 和/或this other question

    【讨论】:

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