Accessing Method from other Classes Objective-C

前端 未结 8 1495
挽巷
挽巷 2020-11-29 05:57

Looked for an answer for this question, but I haven\'t found a suitable one yet. I\'m hoping you guys (and gals) can help me out! (This is for an iPhone app)

Alrig

相关标签:
8条回答
  • 2020-11-29 06:49

    It sounds to me like the common code doesn't need to be in a class at all. Is there a reason you can't just use a C-style function for what you want to do?

    You could put the common code in a class and then make your other two classes subclasses of that one; this method also avoids the code duplication.

    Another option might be to write a class method instead of instance methods for this common code. I think most people feel that singletons are best avoided as a design choice.

    It would be easier to give a good answer if we knew more about what you were really trying to accomplish.

    0 讨论(0)
  • 2020-11-29 06:50

    Bear in mind that Objective-C is just a superset of C, and that whilst #include directives are mostly used for header files, there's nothing stopping you using a #include to embed the contents of one implementation inside another implementation. If the code is truly identical, you can easily just stick it in its own file, and #include it in the .m file.

    Having said that, perhaps it would be better to use this technique in conjunction with categories, especially if the same implementation has similar behaviours.

    0 讨论(0)
提交回复
热议问题