Stateless static methods compared to C functions in Objective-C

As for good Objective-C encoding methods, if I create a function that has no state, is it better to write it as a static method of some class or as a C function?

For example, I have a special method for finding the path to a file that checks the Caches directory before moving on to the main NSBundle. I am currently using it as a static method in another empty Utils class. If it is instead a C function?

The reason I decided to use the static method (for now) is because a) it is consistent with the Objective-C syntax, and b) the class helps classify the method. However, I feel like I'm cheating a little, as I could easily populate my Util class with these static static methods and end up with an ugly "shell class" whose sole purpose would be to keep them.

What agreement are you using? Is this “better” than some other objective metric? Thank!

+5
source share
3 answers

, , , Objective-C. , .

:

@interface NSString (MyStringCategories) 
- (NSString*) myCoolMethod;
@end

// [StringCategories.m]
#import "StringCategories.h"

@implementation NSString (MyStringCategories)
- (NSString*) myCoolMethod {
    // do cool stuff here
    return whateverYouLike;
}
@end

myCoolMethod . !

, NSBundle . , , -, .

+2

, , . , , , , .. Pp. , , , , OO, .

, (imo) :

http://www.gotw.ca/publications/c++cs.htm

, ++, , (esp. Objective-C Objective-C ++), , " ". " ".

: "Nonemember nonfriend functions [...] [...] [] [...]".

, .

+1

, . , , . , , ( , .m , , ). ObjC , , , , - objc . , / .

@interface, , . , , , .

, , .

. , , , .

+1

All Articles