开发者

Creating a 'Player' object in a singleton class

I am creating a calculator for some cards game. In that game, I am creating a Singleton class to manager the game. It holds the scores, keeps track of where the game is etc...

Now after the app launches, I will ask the user to enter 4 players' names. After that, 4 player objects are instantiated according to th开发者_如何学Pythoneir names. I already have an object called "Player", so 4 players will get instantiated with their name, and a score of 0 to start with.

Now I need to store those players in my singleton class. Therefore, I created 4 Player properties in the class. However my question is, under the init method in the Singleton class, in:

if ((self = [super init])) {
    // set properties here
}

Where // set properties here is, what do I write? Do I have to do anything with the Players properties over there?

Thank you,


You don't need to do anything except return the shared instance. Usually the singleton's properties are set in whatever class your instantiating it from.


No, you aren't required to do anything with them, although I would probably set them to nil.


Maybe I'm wrong, but doesn't your AppDelegate already serve as a Singleton?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜