简体   繁体   English

处理不同类型的对象

[英]Handle object of different types

From my backend API, I get a json of objects consisting of array, dictionary, number, bool, string etc. For eg. 从我的后端API,我得到一个对象的json,该对象由数组,字典,数字,布尔值,字符串等组成。例如。

{
data:[
{
id : 1,
name : "abcd"
},
{
id : 2,
name : "abcde"
},
{
id : 3,
name : "abcde"
},
]
total_count : 10
}

Sometimes the value in total_count comes as a number and sometimes it comes as a string. 有时total_count中的值是数字,有时是字符串。 In my code I have coded 在我的代码中,我已经编码

[lbl setText:[jsonObject valueForKey:@"total_count"]]

This crashes because when the total_count key value is a number. 由于total_count键值为数字时,这会导致崩溃。 Obviously I can do this 显然我可以做到这一点

[lbl setText:[NSString stringWithFormat:@"%d",[[jsonObject valueForKey:@"total_count"] intValue]]];

but this happens at a lot of places in the API. 但这在API中的很多地方都会发生。 A string is coming instead of a bool. 字符串代替了布尔。 data:false instead of data:[] data:false而不是data:[]

[EDIT] [编辑]

[[AFHTTPRequestOperationManager manager] GET:[URLString attachToken] parameters:parameters success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        if([[[responseObject valueForKey:@"response"] valueForKey:@"status"] boolValue]) {
NSLog(@"success");
}
                if(success)success(operation, **responseObject**);

            } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
                if(failure)failure(operation, error);
                if(operation.response.statusCode == 0) {
                    ATAFNetworkingRequestObject *obj = [[ATAFNetworkingRequestObject alloc] init];
                    obj.urlString = URLString;
                    obj.paramters = parameters;
                    obj.successBlock = success;
                    obj.failureBlock = failure;
                    obj.type = ATNetworkingRequestGET;
                    if(![self duplicateRequestExists:obj])[pendingAPICalls addObject:obj];
                }

                [self logAPIFailedWithOperation:operation parameters:parameters error:error];
            } autoRetry:5 retryInterval:7];

do like after serilization based on your A string is coming instead of a bool. data:false instead of data:[] 在根据您A string is coming instead of a bool. data:false instead of data:[]之后,您会喜欢A string is coming instead of a bool. data:false instead of data:[] A string is coming instead of a bool. data:false instead of data:[]

if([datajsonObject isKindOfClass:[NSArray class]]){
    //Is array
}else if([datajsonObject isKindOfClass:[NSDictionary class]]){
    //is dictionary
}else if([datajsonObject isKindOfClass:[NSString class]])
 {
    //is String
 }
else{
    //is something else
}

You can check server value is Number or string as this 您可以检查服务器值是数字还是字符串

NSString *newString = [NSString stringWithFormat:@"%@",[[jsonObject valueForKey:@"total_count"]

if ([newString isKindOfClass:[NSNumber class]])
{
    NSLog(@"It is number");
}
if ([newString isKindOfClass:[NSString class]])
{
    NSLog(@"It is string");
}

Swift code : SWIFT代码 :

lblCount.text = String(datajsonObject["total_count"] as AnyObject)

Objective c : 目标c:

NSString *strCount = [NSString stringWithFormat:@"%@",[jsonObject valueForKey:@"total_count"]]

if ([strCount isKindOfClass:[NSString class]])
{
    // Write your code to show on label
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何配置 cellForRowAtIndexPath 来处理两种不同的自定义 UITableViewCell 类型 - How to configure cellForRowAtIndexPath to handle two different custom UITableViewCell types 解码具有不同类型的Json对象 - Decode Json object that has different types Swift - 循环遍历不同的对象类型 - Swift - Loop through different object types 将两种不同类型的对象作为一个函数变量传递 - Pass two different types of object as one function variable 通过NSDictionary迭代时处理不同的对象类型 - Dealing with different object types while iterating through an NSDictionary 将不同的对象(类型)坚持为单个对象-CoreData-(收藏夹示例) - Persist different objects (types) as single object - CoreData - (Favorites example) iPhone-尝试在表格视图中显示2种不同的对象类型 - iPhone - Trying to Display 2 different object types in a table-view 如何在UIPickerView中为两个不同的组件显示两个不同的对象类型? - How do I show two different object types in UIPickerView for two different components? RestKit:两个单独的提要,两种不同的对象类型。 一个对象管理器? - RestKit: two separate feeds, two different object types. One object manager? 实现不同的单元格类型 - Implementing Different Cell Types
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM