如何在两个类方法之间共享对象

发布于 2024-09-25 17:13:36 字数 1432 浏览 3 评论 0原文

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

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

发布评论

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

评论(2

烂柯人 2024-10-02 17:13:36

有很多方法可以做到这一点。这不是一份详尽的清单。

  • 将其作为参数传递

  • 声明一个全局

  • 将其粘贴在NSThread的+threadDictionary

  • 声明一个返回对象的类方法

  • 在实例变量中粘贴对该对象的引用

    >

  • 声明一个具有存储该对象并返回它的静态局部变量的函数或方法

  • 使用关联引用将其附加到两个方法都可以到达的某个随机对象

  • 映射一大块位于已知地址的内存,并将引用写入页面的第一个字

  • 将指针归档到 NSUserDefaults 并读取它返回

  • 将指针或对象归档到文件系统中的文件并读回

  • 成图片,用光学字符识别抓回来

不说更多细节,很难说哪个是最好的方法(如果最后 4 个是正确的解决方案,我会感到非常惊讶)。


一些附加功能:

  • 使用音频合成说出地址,语音识别将其读回(@chockenberry 说“您可以耳语以获得弱引用。”)

  • @boredzo 建议您模拟 Apple ][/C64 盒式接口

  • 在音频脉络中,最实用的可能是使用摩尔斯电码,因为它很容易识别

  • 您可能会使用推送通知,但是您必须依靠用户按下按钮来给您返回参考(假设是iOS)

There are many ways to do this. This is not an exhaustive list.

  • pass it as parameter

  • declare a global

  • stick it in NSThread's +threadDictionary

  • declare a class method that returns the object

  • stick a reference to the object in an instance variable

  • declare a function or method that has a static local that stores the object and returns it

  • use associated references to attach it to some random object somewhere that both methods can get to

  • map a hunk of memory at a known address and write the reference into the first word of the page

  • archive the pointer to NSUserDefaults and read it back

  • archive the pointer or object to a file in the filesystem and read it back

  • draw the address into an image and use optical character recognition to grab it back

Without more details, it is difficult to say which is the best approach (beyond saying that I would be exceedingly surprised if the last 4 were the right solution).


Some additional:

  • use audio synthesis to say the address, voice recognition to read it back (@chockenberry says "ou could whisper to get a weak reference.")

  • @boredzo suggested that you simulate an Apple ][/C64 casette interface

  • in the audio vein, the most practical is likely to use morse code as it is easy to recognize

  • you could probably use a push notification, but then you'd have to count on the user pushing a button to give you the reference back (assuming iOS)

时常饿 2024-10-02 17:13:36

确实有很多选择。在不了解更多信息的情况下,我只是猜测您的问题最有可能的答案是使用单例。查找“单例设计模式”,因为这是您问题最有可能的答案。

It is true there are a lot of options. Without knowing more I will just guess that the most likely answer to your question is to use a singleton. Look up the "Singleton design pattern" as this is the most likely answer to your question.

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