How to check in an NSAssert of an variable is (null)? - iphone

the console prints me (null) for an variable that should not be, so I want to put an assert there just for fun:
NSAssert(myVar, #"myVar was (null)!");
what's the trick here?
this (null) thing doesn't seem to be "nil", right? How can I check for it? I want to assume that the var is set properly and not nil, not null.

Assuming myVar is of type id (or any object type), use
NSAssert(myVar != nil, #"")
If the assertion passes, you should be able to print myVar to the console with
NSLog(#"myVar = %#", myVar);
As a side note, nil==null, (both are #defined as __DARWIN_NULL which is #defined ((void *)0)). If you try to use NSLog to print the -description of a nil (or NULL) object reference you get "(null)"

do [myVar isEqualToClass:[NSNull null]] this returns yes if it is NSNull, if its nil you can do if(myVar==nil)

I usually do
if (object != nil)
doSomething(object);
else
NSLog("bad object!");

Related

if statements and optionals in Swift

Is there a difference between something like
if let error = error {
print(error.localizedDescription)
}
and just checking if it is = nil
if error != nil {
print(error.localizedDescription)
}
if I want to check if error has a value? Think of firebase's creating user function.
Yes.
The if let statement allows you to bind the value of error to a variable if it is non-nil and use it in the block. If maybeError is of type Error?, when you do:
if let error = maybeError {
/* block contents */
}
the type of error will be Error within the block - ie: It won't be an optional anymore. If you just do a nil-check using if, error will still be of type Error? within the block. So the code that would actually be equivalent to your first snippet would be:
if error != nil {
print(error!.localizedDescription)
}
(Your second snippet, as it is, won't compile, as you're trying to get the localizedDescription variable of an Error? object, which has no such property)
By the way, in case you haven't seen it before, the !. thing is the unwrap operator. It runs the method on the object if the object is non-nil, but it crashes in case the object is nil. In this case, you generally know it won't crash. (But this might not actually be safe depending on where and how you use it - check #rmaddy's comment)
In the first, error is now a non-optional type, so you can use .. This is also idiomatic -- it more clearly shows what you are trying to do.
In the second, you would need to use ?. to look at error's properties (your code won't compile because you haven't done that).

Comparing non-optional Any to nil is always false?

I'm iterating through a dictionary of [String: Any], looking for nils, so I can replace them with NSNull for a JSON write. My precompiler warning is telling me that comparing an Any to a nil will always be false, but I know it contains at least two nils which are never found. Is there a way to check is an Any is nil?
An Optional can be nil. Anything else can never be nil. An Any is not an Optional. Thus there is no point comparing an Any to nil. The test will never succeed.
If you know that these things might be Optionals, you should have typed this as Any?. That is an Optional and can be compared to nil. Here's a simple example:
let s : String? = nil
let any : Any? = s
if any == nil {
print("nil") // nil
}
As you can see, the test succeeds.
(Still, if at all possible, it would be even better to type things more precisely.)
I have solved this using bellow expression:
let filteredResult = dictionary.filter { !(($0.value as AnyObject) is NSNull) }
if(object_getClass(yourVariable)?.description() == "NSNull")
can be one of the way to check.
Objective-c property in swift.
If you're using some objective c property in swift and it says something like "Comparing non-optional value of type 'XYZ' to 'nil' always returns true" you have to make that objective c property to "_Nullable" so that property may not be optional anymore. Like
#property (strong,nonatomic) NSString *_Nullable someString;

How to check null value of object in decision statement for Objective C

I am getting a object value from server as null value when NSlog this object.I want to identify it in if-else decision statement. How can I check it because nil have reference to a unknown object which not means NULL.and i can't compare it with zero too.
How can i identify that this value is NULL, i have a crash on this point.I have tried #try - #catch block too but all gone in vain.
Any suggestion for this problem.
As others have pointed out, there are many kinds of "null" under Cocoa/Objective C.
But one further thing to note is that [object isKindOfClass:[NSNull class]] is pointlessly complex since [NSNull null] is documented to be a singleton so you can just check for pointer equality. See Topics for Cocoa: Using Null
So use this :-
if (title == (id)[NSNull null] || title.length == 0 ) title = #"Something";
Note how you can use the fact that even if title is nil, title.length will return 0/nil/false, ie 0 in this case, so you do not have to special case it. This is something that people who are new to Objective C have trouble getting used to, especially coming form other languages where messages/method calls to nil crash.
If you want in detail what is the difference between nil, Nil and null, you can check this article What is the difference between nil, Nil and null.
You can try following code to check for NULL values from server:
if (nil == str || NSNull.null == (id)str) {
//Object has Null value
}
else{
// Object has some value
}
str is string value which contain server value.
This may helps you.
The Best Approach is :
if([yourObject isKindOfClass:[NSNull null]])
{
// yourObject is null.
}
else
{
// yourObject is not null.
}

Can't set a BOOL to yes

I've ran into this problem and so far I can't find any answers. so a have a BOOL property and I am trying to set it to YES/TRUE/true ( I have tried them all) but whenever I check the BOOL it always returns NO/FALSE/false. Below is my code
[myObject setAllowReg:YES];
myObject.allowReg = YES;
if (myObject.allowReg)
{
NSLog(#"YES");
}
else
{
NSLog(#"NO");
}
The output to this is always "NO". This must be something really simple that I am missing but it seems whatever I try doesn't work.
Thanks
Beat guess is that myObject.allowReg is nil and probably myObject is nil.
Add some NSLog statements to help figure this out:
NSLog(#"myObject: %#", myObject);
NSLog(#"myObject.allowReg: %i", myObject.allowReg);
Advice: In debugging break everything down to the simplest statements and test each step along the way.
myObject is probably nil. You can send messages to nil objects, they always return a 0 value.

Why is NULL getting different result that nil?

For AVAudioPlayer, when I initWithContentsOfURL:error: if I pass nil it doesn't work, but if I pass NULL it does. And in the documentation, it specifically says to pass NULL. Why is this?
The reason why is because nil is usually used for an Objective-C object type, while NULL is used for c-style pointers
I would suspect something else is going on. If you check the definition of nil, it's the same as NULL:
#ifndef nil
#define nil NULL
#endif /* ! nil */