Setobject: forkey: and setValue: forkey:

Time:2022-2-9
1: Differences and relations between setobject: forkey: and setValue: forkey:

1. Setobject: forkey: unique to nsmutabledictionary;
SetValue: forkey: is the main method of KVC;

2. Setobject: forkey:Object cannot be nil, otherwise an error will be reportedAs long as the parameter of key is an object, it is not limited to nsstring

The value in setValue: forkey: can be nil, and the removeobject: forkey: method will be called automatically at this time;The parameter of key can only be nsstring type

3. Nil is different from null, [nsnull null] indicates that it is an empty object, not nil;

4. SetValue: forkey: it is created in the nsobject object, that is, all objects have this method, which can be used in any class; (when the method caller is an object)

2: Objectforkey: and valueforkey:

There are two methods for nsdictionary value, objectforkey: and valueforkey:
(it is recommended to use objectforkey:)

1.If the key does not start with @ match, they are the same

2.If key starts with @For example: @ “@ hhisiskey”, valueforkey: will remove @, and then execute [super valueforkey] with the rest;

3. Example: person * person = [person alloc] init];
person.name = @“Chen”;
Pass: [person valueforkey: @ “name”]; Take out: Chen. This is KVC’s method.

4. Valueforkey: the value is to find a property accessor with the same name as the specified key. If it is not found, execute valueforundefined key:, while valueforundefined key: by default, throw a crash exception;

Recommended Today

Redistemplate set expiration time

//向redis里存入数据和设置缓存时间 stringRedisTemplate.opsForValue().set(“baike”, “100”, 60 * 10, TimeUnit.SECONDS); //val做-1操作 stringRedisTemplate.boundValueOps(“baike”).increment(-1); //根据key获取缓存中的val stringRedisTemplate.opsForValue().get(“baike”) //val +1 stringRedisTemplate.boundValueOps(“baike”).increment(1); //根据key获取过期时间 stringRedisTemplate.getExpire(“baike”); //根据key获取过期时间并换算成指定单位 stringRedisTemplate.getExpire(“baike”,TimeUnit.SECONDS); //根据key删除缓存 stringRedisTemplate.delete(“baike”); //检查key是否存在,返回boolean值 stringRedisTemplate.hasKey(“baike”); //向指定key中存放set集合 stringRedisTemplate.opsForSet().add(“baike”, “1”,”2″,”3″); //设置过期时间 stringRedisTemplate.expire(“baike”,1000 , TimeUnit.MILLISECONDS); //根据key查看集合中是否存在指定数据 stringRedisTemplate.opsForSet().isMember(“baike”, “1”); //根据key获取set集合 stringRedisTemplate.opsForSet().members(“baike”); //验证有效时间 Long expire = redisTemplate.boundHashOps(“baike”).getExpire(); /** * 从redis中获取key对应的过期时间; * 如果该值有过期时间,就返回相应的过期时间; * 如果该值没有设置过期时间,就返回-1; * 如果没有该值,就返回-2; */ redisTemplate.opsForValue().getOperations().getExpire(“key的名称”)