用category重写NSMutableArray排序,倒序

发布时间:2020-07-16 11:40:43 作者:Im刘亚芳
来源:网络 阅读:1292

首先新建Category为convert     Category on为NSMutableArray的工程

可以看见.h的文件名为NSMutableArray+convert.h

        .m 文件名为NSMutableArray+convert.m

NSMutableArray+convert.h文件的写法是

#import <Foundation/Foundation.h>
@interface NSMutableArray (convert)
- (NSMutableArray *)convert:(NSMutableArray *)array;
- (NSArray *)convert1:(NSArray *)array;
- (NSMutableArray *)convert2:(NSMutableArray *)array;
@end


NSMutableArray+convert.h文件的写法是


#import "NSMutableArray+convert.h"
@implementation NSMutableArray (convert)
- (NSMutableArray *)convert:(NSMutableArray *)array
{
    NSInteger count = [array count];
    for (int i = 0; i < count; i++) {
        for (int j = 0; j < count - i - 1; j++) {
            // if ([[array objectAtIndex:j] intValue] > [[array objectAtIndex:(j + 1)] intValue]) {
            //                if([[array objectAtIndex:j] compare:[array objectAtIndex:j + 1]] == -1){
            
            if([[array objectAtIndex:j] compare:[array objectAtIndex:j + 1] options:NSNumericSearch] == -1){  //potions  NSNumericSearch = 64,
                [array exchangeObjectAtIndex:j withObjectAtIndex:(j + 1)];
            }
        }
    }
    for (NSString *i in array) {
        NSLog(@"冒泡排序降序:%@", i);
    }
    return array;
}
- (NSArray *)convert1:(NSArray *)array
{
//    NSInteger count = [array count];
    array = [[array reverseObjectEnumerator] allObjects];   //数组倒叙排列
    
    for (NSString *i in array) {
        NSLog(@"数组倒叙输出,用方法%@", i);
    }
    return array;
}
- (NSMutableArray *)convert2:(NSMutableArray *)array
{
    NSInteger count = [array count];
   
        for (int j = 0; j < count / 2; j++) {
            // if ([[array objectAtIndex:j] intValue] > [[array objectAtIndex:(j + 1)] intValue]) {
            //                if([[array objectAtIndex:j] compare:[array objectAtIndex:j + 1]] == -1){
            
//            if([[array objectAtIndex:i] compare:[array objectAtIndex:j + 1] options:NSNumericSearch] == -1){  //potions  NSNumericSearch = 64,
                [array exchangeObjectAtIndex:j withObjectAtIndex:(count - j - 1)];
            }
    
    
    for (NSString *i in array) {
        NSLog(@"数组倒序输出,交换位置%@", i);
    }
    return array;
}
@end

main.m文件中代码

#import <Foundation/Foundation.h>
#import "NSMutableArray+convert.h"
int main(int argc, const char * argv[])
{
    @autoreleasepool {
        
        NSMutableArray *array = [NSMutableArray arrayWithObjects:@"34",@"8",@"32",@"90",@"33",@"78",@"30", nil];
        
        [array convert:array];
        NSMutableArray *array1 = [NSMutableArray arrayWithObjects:@"34",@"8",@"32",@"90",@"33",@"78",@"30", nil];
        [array1 convert1:array1];
        NSMutableArray *array2 = [NSMutableArray arrayWithObjects:@"34",@"8",@"32",@"90",@"33",@"78",@"30", nil];
        [array2 convert2:array2];
        
    }
    return 0;
}


类目Category   

Category的定义

Category的声明

Category的实现


推荐阅读:
  1. php - 数组倒序
  2. sorted 与 sort的区别 正向排序 reverse()列表反向排序 与列表数字倒序排序

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

interface 工程 import

上一篇:如何利用C#编写拼图游戏

下一篇:Microsoft Teams语音落地-01实战软语音网关部署

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》