Objective C ViewController 带有 C++对象作为属性具有 getter 和 setter,每次引用它时都会重新初始化它

发布于 2024-09-28 13:31:30 字数 589 浏览 10 评论 0原文

我已经尝试了很多代码组合,但没有示例可以展示。

我有一个 Objective C 视图控制器,在界面中我声明了一个 C++ 类,其中包含用户想要的首选项。我在标头中执行 @property 命令,在 .mm 文件顶部执行 @synthesize 命令。

然后我在 loadView 方法中使用该类并进行初始化,这是一件好事,我将所有首选项完全按照我希望的方式加载到类中,这一切都很好。

然后在其他方法(例如 numberOfSectionsInTableView 和 numberOfRowsInSection 等)中,我使用该类来检索值,但出现了错误。

每次我去使用它时,该类都会初始化。因此,当想知道首选项文件中有多少个组时,我调用的 C++ 方法 countGroup,它只是重新初始化所有内容,并且我的 C++ 类中不再有任何数据。

我认为 @property 命令以专门重新初始化类的方式生成了 getter 和 setter。这只是一个猜测,但如果我是对的,我该如何重写它们,或者是否有其他方法可以通过我的视图控制器全局使用我的 C++ 类。

注意:如果将 C++ 类作为指针引用,则它不起作用,因为它有大量嵌套向量和内容,编译器只会抛出一个不稳定的错误。

I have tried so many combination's of code I don't have a sample to show.

I have a Objective C view controller, and in the interface I declare a C++ class that contains the preferences of what the user wants to have. I do my @property command in the header and @synthesize command at the top of the .mm file.

Then I use the class in the loadView method and it initialises, which is a good thing and I load all the preferences into the class exactly how I want them to go, which is all fine.

Then down in the other methods such as numberOfSectionsInTableView and numberOfRowsInSection, etc, I go to use the class to retrieve the values, and this goes wrong.

The class initialises every time I go to use it. So when want to know how many groups in the preference file, the C++ method I called countGroup, it just reinitialises everything and there is no longer any data in my C++ class.

What I think, is that the @property command has generated the getters and setters in a way that specifically reinitialises classes. This is just a guess but if I'm right how do I over write them or is there some other way of using my C++ class globally through out my view controller.

Note: the C++ class doesn't work if it's referenced as a pointer because it's got loads of nested vectors and stuff, the compiler just throws a wobbly at that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的他你的她 2024-10-05 13:31:30

我有一个与你类似的故事,我尝试使用 boost 共享指针,但奇怪和疯狂的事情不断发生。

Objective-C++ 的工作规则与 C++ 不同。 Obj-C++ 未涵盖 C++ 类的内存范围规则。智能指针和向量之类的东西在 Obj-C++ 中无法正常工作。

我解决这个问题的方法是编写一个非常简单的容器类:

class MyContainerClass
{
    public:
        boost::shared_ptr<MyClass> mySharedPointer;
        /// etc
};

然后在我的 Obj-C++ 代码中我将分配/释放上面的 Obj-C++ 方式:

- (id)init
{
    if (self = [super init])
    {
        myContainer = new MyContainerClass();
        // etc
    }
    return self;
}

然后

- (void)dealloc
{
    // etc
    delete myContainer;
    [super dealloc];
}

我就可以了像这样的访问器:

- (boost::shared_ptr<MyClass>)mySharedPointer
{
    return myContainer->mySharedPointer;
}

这是一种丑陋的方法,但这是我能弄清楚如何解决这个问题的唯一方法。

I had a similar story to you where I tried to use boost shared pointers and weird and crazy stuff kept happening.

Objective-C++ just doesn't work by the same rules as C++. The memory scope rules of C++ classes are not covered by Obj-C++. Things like smart pointers and vectors just won't work properly in Obj-C++.

The way that I got around it was to write a very simple container class:

class MyContainerClass
{
    public:
        boost::shared_ptr<MyClass> mySharedPointer;
        /// etc
};

Then in my Obj-C++ code I'd allocate/free the above the Obj-C++ way:

- (id)init
{
    if (self = [super init])
    {
        myContainer = new MyContainerClass();
        // etc
    }
    return self;
}

and

- (void)dealloc
{
    // etc
    delete myContainer;
    [super dealloc];
}

Then I'd have an accessor like:

- (boost::shared_ptr<MyClass>)mySharedPointer
{
    return myContainer->mySharedPointer;
}

It's an ugly approach but it's the only way I could figure out how to get around this issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文