开发者

How to set up a has-many relationship in Cocoa?

开发者 https://www.devze.com 2022-12-31 00:26 出处:网络
I\'m building a (very) simple FTP app in Cocoa, and I need to store information on the different types of servers that are supported. So, I\'ve created a ServerTy开发者_运维问答pe class, which stores

I'm building a (very) simple FTP app in Cocoa, and I need to store information on the different types of servers that are supported. So, I've created a ServerTy开发者_运维问答pe class, which stores all of the relevant information about a single type of server. I then have a ServerTypes class which is designed to manage all of the ServerType classes that are created.

My question is, how to set up the relationship between the two objects. Is there a preferred method to do so?

Also, since Objective-C doesn't support non-instance classes, where should I create an instance of ServerTypes that will have to be used throughout the entire program? Or is there a better way to do that? I need it to be KVC compliant so That I can bind one of the ServerType properties to an NSPopupBox.

I'm fairly new to Cocoa and Objective-C.


To manage a relationship between 2 objects, you have 2 ways: composition or inheritance.

You can inherit from a class to create a subclass then you will have a is-a relationship.

If one object contains another as an instance variable then you will have a has-a relationship.

Here, I think it would be the best to use composition where the ServerTypes objects has an array of all server type objects. Objective-C supports non-instance variable (if that's what you mean), by creating static variable. Then you can use it accross the whole program

0

精彩评论

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

关注公众号