OC 自带方法 简绍 4 NSMutableArray

这篇博客介绍了NSMutableArray的一些主要操作,包括初始化、添加元素、插入元素、删除元素、替换元素、交换元素、判断对象类型以及对象与基本类型转换的方法,并通过代码示例进行了详细解释。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.//NSMutableArray   继承自array 的可变数组

NSMutableArray * array = [[NSMutableArray alloc] initWithCapacity:1];// 这个数字代表了数组初始化有n个结构体的内存,若不够,再申请 n 个结构体内存.

例子

        Person * p = [[Person alloc] initWithName:@"fu" sex:@"nan" age:30];
        Person * p1 = [[Person alloc] initWithName:@"lu" sex:@"nan" age:30];
        Person * p2 = [[Person alloc] initWithName:@"shou" sex:@"nan" age:30];

        NSMutableArray * array = [[NSMutableArray alloc] initWithCapacity:1];

2.addObject // 给可变数组添加元素

- (void)addObject:(id)object  //Adds a given object to the receiver

例子 

        [array addObject:p];
        [array addObject:p1];
        [array addObject:p2];
        [array addObject:p];
        NSLog(@"%@", array);

3.insertObject  //在可变数组中在某个下标位置添加元素

- (void)insertObject:(NSManagedObject *)object // Registers an object to be inserted in the receiver’s persistent store the next time changes are saved.

例子

        [array insertObject:p1 atIndex:3];
        NSLog(@"%@", array);

4.removeLastObject //删除最后一个

- (void)removeLastObject  //Removes the object with the highest-valued index in the array

例子

        [array removeLastObject];
        NSLog(@"%@", array);

5.removeObject // 删除该数组所有得 p (元素)

virtual void removeObject( const OSMetaClassBase *anObject);  //Removes an object from the set.

例子

        [array removeObject:p];
        NSLog(@"%@", array);

6. removeObjectAtIndex      // 根据下标删除某个元素

- (void)removeObjectAtIndex:(NSUInteger)idx //Removes a the object at the specified index from the mutable ordered set.

例子

        [array removeObjectAtIndex:1];
        NSLog(@"%@", array);

        for (int i = 0; i < [array count]; i++) {
            [array removeObjectAtIndex:i];
            i--;//数组是动态的
        }
        NSLog(@"%@", array);

7.replaceObjectAtIndex // 用某个对象替换给定下标的对象

- (void)replaceObjectAtIndex:(NSUInteger)idx
                  withObject:(id)object   

//Replaces the object at the specified index with the new object.

例子

        [array replaceObjectAtIndex:1 withObject:p];
        NSLog(@"%@", array);


8.exchangeObjectAtIndex // //给定两个下标 交换

- (void)exchangeObjectAtIndex:(NSUInteger)idx1
            withObjectAtIndex:(NSUInteger)idx2

//Exchanges the objects in the array at given indices.

例子

        [array exchangeObjectAtIndex:1 withObjectAtIndex:3];
        NSLog(@"%@", array);

9.isKindOfClass // 是否是某个类

- (BOOL)isKindOfClass:(Class)aClass //Returns a Boolean value that indicates whether the receiver is an instance of given class or an instance of any class that inherits from that class. (required)

例子

        Person * p = [[Person alloc] initWithName:@"fu" sex:@"nan" age:30];
        Person * p1 = [[Person alloc] initWithName:@"lu" sex:@"nan" age:30];
        Person * p2 = [[Person alloc] initWithName:@"shou" sex:@"nan" age:30];
        NSArray * array = [NSArray arrayWithObjects:p, @"111", p1, @"222", p2, @"333", nil];
//        NSLog(@"%@", array);
//        
        for (int i = 0; i < [array count]; i++) {
            NSString * obj = [array objectAtIndex:i];
            idnewStr = [obj stringByAppendingString:@"aaaa"];// 崩溃 !
            NSLog(@"%@", obj);
        }
        for (int i = 0; i < [array count]; i++) {
            id obj = [array objectAtIndex:i];
            if ([obj isKindOfClass:[NSString class]]) {
                NSString * newStr = [obj stringByAppendingString:@"aaaa"];
                NSLog(@"%@", newStr);
            }
            NSLog(@"%@", obj);
        }

10.//numberWithInt  把基本型转位对象

+ (NSNumber *)numberWithInt:(int)value  //Creates and returns an NSNumber object containing a given value, treating it as a signed int.

例子

        int n = 5;
        NSNumber * number = [NSNumber numberWithInt:n];
        NSArray * array = [NSArray arrayWithObjects:number, [NSNumber numberWithInt:8], nil];
        NSLog(@"%@", array);

11.intValue   //对象转为基本型

@property(readonly) int intValue // The integer value of the string. (read-only)

例子

        int sum = [[array firstObject] intValue] + [[array lastObject] intValue];
        NSLog(@"%d", sum);



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值