1、NSArray
NSArray不可变集合,不能添加新元素和删除已有元素和替换元素
2、demo
Dog.h
#import <Foundation/Foundation.h>
#ifndef Dog_h
#define Dog_h
@interface Dog : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) int age;
-(id)initWithName:(NSString *)name age:(int)age;
-(void)say:(NSString *)content;
@end
#endif /* Dog_h */
Dog.m
#import <Foundation/Foundation.h>
#import "Dog.h"
@implementation Dog
@synthesize name;
@synthesize age;
-(id)initWithName:(NSString *)name age:(int)age
{
if (self = [super init])
{
self.name = name;
self.age = age;
}
return self;
}
-(void)say:(NSString *)content
{
NSLog(@"%@ say %@", name, content);
}
-(BOOL)isEqual:(id)object
{
if (object == self)
return YES;
if ([object class] == Dog.class)
{
Dog *dog = (Dog *)object;
return [self.name isEqualToString:dog.name] && (self.age == dog.age);
}
return NO;
}
@end
IOS学习笔记十九NSArray和NSMutableArray
最新推荐文章于 2024-08-23 21:28:47 发布
该博客介绍了Objective-C中的NSArray容器,强调其不可变特性,即不能添加、删除或替换元素。同时,展示了Dog类的定义,包括属性、初始化方法和一个say方法。Dog类还实现了isEqual方法进行对象比较。示例代码详细展示了类的使用和对象操作。
摘要由CSDN通过智能技术生成