Is a prefix required for methods in iOS?

I'm a little confused by Apple's documentation regarding explaining whether to prefix methods or not?

Apple Doc Explanation 1

:

Use prefixes when naming classes, protocols, functions, constants, and typedefs. Don't use prefixes with naming methods; methods exist in a namespace created by the class that defines them. Also, don't use prefixes to denote structure fields

Apple Doc Explanation 2

:

If you are subclassing a large Cocoa frame class (for example, NSView

or UIView

), and you are absolutely sure that your private methods have different names than the names in the superclass, you can add your own prefix to your private methods. The prefix should be as unique as possible, possibly based on your company or project and form "XX_"

. Therefore, if your project is called Byte Flogger, the prefix could be BF_addObject

:

+3


source to share


3 answers


For classes that contain project-related storylines / materials, no prefixes are required.



But if we use Apple Classes, extending a few of the methods given in the example, like UIView for MBView, we have to prefix the methods for private methods in the private category (in the .m file).

+1


source


This is because ObjC does not support namespaces . You must be prefixed with an uppercase letter instead (as you read correctly in the documentation). You can read this discussion on why to use it.



Note that Apple uses two-letter prefixes (UI (View), NS (String), etc.) and advises programmers to use 3-letter prefixes.

+1


source


I think you should use the prefix whenever you can, it can be good practice and you can determine which part of your big software you are playing on.

Let's say your program name is Byte Flogger, then all your classes should start with:

BF

prefix. BFBaseList

for example, and if you want to prevent rejections when submitting your app to the AppStore, it's also a good idea to name your methods bfMyMethodName

so that you still respect naming conventions CamLCase

.

So, for an image, you can name a property bfContentMode

without suspecting Apple, use one private API function.

Now let's say that you are handling some modules, main module, networking module, etc ... If your class name BFCObject

, you may know that you are working with the Core object of your program.

So it's not necessary, but not doing it can force you to refactor your code at the last moment of submission. In a time-driven project, I would not even venture.

0


source







All Articles