iOS: Check if boolean is nil (Objective-C) - ios

I'm checking in the console if child viewcontroller is loaded:
po self.childVC.isViewLoaded
<nil>
My question to you guys is how can check if boolean is nil ?

What about checking like this?
If assume that boolean can be nil then..
// Can check nil & NO for isViewLoaded
if (self.childVC.isViewLoaded != NO) {
//blah
}

Already asked. Please check this How to check if a BOOL is null?
if (self.childVC.isViewLoaded != [NSNull null]) {
}
else {
}
Thanks

LLDB: As simple as p self.childVC.isViewLoaded
CODE: if (self.childVC.isViewLoaded) {//isViewLoaded = YES}

A BOOL is either YES or NO. There is no other state.
To check if the view is loaded add below code,
if (self.childVC.isViewLoaded && self.childVC.view.window) {
// self.childVC is visible
}

Related

how to solve null exception for var type variable

var Ongoing_List = _objApi.OngingList(user_id).ToList();
i have one function OngoingList(). when it return data it work approprite but when return then then var OngoingList return can not handle null exception. how to solve
check if it is not null and its count is greater than zero (it have values) as following
if(Ongoing_List != null && Ongoing_List.Count()>0){
//do your job
}else{
//don't do any thing with it here
}
also you may add try-catch block, which will throw an exception if something occurs as following
try{
if(Ongoing_List != null && Ongoing_List.Count()>0){
//do your job
}else{
//don't do any thing with it here
}
}catch(Exception ex){
//you can handle the error, maybe print it to a log file or something else
}
best regards

What is the Objective C equivalent of self.view.traitCollection.horizontalSizeClass?

What is the obj c code for below swift statement?
if (self.view.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClass.Compact)
what i figured out so far is proviede below but it is giving error when i add self.view
if ([self.view UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassCompact])
{
NSLog(#"Compact");
}
Try this
if (self.view.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact) {
// do your stuff
}
Hope it works

object return empty description in iOS

hello I am doing something like this.
NSString * fblink=[dm.commonDataArray valueForKey:#"fb_link"];
if(![fblink isEqual:[NSNull null]]||![fblink isEqualToString:#""]||![fblink length]<=0)
{
[fb_button setImage:[UIImage imageNamed:#"ico_user_fb"] forState:UIControlStateNormal];
[fb_button addTarget:self
action:#selector(buttonFBTouch:)
forControlEvents:UIControlEventTouchUpInside];
}else{
[fb_button setImage:[UIImage imageNamed:#"ico_fb_red_gray"] forState:UIControlStateNormal];
}
when I type fblink in my log it says (lldb) po fblink
<object returned empty description> But still my if condition become true and go inside ratherthan going to else part.
How can I overcome this problem?I dont want to execute my if condition true part if its return empty description. How can I checked this condition? Please help me
Thanks
The problem is with your if condition. fblink is nil, you are checking nil is equal to [NSNull null] so this case fails and you invert the result so it becomes true and goes inside of this if case.
If we expand your if case it becomes:
if(![nil isEqual:[NSNull null]]||![nil isEqualToString:#""]||![nil length]<=0)
{
}
Which becomes
if(!false||!false||!true) // which evaluates to true, you are using ||, so if any one condition is true, it will go inside of the if case
{
}
You need to change that if case to:
if([fblink isKindOfClass:[NSString class]] && ![fblink length]<=0)
{
}
else
{
}
you can try.....
if(![fblink isEqual:[NSNull null]]&&![fblink isEqualToString:#""]&&![fblink length]<=0)
{
//your code here .......
}
else
{//your code here ...}
I think that the fblink is not null but its an empty string. Thus your ![fblink isEqual:[NSNull null]] is becoming true and your if statement is getting executed. Only check it with the following condition:
if(![fblink isEqualToString:#""]||![fblink length]=0)
You have to change the || (or) condition to &&(and) condition.
Try this:
if(![fblink isEqual:[NSNull null]]&&![fblink isEqualToString:#""]&&![fblink length]<=0 )
{
//your code
}
else
{
// your code
}

How to check if an object is nil in Objective-C? [duplicate]

This question already has answers here:
Testing for nil in Objective-C -- if(x != nil) vs if(x)
(4 answers)
Closed 7 years ago.
So I know this seems pretty basic but it doesn't seem to be working for some reason. I have the following code.
Target *t = self.skill.target;
if (![t isEqual:nil]) {
NSLog(#"Not nil");
}
I tried this and it comes up as not nil everytime which is great except for when t should actually be nil. I even tried putting variation into the code like so and my t is still coming up as not nil for some reason. Am I doing something wrong? :\
Target *t = self.skill.target;
t = nil;
if (![t isEqual:nil]) {
NSLog(#"Not nil");
}
You can do it normally just by checking the object itself. There is also a good explanation on NSHipster for NSNull.
if( myObject ){
// do something if object isn't nil
} else {
// initialize object and do something
}
otherwise just use
if( myObject == nil ){
}
Target *t = self.skill.target;
t = nil;
if (t) {
NSLog(#"t is Not nil");
}
if (!t) {
NSLog(#"t is nil");
}
As this answer says:
Any message to nil will return a result which is the equivalent to 0 for the type requested. Since the 0 for a boolean is NO, that is the result.
So, a nil object is special. you can't compare it using the isEqual method. you should compare it without sending it a message, like this:
if (t)
{
}
or simply:
if (t != nil)
{
}
The if operation checks nillable so this should works:
Target *t = self.skill.target;
if (t) {
NSLog(#"Not nil");
}else{
NSLog(#"nil");
}
Hope it helps.

Substituting JSON null values in Mantle

I'm using Mantle to parse some JSON which normally looks like this:
"fields": {
"foobar": 41
}
However sometimes the value of foobar is null:
"fields": {
"foobar": null
}
This causes MTLValidateAndSetValue to throw an exception as it's trying to set a nil value via Key-Value Coding.
What I would like to do is detect the presence of this null value and substitute it with -1.
I tried overriding foobarJSONTransformer in my MTLModel subclass as follows:
+ (NSValueTransformer *)foobarJSONTransformer {
return [MTLValueTransformer transformerWithBlock:^id(id inObj) {
if (inObj == [NSNull null]) {
return [NSNumber numberWithInteger: -1];
} else {
return inObj;
}
}];
...and I can see this code being called but inObj is never equal to [NSNull null], hence the substitution doesn't happen and the exception is still thrown by Mantle.
What is the correct way to catch this JSON null case and do the substitution?
I had incorrectly assumed NSNull would be generated for a null JSON value. In fact it is parsed to a nil value.
So the solution is to check inObj against nil, rather than NSNull:
+ (NSValueTransformer *)foobarJSONTransformer {
return [MTLValueTransformer transformerWithBlock:^id(id inObj) {
if (inObj == nil) {
return [NSNumber numberWithInteger: -1];
} else {
return inObj;
}
}];
the substitution will then work as expected.
If you received as null, it will generated as NSNull. But if they missed this field or send it as empty, it will generate as nil while parsing. so better you can use below code
if (inObj == nil || inObj == [NSNull null] )
create a base model and inherit MTLModel,and override blow code:
- (void)setNilValueForKey:(NSString *)key {
[self setValue:#0 forKey:key];
}
if you property is oc object,it will be nil immediate,and can't execute the method.but it can execute if the property is int,bool or float.

Resources