Why is NULL getting different result that nil? - iphone

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 */

Related

Comparing non-optional value of type 'Bool' to 'nil' always returns true

I have an if-else statement where I am checking if the value coming from user defaults is nil or not like this:
if defaults.bool(forKey: "abcd") != nil{
//Do something
}
else{
//do something else
}
But Xcode is giving me an error saying:
"Comparing non-optional value of type 'Bool' to 'nil' always returns true"
Can someone explain what's happening here and how to fix this?
bool(forKey:) returns a NON-optional, which cannot be nil. If the key is missing in the user defaults, the return value will be false.
If you want trinary logic here (nil/true/false) use object(forKey:) looking for an NSNumber, and if present, take its boolValue.
As
defaults.bool(forKey: "abcd")
will return false by default check Docs , so it will never be optional
The Boolean value associated with the specified key. If the specified key doesn‘t exist, this method returns false.
The func bool(forKey: "abcd") returns Bool type not optional.
Which means you cant compare it to bool, what you can do is simply:
if defaults.bool(forKey: "abcd") {
//Do something
} else {
//do something else
}
Now if the key exists and has true value it will get into the if statement, if it does not exists or is false it will go to the else.
If you have any doubts you can read about the func in the following Apple developer link: Apple:bool(forKey:)
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;
defaults.bool(forKey: "abcd") != nil
The first part, defaults.bool(forKey: "abcd"), returns a non-optional boolean. We know that because bool(forKey:) returns Bool, not Bool?. Therefore, you'll always get a Bool value, i.e. either true or false, never nil. Note the documentation:
If the specified key doesn‘t exist, this method returns false.
"Comparing non-optional value of type 'Bool' to 'nil' always returns true"
The compiler is simply pointing out that it knows that defaults.bool(forKey: "abcd") can't be nil, and since you're comparing it to nil, you're probably making a mistake. Your else block will never execute.
Can someone explain what's happening here and how to fix this?
It depends on what you mean for the code to do. If you want to take different actions depending on whether the value is true or false, then compare it to one of those values. If you want to get an optional value back, use object(forKey:) (which returns an optional) instead.
i solved like this:
if(Userdefaults.standart.bool(forkey: "blablabool"){
}
This works..
When you call this if its null it returns false.

Why casting nil value as Any is not considered as nil? [duplicate]

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;

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.
}

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

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!");