开发者

Objective-C newbie question re: global instances

开发者 https://www.devze.com 2023-03-02 00:02 出处:网络
Is there a way to make one (1) instance of an object and share that instance between different files?

Is there a way to make one (1) instance of an object and share that instance between different files?

For instance, I have Class "A" (created in fileA.m), and I create an instance of it in a different file (fileB.m). Now, I want to use that same instance created in fileB.m in several other files (fileC.m and fileD.m) so I can share ivar's (s开发者_Python百科uch as a database).

How do I do that?


You would want Class A to be a singleton. Here's a great guide to help you out with that.


You need to use a static class method as a constructor which will return a singleton instance of the object you're looking for.

0

精彩评论

暂无评论...
验证码 换一张
取 消