Category iOS Developer ios零碎记录

iOS NSMutableArray的Category

2016-03-11  本文已影响494人  王隆帅

前言

项目中自己通过各种渠道及结合项目的经验整理了一套自己的工具包,里面有各种Category,及封装的方法,方便项目使用,这段时间我会陆续分享出来,希望能帮助到有需求的人YoY 。

NSMutableArray+LSCore.h

//
//  NSMutableArray+LSCore.h
//  LSCoreFramework
//
//  Created by 王隆帅 on 15/5/4.
//  Copyright (c) 2015年 王隆帅. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface NSMutableArray (LSCore)

#pragma 增加或删除对象
- (void)insertObject:(id)object atIndexIfNotNil:(NSUInteger)index;
- (void)moveObjectAtIndex:(NSUInteger)index toIndex:(NSUInteger)toIndex;

#pragma mark - 排序
- (void)shuffle;
- (void)reverse;
- (void)unique;
- (void)sorting:(NSString *)parameters ascending:(BOOL)ascending;

#pragma - mark 安全操作
-(void)addObj:(id)i;
-(void)addString:(NSString*)i;
-(void)addBool:(BOOL)i;
-(void)addInt:(int)i;
-(void)addInteger:(NSInteger)i;
-(void)addUnsignedInteger:(NSUInteger)i;
-(void)addCGFloat:(CGFloat)f;
-(void)addChar:(char)c;
-(void)addFloat:(float)i;
-(void)addPoint:(CGPoint)o;
-(void)addSize:(CGSize)o;
-(void)addRect:(CGRect)o;

@end

NSMutableArray+LSCore.m

//
//  NSMutableArray+LSCore.m
//  LSCoreFramework
//
//  Created by 王隆帅 on 15/5/4.
//  Copyright (c) 2015年 王隆帅. All rights reserved.
//

#import "NSMutableArray+LSCore.h"

@implementation NSMutableArray (LSCore)
#pragma 增加或删除对象

/**
 *  插入一个元素到指定位置
 *
 *  @param object 需要插入的元素
 *  @param index  位置
 */
- (void)insertObject:(id)object atIndexIfNotNil:(NSUInteger)index {
    if (self.count > index && object) {
        [self insertObject:object atIndex:index];
    }
}

/**
 *  移动对象 从一个位置到另一个位置
 *
 *  @param index   原位置
 *  @param toIndex 目标位置
 */
- (void)moveObjectAtIndex:(NSUInteger)index toIndex:(NSUInteger)toIndex {
    if (self.count > index && self.count > toIndex) {
        id object = [self objectAtIndex:index];
        if (index > toIndex) {
            [self removeObjectAtIndex:index];
            [self insertObject:object atIndex:toIndex];
        } else if (index < toIndex){
            [self removeObjectAtIndex:index];
            [self insertObject:object atIndex:toIndex - 1];
        }
    }
}

- (void)removeFirstObject {
    if (self.count > 0) {
        [self removeObjectAtIndex:0];
    }
}

#pragma mark - 排序
/**
 *  重组数组(打乱顺序)
 *
 */
- (void)shuffle {
    NSMutableArray *copy = [self mutableCopy];
    [self removeAllObjects];
    while ([copy count] > 0)
    {
        int index = arc4random() % [copy count];
        id objectToMove = [copy objectAtIndex:index];
        [self addObject:objectToMove];
        [copy removeObjectAtIndex:index];
    }
}

/**
 *  数组倒序
 *
 */
- (void)reverse {
    NSArray *reversedArray = [[self reverseObjectEnumerator] allObjects];
    [self removeAllObjects];
    [self addObjectsFromArray:reversedArray];
}

/**
 *  数组去除相同的元素
 */
- (void)unique {
    NSSet *set = [NSSet setWithArray:self];
    NSArray *array = [[NSArray alloc] initWithArray:[set allObjects]];
    [self removeAllObjects];
    [self addObjectsFromArray:array];}

/**
 *  根据关键词 对本数组的内容进行排序
 *
 *  @param parameters 关键词
 *  @param ascending  YES 升序 NO 降序
 *
 */
- (void)sorting:(NSString *)parameters ascending:(BOOL)ascending {
    NSSortDescriptor*sorter=[[NSSortDescriptor alloc]initWithKey:parameters ascending:ascending];
    NSMutableArray *sortDescriptors=[[NSMutableArray alloc]initWithObjects:&sorter count:1];
    NSArray *sortArray=[self sortedArrayUsingDescriptors:sortDescriptors];
    [self removeAllObjects];
    [self addObjectsFromArray:sortArray];
}

#pragma - mark 安全操作
/**
 *  添加新对象
 *
 *  @param i 添加的对象
 */
-(void)addObj:(id)i {
    if (i!=nil) {
        [self addObject:i];
    }
}

/**
 *  添加字符串
 *
 *  @param i 添加的字符串
 */
-(void)addString:(NSString*)i {
    if (i!=nil) {
        [self addObject:i];
    }
}

/**
 *  添加Bool
 *
 *  @param i 添加的Bool
 */
-(void)addBool:(BOOL)i {
    [self addObject:@(i)];
}

/**
 *  添加Int
 *
 *  @param i 添加的Int
 */
-(void)addInt:(int)i {
    [self addObject:@(i)];
}

/**
 *  添加Integer
 *
 *  @param i 添加的Integer
 */
-(void)addInteger:(NSInteger)i {
    [self addObject:@(i)];
}

/**
 *  添加UnsignedInteger
 *
 *  @param i 添加的UnsignedInteger
 */
-(void)addUnsignedInteger:(NSUInteger)i {
    [self addObject:@(i)];
}

/**
 *  添加CGFloat
 *
 *  @param i 添加的CGFloat
 */
-(void)addCGFloat:(CGFloat)f {
    [self addObject:@(f)];
}

/**
 *  添加Char
 *
 *  @param i 添加的Char
 */
-(void)addChar:(char)c {
    [self addObject:@(c)];
}

/**
 *  添加Float
 *
 *  @param i 添加的Float
 */
-(void)addFloat:(float)i {
    [self addObject:@(i)];
}

/**
 *  添加Point
 *
 *  @param i 添加的Point
 */
-(void)addPoint:(CGPoint)o {
    [self addObject:NSStringFromCGPoint(o)];
}

/**
 *  添加Size
 *
 *  @param i 添加的Size
 */
-(void)addSize:(CGSize)o {
    [self addObject:NSStringFromCGSize(o)];
}

/**
 *  添加Rect
 *
 *  @param i 添加的Rect
 */
-(void)addRect:(CGRect)o {
    [self addObject:NSStringFromCGRect(o)];
}


@end

我的公众号二维码
上一篇 下一篇

猜你喜欢

热点阅读