iOS: Why can't I set nil to NSDictionary value?

后端 未结 5 1459
清酒与你
清酒与你 2020-12-29 01:15

This might be very basic question but I was wondering why can\'t I assign nil as NSDictionary value? I have following statement many places in my code. If [q objectFor

5条回答
  •  猫巷女王i
    2020-12-29 02:06

    My friend using nil as marker is a sign of bad programming . nil is reserved for some diffrent purpose .

    if([q objectForKey:@"text"] != nil)
        [dict setObject:[q objectForKey:@"text"] forKey:@"text"];
    else
        [dict removeObjectforKey:@"text"]; // this will do nothing if key does not exsist.
    

    //by default for all the keys the value is nil and you are trying to override this behavior. going against the language rules will always get you in trouble .

    to check just use

    if([dict objectforKey:@"text"] !=nil){} // this will work becuase default value is nil 
    itself 
    

提交回复
热议问题