I have used the below function
while(labelZ.text!=0)
but I'm getting a typecast error.
To check if a string is not set, use:
labelZ.text == nil
To check if a string is an empty string, use:
[labelZ.text isEqualToString:#""]
To check if a string equals "0", use:
[labelZ.text isEqualToString:#"0"]
Use nil instead of 0.
Try this
while((int)labelZ.text!=0)
Related
i wanna solve below error
Thanks for help
As the error states you cannot compare a String (mycheck) with an array of Any (localMsg), you must compare the string directly with something like
if let localMsg = oDict_Fail["message"] as? String, localMsg == mycheck {
NSLog("True")
} else {
NSLog("False")
}
Here we first try to access the oDict_Fail dictionary with the key message, if it exists try to cast it as a String and if it is successful only then do the comparison, if any of this fails the else branch will be executed.
You can read more about optional binding here.
Following code
extension String {
func isValidEmail() -> Bool {
if self == nil { return false }
Gives me error Value of type 'String' can never be nil, comparison isn't allowed
Is it possible to somehow check for nil here?
Checking for nil there is not required.
That function is an instance function on String.
It can only ever be run on an instance of String.
Secondly Swift does not have "nil messaging" like Objective-C so the String instance that the function is called on HAS to be not nil. Even in Objective-C this would still not matter as the function would not run if called on a nil String.
So, the message is correct, Value of type "String" can never be nil.
You could check if your string is empty as:
var myString : String?
if myString.isEmpty {
// do whatever you want ..
}
That's has more sense..
Recall that in Swift, values of type String can never be nil; if you wanted a nillable value, you'd have to declare it String?.
So no, there is no way to check if a String variable is set to nil, because it can't.
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.
}
If I want to check whether a UITextField or NSString is empty, can I compare it with NULL or nil?
Neither of the methods you suggest are foolproof. The best tests are:
if ([myTextField.text length] > 0) ...
or
if ([myString length] > 0) ...
if i want to check whether a textfield or string is empty i compare it with NULL or nil?
No.
An empty string object (a string object containing no characters) or a text-field object containing an empty string object is not the same as nil, which is no object at all. You need to ask the (text field's) string how long it is, or ask it whether it is equal to an empty string you have on hand (#"").
NULL, while also a null pointer, should be used for general pointers, not pointers to Objective-C instances (for which you have the more specific nil) or classes (for which you have the more specific Nil).
I had a similar problem but no method other than this worked for me:
NSString *string = textfield.text;
if ([string isEqualToString:#""]) {
....
}
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!");