Application scenario: The app requests back-end data, and the returned data is in JSON form, such as:
- {
- "Is_favor" = 0;
- "Is_follow" = 0;
- "Is_praise" = 0;
- "Is_self" = 0;
- }
I often new a class that accepts this set of data, and then uses the object's Setvaluesforkeyswithdictionary method to automatically assign each field.
- Hstopicmodel *model = [Hstopicmodel new];
- [Model Setvaluesforkeyswithdictionary:responsejson];
As long as each field in the back-end JSON data is named consistent with each field in the model defined by the front end, Setvaluesforkeyswithdictionary can help us to assign each field of the Model a good value. If there is a case where the field naming is inconsistent, we can implement a method in the model's. m file,-(void) SetValue: (nullable ID) value Forundefinedkey: (nsstring *) key; it allows us to manually assign a value to a field named inconsistent.
- -(void) SetValue: (ID) value forundefinedkey: (NSString *) key {
- if (key) {
- if ([Key isequaltostring:@ "id"]) {
- _uid = [self numbertostring:value];
- } Else if ([Key isequaltostring:@ "Is_follow"]) {
- _isfollow = (1 = = [self numbertoint:value])? YES: NO;
- } Else if ([Key isequaltostring:@ "Is_editer"]) {
- _isediter = (1 = = [self numbertoint:value])? YES: NO;
- } Else if ([Key isequaltostring:@ "Constelltion"]) {
- _constellation = [self numbertostring:value];
- } Else if ([Key isequaltostring:@ "status"]) {
- _isblocked = (1 = = [self numbertoint:value])? YES: NO;
- } Else if ([Key isequaltostring:@ "Nospeak"]) {
- _isbanned = (1 = = [self numbertoint:value])? YES: NO;
- }
- }
- }
There is also a method, contrary to this method function -(void) SetValue: (nullable ID) value forkey: (nsstring *) key; This is the method that lets us manually assign a value to a named consistent field, which, if implemented at the same time, will only run -(void) SetValue: (nullable ID) value Forkey: (nsstring *) key; this method.
It took a long time, but recently there was a supernatural phenomenon. The field name for the backend data is "nospeak" and the front end is defined as "Isnospeak", In the simulator IPhone6 (9.0) on the normal operation, but to the simulator iphone5s (9.0) on a run to the field to assign value crash, Miss, so the field name casually replaced one, the result crash phenomenon disappeared, and then changed back to "Isnospeak", began to crash again. Ultimately, the root cause of the problem is not found and it consumes a lot of time to locate the problem.
After that, I decided to change the method of assignment, which is implemented in the model's. m file.
-(void) Setvaluesforkeyswithdictionary: (nsdictionary<nsstring *,ID> *) Keyedvalues; Method. Although this method needs to be manually assigned to each field, it is convenient for us to verify the type of each field, and if there is a problem, you can quickly locate it.
- -(void) Setvaluesforkeyswithdictionary: (nsdictionary<NSString *,id> *) keyedvalues
- {
- if (keyedvalues) {
- For (nsstring *key in [keyedvalues Keyenumerator]) {
- if ([Key isequaltostring:@ "id"]) {
- _tid = [self numbertostring:[keyedvalues Objectforkey:key];
- } Else if ([Key isequaltostring:@ "Score"]) {
- _score = [self numbertostring:[keyedvalues Objectforkey:key];
- } Else if ([Key isequaltostring:@ "title"]) {
- _title = [self numbertostring:[keyedvalues Objectforkey:key];
- }
- }
- }
- }
Original: http://blog.csdn.net/worldzhy/article/details/49982179#
Reprint--ios Model-setvaluesforkeyswithdictionary