What I want to create:
I want to make a class that has a shared instance of itself. When I call [someClass sharedInstance];
, I want to have it return just that: a shared instance of itself.
The reason is this: I want to have a class in my app that can handle a bunch of methods that I would need to call a bunch of times, or get values of. For example, I want a class that can access motion data using CoreMotion from the data's gyroscope and have it be read correctly in any orientation.
I know, in landscape, the X and Y values are swapped if you're making the update change the Euler angles of a SceneKit camera node.
If I want to make a bunch of scenes, I'd have to create many instances of this motion manager class, and have it load with each scene. That can be avoided with only making one instance of said motion manager, starting it up, and then getting/returning values every so often.
My problem is:
How would I go about coding a class method call so it will return the class's shared instance? I know the method call would be +(instancetype)sharedInstance
in the class's .h
/.m
files, but I don't know how to get it to return said shared instance.
Also, how would I initialize the shared instance?
Edit note: The question it is apparently a duplicate of is NOT a duplicate of this question; that question is about whether or not GCD's dispatch_once
is the best way of doing it in iOS 4.0. Most devices nowadays don't use iOS 4.0, and 4.0 is very well obsolete.