Objective-C 实现两个NSNumber的加法

实现一个类方法:

#import "Adder.h"

@implementation Adder
+(NSNumber*)add:(NSNumber *)one and:(NSNumber *)anotherNumber
{
    return [NSNumber numberWithFloat:[one floatValue] + [anotherNumber floatValue]];
}
@end



测试:

#import <Foundation/Foundation.h>
#import "Adder.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSNumber* one = [NSNumber numberWithFloat:10];
        NSNumber* anotherNumber = [NSNumber numberWithFloat:11.2];
        NSNumber* result = [Adder add:one and:anotherNumber];
        NSLog(@"The Result Is : %@", result);
    }
    return 0;
}






你可能感兴趣的:(Objective-C 实现两个NSNumber的加法)