iOS NSNumber compare 比较

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> Object-C 基础

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> Object-C 线程

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> OpenGL ES

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> GPUImage

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> AVFoundation

零基础 Object-C 学习路线推荐 : Object-C 学习目录 >> CocoaPods


一.NSNumber compare 简介

/*

 If:
    a < b   then return NSOrderedAscending. The left operand is smaller than the right operand.
    a > b   then return NSOrderedDescending. The left operand is greater than the right operand.
    a == b  then return NSOrderedSame. The operands are equal.
*/
typedef NS_CLOSED_ENUM(NSInteger, NSComparisonResult) {
    NSOrderedAscending = -1L,
    NSOrderedSame,
    NSOrderedDescending
};

- (NSComparisonResult)compare:(NSNumber *)otherNumber;

二.NSNumber compare 实战

/******************************************************************************************/
//@Author:猿说编程
//@Blog(个人博客地址): www.codersrc.com
//@File:iOS NSNumber 和 compare 相互转换
//@Time:2021/09/23 08:00
//@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
/******************************************************************************************/

NSNumber *number1 = [NSNumber numberWithFloat:10.0];
NSNumber *number2 = [NSNumber numberWithFloat:20.0];
NSNumber *number3 = [NSNumber numberWithFloat:30.0];
NSNumber *number4 = [NSNumber numberWithFloat:30.0];
    
NSLog(@"number1 = %@ number2 = %@ number3 = %@",number1,number2,number3);
    
//number1 < number2
NSLog(@"[number1 compare:number2]:%ld",(long)[number1 compare:number2]);
    
//number2 > number1
NSLog(@"[number2 compare:number1]:%ld",(long)[number2 compare:number1]);
    
//number3 = number4
NSLog(@"[number3 compare:number4]:%ld",(long)[number3 compare:number4]);

/*
number1 = 10 number2 = 20 number3 = 30
[number1 compare:number2]:-1
[number2 compare:number1]:1
[number3 compare:number4]:0
*/

三.猜你喜欢


未经允许不得转载:猿说编程 » iOS NSNumber compare 比较
喜欢(0) 打赏

评论抢沙发

评论前必须登录!

不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!!

开始学习

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏