首页 > 其他分享 >OC之【NSDictionary详解】

OC之【NSDictionary详解】

时间:2022-12-09 15:03:40浏览次数:39  
标签:nil NSLog OC NSDictionary k2 详解 dict key

main.m文件

#import <Foundation/Foundation.h>
#import "Student.h"

字典的初始化
void
// NSDictionary是不可变的
NSDictionary *dict = [NSDictionarydictionaryWithObject:@"v"forKey:@"k"];

//最常用的初始化方式
dict = [NSDictionarydictionaryWithObjectsAndKeys:
@"v1", @"k1",
@"v2", @"k2",
@"v3", @"k3",nil];

NSArray *objects = [NSArrayarrayWithObjects:@"v1",@"v2", @"v3",nil];
NSArray *keys = [NSArrayarrayWithObjects:@"k1",@"k2", @"k3",nil];
NSDictionarydictionaryWithObjects:objects forKeys:keys];
NSLog(@"%@", dict);
}

字典的基本用法
void
NSDictionary *dict = [NSDictionarydictionaryWithObjectsAndKeys:
@"v1", @"k1",
@"v2", @"k2",
@"v3", @"k3",nil];

// count是计算有多少个键值对(key-value)
NSLog(@"count=%zi", dict.count);

//由于NSDictionary是不可变的,所以只能取值,而不能修改值
id obj = [dict objectForKey:@"k2"];
NSLog(@"obj=%@", obj);

//将字典写入文件中
NSString *path =@"/Users/apple/Desktop/dictionary.xml";
[dict writeToFile:pathatomically:YES];

//从文件中读取内容
dict = [NSDictionarydictionaryWithContentsOfFile:path];
NSLog(@"dict=%@", dict);
}

字典的用法
void
NSDictionary *dict = [NSDictionarydictionaryWithObjectsAndKeys:
@"v1", @"k1",
@"v2", @"k2",
@"v3", @"k3",nil];
// 返回所有的key
NSArray *keys = [dict allKeys];
//NSLog(@"keys=%@", keys);

NSArray *objects = [dict allValues];
//NSLog(@"objects=%@", objects);

// 根据多个key取出对应的多个value
// 当key找不到对应的value时,用marker参数值代替
objects = [dict objectsForKeys:[NSArrayarrayWithObjects:@"k1",@"k2", @"k4", nil]notFoundMarker:@"not-found"];
NSLog(@"objects=%@", objects);
}

遍历字典
void
NSDictionary *dict = [NSDictionarydictionaryWithObjectsAndKeys:
@"v1", @"k1",
@"v2", @"k2",
@"v3", @"k3",nil];
// 遍历字典的所有key
for (id keyin
id value = [dict objectForKey:key];
NSLog(@"%@=%@", key, value);
}
}

遍历字典2
void
NSDictionary *dict = [NSDictionarydictionaryWithObjectsAndKeys:
@"v1", @"k1",
@"v2", @"k2",
@"v3", @"k3",nil];
// key迭代器
NSEnumerator *enumer = [dict keyEnumerator];
id key = nil;
while ( key = [enumer nextObject]) {
id value = [dict objectForKey:key];
NSLog(@"%@=%@", key, value);
}

// 对象迭代器
// [dict objectEnumerator];
}

遍历字典3
void
NSDictionary *dict = [NSDictionarydictionaryWithObjectsAndKeys:
@"v1", @"k1",
@"v2", @"k2",
@"v3", @"k3",nil];
[dict enumerateKeysAndObjectsUsingBlock:
id key,id obj, BOOL
NSLog(@"%@=%@", key, obj);
}];
}

#pragma mark
void
Student *stu1 = [StudentstudentWithName:@"stu1"];
Student *stu2 = [StudentstudentWithName:@"stu2"];
Student *stu3 = [StudentstudentWithName:@"stu3"];

//一个对象称为字典的key或者value时,会做一次retain操作,也就是计数器会+1
NSDictionary *dict = [NSDictionarydictionaryWithObjectsAndKeys:
@"k1",
@"k2",
@"k3", nil];

//当字典被销毁时,里面的所有key和value都会做一次release操作,也就是计数器会-1
}

int main(int argc,const char
{
@autoreleasepool {
dictMemory();
}
return 0;
}


Student.h文件
#import <Foundation/Foundation.h>

@interface Student :NSObject
(nonatomic,retain) NSString *name;

+ (id)studentWithName:(NSString
@end


Student.m文件
#import "Student.h"

Student

+ (id)studentWithName:(NSString
Student *stu = [[Studentalloc] init];
name
return [stu autorelease];
}

- (void)dealloc {
NSLog(@"%@被销毁了", _name);
// 释放name
_namerelease];
superdealloc];
}
@end

标签:nil,NSLog,OC,NSDictionary,k2,详解,dict,key
From: https://blog.51cto.com/u_15907570/5925315

相关文章

  • OC之【c语言结构体】
    <stdio.h>void//这个机构只能在函数内部使用//定义一个名为Student的结构体类型structintage;//年龄char*name;//姓名floatheight;//身高};......
  • OC之【NSMutableDictionary的使用】
    main.m文件#import<Foundation/Foundation.h>#import"Student.h"可变字典的使用void//创建一个空的字典NSMutableDictionary*dict=[NSMutableDictionarydicti......
  • OC之【NSValue的使用】
    #import<Foundation/Foundation.h>voidCGPointpoint=CGPointMake(10,10);//将结构体变量包装成一个对象NSValue*value=[NSValuevalueWithPoint:point];......
  • OC之【enum枚举】
    void//定义一种枚举类型enum//定义一个枚举变量senumSeasons=winter;}void//定义枚举类型的同时定义一个枚举变量senumSeason{spring,summer,......
  • OC之【NSDate使用】
    #import<Foundation/Foundation.h>日期创建void//date方法返回的就是当前时间(now)NSDate*date=[NSDatedate];//now:21:09:40//date:21:09:50......
  • OC之【内存管理】
    Student@synthesizeage=_age;//在xcode4.5以上环境下可以省略-(void)dealloc{@"%@被销毁了",self);super//一定要调用super的dealloc方法,而且最好放在最......
  • OC之【NSObject使用】
    main.m文件#import<Foundation/Foundation.h>#import"Student.h"#import"Person.h"常用方法voidStudent*stu=[[[Studentalloc]init]autorelease];//isKin......
  • OC之【@property的用法】
    1.这里的retain代表:在set方法中,release旧值,retain新值(nonatomic,retain)Book*book;(retain)Card*card;代表只生成get方法的声明默认是readwrite,同时生成get和set......
  • OC之【objective-c中结构体】
    #import<Foundation/Foundation.h>void//定义了Date这种结构体类型structintintint};//定义结构体变量structDated={2013,4,5};day=6;}voi......
  • C# OpenProtocol 开放以太网协议 读写数据 订阅数据
    主要使用的软件是HslCommunication关于这个软件的本身,详细可以参考下面的地址:github地址:https://github.com/dathlin/HslCommunication官网:http://www.hslcommunicatio......