I have made a function to consume webservices , now I want to check if (id) object is null or not. What I am doing is here:
我已经创建了一个使用webservices的函数,现在我想检查if(id)对象是否为null。我在做什么在这里:
-(void)callService:(NSString*)request param:(NSString*) parameters completion:(void (^)(NSArray *list, NSError *error))completionHandler
{
[self.manager POST:request parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
NSLog(@"Response Object: %@", responseObject);
if (![responseObject isKindOfClass:[NSNull class]]) {
[self methodUsingJsonFromSuccessBlock:responseObject];
if (completionHandler) {
//NSLog(@"%@", list);
completionHandler(list,nil);
}
}
else {
NSLog(@"Nothing found");
}
}failure:^(NSURLSessionDataTask *task, NSError *error) {
//NSLog(@"Error: %@", [error description]);
if (completionHandler) {
completionHandler(nil,error);
}
}];
}
but what I have found on break points is when (id) is null it has NSZeroData but no NSNull so it always passes my condition. I am using AFNetworking
但我在断点上找到的是当(id)为null时它有NSZeroData但没有NSNull所以它总是通过我的条件。我正在使用AFNetworking
Thanks in advance for helping me out.
在此先感谢帮助我。
3 个解决方案
#1
5
Your responseObject
is not NSNull
, but an NSData
(which _NSZeroData
is a subclass of).
您的responseObject不是NSNull,而是NSData(_NSZeroData是其子类)。
So what you really want is this condition:
所以你真正想要的是这种情况:
if ([responseObject isKindOfClass:[NSData class]] && ((NSData *)responseObject).length > 0) {
// ...
}
#2
0
This is not the fastest response, but it can be of some use to someone.
这不是最快的响应,但它对某人有用。
Here is what I've been using and what you can do:
以下是我一直在使用的内容以及您可以做的事情:
In some class, say "defaults.h" you can define following:
在某些类中,例如“defaults.h”,您可以定义以下内容:
#define Secure(x) x != (id)[NSNull null] ? x : @""
#define isSecure(x) x != (id) [NSNull null] ? YES : NO
Later, in your method you can use it like this:
稍后,在您的方法中,您可以像这样使用它:
if(isSecure(responseObject)){
doSuccess
}
else {
doError
}
First method can be used for example for strings, while the other is general.
第一种方法可以用于例如字符串,而另一种方法是通用的。
#3
-3
try this
尝试这个
put this on top
把它放在首位
#define isNSNull(value) [value isKindOfClass:[NSNull class]]
and then while comparing
然后在比较时
if (isNSNull(object) ...
hope it helps.
希望能帮助到你。
#1
5
Your responseObject
is not NSNull
, but an NSData
(which _NSZeroData
is a subclass of).
您的responseObject不是NSNull,而是NSData(_NSZeroData是其子类)。
So what you really want is this condition:
所以你真正想要的是这种情况:
if ([responseObject isKindOfClass:[NSData class]] && ((NSData *)responseObject).length > 0) {
// ...
}
#2
0
This is not the fastest response, but it can be of some use to someone.
这不是最快的响应,但它对某人有用。
Here is what I've been using and what you can do:
以下是我一直在使用的内容以及您可以做的事情:
In some class, say "defaults.h" you can define following:
在某些类中,例如“defaults.h”,您可以定义以下内容:
#define Secure(x) x != (id)[NSNull null] ? x : @""
#define isSecure(x) x != (id) [NSNull null] ? YES : NO
Later, in your method you can use it like this:
稍后,在您的方法中,您可以像这样使用它:
if(isSecure(responseObject)){
doSuccess
}
else {
doError
}
First method can be used for example for strings, while the other is general.
第一种方法可以用于例如字符串,而另一种方法是通用的。
#3
-3
try this
尝试这个
put this on top
把它放在首位
#define isNSNull(value) [value isKindOfClass:[NSNull class]]
and then while comparing
然后在比较时
if (isNSNull(object) ...
hope it helps.
希望能帮助到你。