OC之之NSDictionary/NSMutableDictionary

NSDictionary/NSMutableDictionary特点:

(1)里存储的东西都是键值对

(2)可以像数组一样快速创建(适用NSDictionary):@{key1 : value1,  key2 : value2}

(3)快速访问元素:字典名 [key]

比如可以这样为指定key赋值:

dic [@”key1”] = @”value1”;

快速获取key所对应的value

NSString *name = dic[@”name”];//存储的是String

或者:

NSDictionary *userdic = dic[@”user”];//存储的是NSDictionary

NSString *name = userdic [@”name”];

(4)字典不允许有相同的key,但允许有相同的valueObject

(5)字典是无序的

1、不可变词典NSDictionary

字典初始化:

1)以一个元素初始化

NSDictionary *dic = [NSDictionary dictionaryWithObject:numObj forKey:@"key"];

2)初始化两个元素

NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:

numObj, @"valueKey", numObj2, @"value2",nil];

3)初始化新字典,新字典包含otherDic

NSDictionary *dic = [NSDictionary dictionaryWithDictionary:otherDic];

4)以文件内容初始化字典

NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];

(1)也可创建多维字典

使用快速创建的方式:

NSArray *persons = @[

          @{@"name" : @"jack", @"qq" : @"432423423", @"books": @[@"5分钟突破iOS编程", @"5分钟突破android编程"]},

    @{@"name" : @"rose", @"qq" : @"767567"},

    @{@"name" : @"jim", @"qq" : @"423423"},

    @{@"name" : @"jake", @"qq" : @"123123213"}

];

访问:

NSLog(@"%@", persons[1][@"qq"]);

上述代码含义:

先取出1位置对应的字典

再取出字典中qq这个key对应的数据

常用方法:

1)获取字典数量

NSInteger count = [dic count];

2)通过key获取对应的value对象

NSObject *valueObj = [dic objectForKey:@"key"];

3)将字典的key转成枚举对象,用于遍历

NSEnumerator *enumerator = [dic keyEnumerator];

4)获取所有键的集合

NSArray *keys = [dic allKeys];

5)获取所有值的集合

NSArray *values = [dic allValues];

2、可变数组NSMutableDictionary

1)初始化一个空的可变字典

NSMutableDictionary *dic2 = [NSMutableDictionary

dictionaryWithObjectsAndKeys:@"v1",@"key1",@"v2",@"key2",nil];

 

NSDictionary *dic3 = [NSDictionary dictionaryWithObject:

@"v3" forKey:@"key3"];

2)向字典2对象中添加整个字典对象3

[dic2 addEntriesFromDictionary:dic3];

3)向字典2对象中最佳一个新的key3value3

[dic2 setValue:@"value3" forKey:@"key3"];

4)初始化一个空的可变字典

NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];

5)将空字典1对象内容设置与字典2对象相同

[dic1 setDictionary:dic2];

6)将字典中key1对应的值删除

[dic1 removeObjectForKey@"key1"];

NSArray *array = [NSArray arrayWithObjects:@"key1", nil];

7)根据指定的数组(key)移除字典1的内容

[dic2 removeObjectsForKeys:array];

8)移除字典所有对象

[dic1 removeAllObjects];

 

3、遍历字典

1)快速枚举

for (id key in dic){

     id obj = [dic objectForKey:key];

     NSLog(@"%@", obj);

}

2)一般枚举

NSArray *keys = [dic allKeys];

inr length = [keys count];

for (int i = 0; i < lengthi++){

     id key = [keys objectAtIndex:i];

     id obj = [dic objectForKey:key];

     NSLog(@"%@", obj);

}

3)通过枚举类型枚举

NSEnumerator *enumerator = [dic keyEnumerator];

id key = [enumerator nextObject];

while (key) {

        id obj = [dic objectForKey:key];

        NSLog(@"%@", obj);

        key = [enumerator nextObject];

}


转载于:https://my.oschina.net/u/2448717/blog/500524

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是iOS Objective-C实现multipart/form-data格式上文件的示例代码: ```objective-c // 设置请求URL NSURL *url = [NSURL URLWithString:@"http://example.com/upload"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; // 设置请求方法为POST [request setHTTPMethod:@"POST"]; // 设置请求头 NSString *boundary = @"Boundary+0xAbCdEfGbOuNdArY"; NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; [request setValue:contentType forHTTPHeaderField:@"Content-Type"]; // 设置请求体 NSMutableData *body = [NSMutableData data]; // 添加文件数据 NSData *fileData = UIImageJPEGRepresentation(image, 1.0); if (fileData) { [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@\"\r\n", @"image.jpg"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", @"image/jpeg"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:fileData]; [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; } // 添加其他参数 NSDictionary *parameters = @{@"key1": @"value1", @"key2": @"value2"}; for (NSString *key in parameters) { [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"%@\r\n", parameters[key]] dataUsingEncoding:NSUTF8StringEncoding]]; } // 添加请求结束标志 [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; // 设置请求体长度 [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[body length]] forHTTPHeaderField:@"Content-Length"]; // 设置请求体 [request setHTTPBody:body]; // 发送请求 NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { // 处理响应 }]; [task resume]; ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值