How would you define a simple “min” method in obj-c

前端 未结 7 2230
感动是毒
感动是毒 2020-12-16 11:42

I want to define a min and max methods in a Utils class.

@interface Utils

int min(int a, int b);
int max(int a, int b);

@end

But I don\'t

7条回答
  •  南方客
    南方客 (楼主)
    2020-12-16 12:18

    In a template file named "XXIntegerMath.h" drop this...

    #import 
    
    static inline NSInteger imax(NSInteger a, NSInteger b) {
        return  a > b ? a : b;
    }
    
    static inline NSInteger imin(NSInteger a, NSInteger b) {
        return  a < b ? a : b;
    }
    

    Then in your objective-c class ...

    #import "XXIntegerMath.h"
    NSInteger minValue = imin(someValue, someOtherValue);
    

    It doesn't suffer from the problems described by Regexident.

提交回复
热议问题