Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is prefix necessary for methods in iOS?

I'm a bit confused with Apple documentation relating to the explanation of whether to use prefix for methods or not?

Apple Doc Explanation 1:

Use prefixes when naming classes, protocols, functions, constants, and typedef structures. Do not use prefixes when naming methods; methods exist in a name space created by the class that defines them. Also, don’t use prefixes for naming the fields of a structure

Apple Doc Explanation 2:

If you are subclassing a large Cocoa framework class (such as NSView or UIView) and you want to be absolutely sure that your private methods have names different from those in the superclass, you can add your own prefix to your private methods. The prefix should be as unique as possible, perhaps one based on your company or project and of the form "XX_". So if your project is called Byte Flogger, the prefix might be BF_addObject:

like image 776
user40910 Avatar asked Dec 09 '25 21:12

user40910


2 Answers

For Classes which contains project related storylines/stuffs, there prefixes are not required.

But if we are using Apple Classes by extending few methods as given in example, like UIView to MBView then we should add prefix to methods to private methods in private category ( in .m file).

like image 53
Mrunal Avatar answered Dec 11 '25 10:12

Mrunal


This is because ObjC does not support namespaces. Instead you should you capital letter prefixes(as you properly read in documentation). You could read this SO discussion why to use them.

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

like image 43
hris.to Avatar answered Dec 11 '25 09:12

hris.to



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!