美文网首页
NSMutableArray、NSSet、NSMutableSe

NSMutableArray、NSSet、NSMutableSe

作者: 我不白先生 | 来源:发表于2020-09-27 20:49 被阅读0次

2.NSMutableArray

2.1可变数组,是NSArray数组的子类
2.2创建方法
2.3添加方法
2.4修改方法
2.5删除方法

 (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //创建方法
    NSMutableArray *array1 = [NSMutableArray array];//空数组,有意义
    NSMutableArray *array2 = [NSMutableArray arrayWithCapacity:100];//预估值
    NSMutableArray *array3 = @[@"one",@"two",@"three"];//array3将退化成NSArray,@只能生成常量
    NSMutableArray *array4 = [NSMutableArray arrayWithObjects:@"one",@"two",@"three", nil];
    
    //添加方法
    [array4 addObject:@"four"];//末尾添加
    [self print:array4];
    [array4 insertObject:@"five" atIndex:1];//在指定下标处插入
    [self print:array4];
    
    //修改方法
    [array4 replaceObjectAtIndex:1 withObject:@"aaa"];//表示修改下标为1的位置改成aaa;修改指定下标的数组元素
    [self print:array4];
    NSArray *replace = @[@"five",@"six",@"seven",@"eight",@"nine",@"ten"];
    [array4 replaceObjectsInRange:NSMakeRange(1, 2) withObjectsFromArray:replace];//批量修改指定范围的数组元素
    [self print:array4];
    //删除方法
    [array4 removeLastObject];
    [self print:array4];
    [array4 removeObjectAtIndex:0];//删除指定下标
    [self print:array4];
    [array4 removeObject:@"six"];//删除指定元素
    [self print:array4];
    [array4 removeObjectsInRange:NSMakeRange(3, 2)];//指定范围
    [self print:array4];
    NSArray *del = @[@"seven",@"three"];
    [array4 removeObjectsInArray:del];
    [self print:array4];
    [array4 removeAllObjects];//清空数组
    self.outputLabel.text = [NSString stringWithFormat:@"%lu",array4.count]    
}
-(void)print:(id)obj
{
    self.outputLabel.text = [NSString stringWithFormat:@"%@",obj];
}

3. NSSet

3.1集合是无序、没有重复元素的数组
3.2创建方法
3.2.1自定义类的对象防止重复是,需要同时重写一下NSObject类中的方法
3.2.2粗滤:-(NSUInteger)hash
3.2.3细滤:-(BOOL)isEqual:(id)object

-(NSUInteger)hash//粗滤
{
    return self.age;
}
-(BOOL)isEqual:(id)object//格式代码
{
    if(self == object)
    {
        return YES;//表示重复
    }
    if([object isMemberOfClass:[self class]])
    {
        TRStudent *s = object;
        if([self.name isEqualToString:s.name] && self.age == s.age)
        {
            return YES;
        }
    }
    return NO;//表示没有重复
}

3.3判断结合中是否有指定元素
3.4判断某集合是否为指定集合的子集
3.5判断两个集合是否相等
3.6集合与数组的相互转换
3.7集合的遍历

#import "ViewController.h"
#import "ViewController+TRPrint.h"
#import "TRStudent.h"
#import "TRTeacher.h"
@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //创建方法
    NSSet *set1 = [NSSet setWithObjects:@"one",@"two",@"three",@"two", nil];
    [self print:set1];
    NSSet *set2 = [NSSet setWithSet:set1];//副本
    [self print:set2];
    TRStudent *stu1 = [TRStudent studentWithName:@"张三" andAge:18];
    TRStudent *stu2 = [TRStudent studentWithName:@"李四" andAge:20];
    TRStudent *stu3 = [TRStudent studentWithName:@"王五" andAge:19];
    TRStudent *stu4 = [TRStudent studentWithName:@"张三" andAge:18];
    NSSet *set3 = [NSSet setWithObjects:stu1,stu2,stu3,stu4, nil];
    [self print:set3];
    
    TRTeacher *t1 = [TRTeacher teacherWithName:@"彭于晏" andCourse:@"数学"];
    TRTeacher *t2 = [TRTeacher teacherWithName:@"张学友" andCourse:@"文娱"];
    TRTeacher *t3 = [TRTeacher teacherWithName:@"周星驰" andCourse:@"表演"];
    TRTeacher *t4 = [TRTeacher teacherWithName:@"陈数" andCourse:@"舞蹈"];
    TRTeacher *t5 = [TRTeacher teacherWithName:@"周星驰" andCourse:@"表演"];
    NSSet *set4 = [NSSet setWithObjects:t1,t2,t3,t4,t5,nil];
    [self print:set4];
    //判断结合中是否有指定元素
    if([set1 containsObject:@"two"])
    {
        [self print:@"集合Set1中有元素two"];
    }
    //判断某集合是否为指定集合的子集
    NSSet *set5 = [NSSet setWithObjects:@"one",@"three", nil];
    if([set5 isSubsetOfSet:set1])
    {
        [self print:@"set5是set1的子集"];
    }
    //判断两个集合是否相等
    if([set1 isEqualToSet:set2])
    {
        [self print:@"集合set1与set2相等"];
    }
    //集合转数组
    NSArray *array = [set1 allObjects];
    [self print:array];
    //数组转集合
    array = @[@"aaa",@"bbb",@"ccc",@"aaa"];
    NSSet *set6 = [NSSet setWithArray:array];
    [self print:set6];
    //集合的遍历
    NSMutableString *str = [NSMutableString stringWithCapacity:7 * 3];
    for(NSString *str1 in set1)
    {
        [str appendFormat:@"%@\n",str1];
    }
    [self print:str];
}

4.NSMutableSet(可变集合)

4.1可变集合,是NSSet的子类
4.2创建方法
4.4添加方法
4.4删除方法
4.5运算方法

 (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //创建方法
    NSMutableSet *set1 = [NSMutableSet set];//空集合,有意思
    NSMutableSet *set2 = [NSMutableSet setWithCapacity:100];//预估值
    NSMutableSet *set3 = [NSMutableSet setWithObjects:@"one",@"two",@"three",@"two",nil];//带参工厂方法
    //添加方法
    [set3 addObject:@"four"];//添加一个
    [self print:set3];
    NSArray *added = @[@"five",@"six"];
    [set3 addObjectsFromArray:added];//多个
    [self print:set3];
    //删除方法
    [set3 removeObject:@"three"];//删一个
    [self print:set3];
    [set3 removeAllObjects];//清空集合
    self.outputLabel.text = [NSString stringWithFormat:@"%lu",set3.count];
    //运算方法
    NSArray *added1 = @[@"one",@"two",@"three"];
    NSMutableSet *set4 = [NSMutableSet setWithArray:added1];
    NSArray *added2 = @[@"one",@"three",@"four"];
    NSMutableSet *set5 = [NSMutableSet setWithArray:added2];
    //交集
    [set4 intersectSet:set5];//计算结果被放回set4
    [self print:set4];
    //并集
    [set4 unionSet:set5];//计算结果被放回set4
    [self print:set4];
    //从一个集合中删除另一个集合的内容
    set4 = [NSMutableSet setWithArray:added1];
    set5 = [NSMutableSet setWithArray:added2];
    [set4 minusSet:set5];
    [self print:set4];
}

相关文章

网友评论

      本文标题:NSMutableArray、NSSet、NSMutableSe

      本文链接:https://www.haomeiwen.com/subject/kyacuktx.html