数组和字典

NSArray和NSMutableArray的使用及字典操作
本文介绍了NSArray的初始化、赋值、获取元素、判断元素存在等基本操作,以及NSMutableArray的添加、插入、移除和替换对象的方法。同时,讲解了如何从字符串中提取URL并拼接,以及字典的创建、获取所有值和使用NSMutableDictionary进行键值对的增删改操作。
部署运行你感兴趣的模型镜像

初始化方法
NSArray *name = [[NSArray alloc]initWithObjects:@”one”,@”two”,@”three”, nil];

便利构造器
NSArray *array = [NSArray arrayWithObjects:@”h”,@”he”,@”heh”,@”hehe”,nil];

字面量
NSArray *name1 = @[@"q",@"w",@"e",@"r"];

数组可以直接赋值
name = name1;

数组的对象个数
@property (readonly) NSUInteger count;
NSLog(@”%lu”,array.count);
NSLog(@”%lu”,[array count]);

获取数组中指定下标对应的元素
- (id)objectAtIndex:(NSUInteger)index;
NSArray *hehe = [name objectAtIndex:2];
NSLog(@”%@”,hehe);

NSLog(@”%@”,[name objectAtIndex:2]);

判断数组中有无(id)anObject的存在
- (BOOL)containsObject:(id)anObject;
if ([array containsObject:@”h”]) {
NSLog(@”存在”);
}
else
{
NSLog(@”不存在”);
};

获取数组元素对象的下标
- (NSUInteger)indexOfObject:(id)anObject;
NSUInteger unit = [array indexOfObject:@”he”];
NSLog(@”%lu”,unit);

按照给定的字符串进⾏行截取,将截取的多段字符串放⼊入数组中
- (NSArray )componentsSeparatedByString:(NSString )separator;
NSString *str = @”www.tenxun.com@gailun@dema@dabaojian”;
NSArray *tx = [str componentsSeparatedByString:@”@”];
NSLog(@”—-%@”,tx);

NSString *str1 = @"www.baozoumanhua.com";

NSArray *baozou = [str1 componentsSeparatedByString:@”.”];
NSLog(@”%@”,baozou);

将数组中的元素按照给定的字符串格式拼接成⼀一个完整的字符串对象
- (NSString )componentsJoinedByString:(NSString )separator;
NSArray *array2 = @[@”Boom”,@”shakalaka”];
NSString *str4 = [array2 componentsJoinedByString:@”&”];
NSLog(@”%@”,str4);

NSArray *array3 = @[@”66666”,@”整天666能”,@”找到女朋友吗?”];
NSString *str5 = [array3 componentsJoinedByString:@”.”];
NSLog(@”%@”,str5);

NSString *str6 = [array3[0] stringByAppendingString:array3[1]];
NSLog(@”%@”,str6);

存在如下的字符串,将其中的图⽚片的⽹网址提取出来。 “http://www.imanhua.com/Cover/2011-10/hyrz.jpg&http://www.imanhua.com/Cover/2011-09/op.jpg&http://www.imanhua.com/Cover/2012-04/yjdwb.jpg

NSString *array4 = @”http://www.baidu.com.jpg&http:www.duniang.com.jpg&http://www.google.com.jpg“;

NSArray *hee = [array4 componentsSeparatedByString:@”&”];
NSLog(@”%@”,hee);
NSString *strr = [hee[0] stringByAppendingString:hee[1]];
strr = [strr stringByAppendingString:hee[2]];
NSLog(@”%@”,strr);

NSMutableArray中常用的方法
数组中添加一个对象
NSMutableArray *arr = [[NSMutableArray alloc] init];
-(void)addObject:(id)object
[arr addObject:@”he”];
NSLog(@”%@”,arr);
数组中指定位置插入一个对象
- (void)insertObject:(ObjectType)anObject atIndex:(NSUInteger)index;
[arr insertObject:@”hehe” atIndex:0];
NSLog(@”%@”,arr);
数组中移除一个对象
- (void)removeObject:(id)object;
[arr removeObject:@”he”];
NSLog(@”%@”,arr);

使用指定的对象替换指定位置的对象

- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
交换指定的两个下标对应的对象
- (void)exchangeObjectAtIndex:(NSUInteger)idx1 withObjectAtIndex:(NSUInteger)idx2;

for (int i = 0; i < 10; i++) {
NSNumber *num = [NSNumber numberWithInteger:arc4random()% 90];
}
NSMutableArray *muArray = [NSMutableArray array];

for (int i = 0 ; i < (muArray.count) - 1; i++) {
for (int j = 0; j < (muArray.count) - 1 - i; j++) {
if ([[muArray objectAtIndex:j]integerValue] > [[muArray objectAtIndex:j + 1]integerValue]) {
[muArray exchangeObjectAtIndex:[[muArray objectAtIndex:j]integerValue]
withObjectAtIndex:[[muArray objectAtIndex:j + 1]integerValue]];
}
}
}
for (int i = 0; i < 10; i++) {
NSLog(@”%@”,muArray[i]);
}

字典
字典初始化 一个value 对应一个 key
NSDictionary *dic = [[NSDictionary alloc]
                      initWithObjectsAndKeys:@"frank", @"name",
                      @"duck", @"name2", @"monkey", @"name3", nil];
便利构造器

NSDictionary *dici = [NSDictionary dictionaryWithObjectsAndKeys:@”h”,@”name”,@”he”,@”name1”,@”heh”,”name2”, nil];
NSDictionary *name = [NSDictionary
dictionaryWithObjectsAndKeys:@”frank”, @”name1”,
@”duck”, @”name2”, @”monkey”, @”name3”, nil];

字面量
一个Key ,一个value
NSDictionary *dataDic = @{@"name":@"h",@"he":@"he"};


获取所有的key
@property (readonly, copy) NSArray<KeyType> *allKeys;
NSArray *keyArray = [dic allKeys];
NSLog(@"%@",keyArray);
获取所有的value

@property (readonly, copy) NSArray *allValues;
NSArray *valueArray = [dic allValues];
NSLog(@”%@”,valueArray);

字典只能通过key取值
NSLog(@"%@",[dic objectForKey:@"name"]);

练习题
NSDictionary *testDic =[ NSDictionary dictionaryWithObjectsAndKeys:@”盖伦”,@”德玛西亚之力”,@”嘉文四世”,@”德玛西亚皇子”,@”菊花信”,@”德邦总管”,@”拉克丝”,@”光辉女郎”,@”薇恩”,@”暗夜猎手”,@”奎因”,@”德玛西亚之翼”,@”泰隆”,@”刀锋之影”,@”德莱文”,@”荣耀执行官”,@”德莱厄斯”,@”诺克萨斯之手”,@”卡特琳娜”,@”不详不刃”,@”凯南”,@”狂暴之心”,@”提莫”,@”迅捷斥候”,@”兰博”,@”机械公敌”,@”塔姆”,@”河流之王”,@”乐芙兰”,@”诡术妖姬”,@”莫甘娜”,@”堕落天使”,@”亚索”,@”疾风之刃” ,nil];

NSLog(@"%lu",[testDic count]);

NSArray *keyarr = [testDic allKeys];
for (int i = 0; i < 17; i++) {
    NSLog(@"[%@]:[%@]",[keyarr objectAtIndex:i],[testDic objectForKey:[keyarr objectAtIndex:i]]);
}

NSMutableDictionary
字典中添加新的键值对以及修改某一对键值对
-(void)setObject:(id)anObject forKey:(id)aKey;
NSMutableDictionary * dicc = [[NSMutableDictionary alloc]init];
[dicc setObject:@”hehe” forKey:@”name”];
NSLog(@”%@”,dicc);

移除指定的键对应的键值对 -(void)removeObjectForKey:(id)aKey;
[dicc removeObjectForKey:@”name”];
NSLog(@”%@”,dicc);
移除字典中所有的键值对
-(void)removeAllObjects;
[dicc removeAllObjects];
NSLog(@”%@”,dicc);

集合特点:互异性,无序性 优点:效率特别高,采用哈希算法

NSSet *name2 = [[NSSet alloc]initWithObjects:@"frank",@"duck",@"sun", nil];

NSSet *name3 = [NSSet setWithObjects:@”frank”,@”duck”,@”sun”, nil];

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值