if ([dict objectForKey:#"photo"] !=(id)[NSNull null])
{NSLOG(#"dictinary is not empty")}
This is not working for me. to check empty tag
Use count.
For example:
if ([dict count] == 0) {
NSLog("empty");
}
If you want to check for a key then:
if([dict objectForKey:#"photo"]) {
NSLog(#"There's an object in photo");
}
This is working for me. You can handle Null with this code.
[dict isKindOfClass:[NSNull class]];
If the objectForKey:#"photo" is null when not present you can just do: if ([dictionary objectForKey:#"photo"]) {NSLog(#"dictionary is not empty);}
try this code for check is null value or not
NSString *value=[dict objectForKey:#"photo"];
if (value.length != 0 )
{
NSLog("not empty!");
}
else
{
NSLog("empty!");
}
I have taken into the string like :
NSString *strPhoto=[dict objectForKey:#"photo"];
if (strPhoto ==(id)[NSNull null] || [strPhoto isEqualToString:#""] || strPhoto==nil || [strPhoto length]==0) {
}
It is working for me !!!!!!!!!!
Related
I have a Textfield, and when i print its text to a NSLog, i get the output as (null). I want to detect this how can i do this programatically.
I have tried several approaches and all of the following failed.
NSLog (#"Print %# ", textfield.text);
if ([textfield.text isEqualToString:#""]) {}
if ([textfield.text isEqualToString:#"(null)"]) {}
if ([textfield.text isEqualToString:nil]) {}
How can i detect (null) which is returned when printed using NSLog ?
I think you mean something like this:
if (textfield.text == nil)
{
NSLog( #"textfield is nil");
} else {
if( [textfield.text length] == 0 )
{
NSLog( #"textfield has zero length")
} else {
NSLog( #"textfield is %#", textfield.text);
}
}
You can do it using
If([textField.text isEqualToString:#""])
{
//Your code ....
}
or you can do it with
if(textField.text == Nil)
{
//Your code ....
}
third option is that
if([textField.text length] == 0)
{
//Your code ....
}
Code:
NSString *tempPhone = [NSString stringWithFormat:#"%#", [personDict objectForKey:kPhoneKey]];
NSLog(#"NSString *tempPhone = %#",tempPhone);
Output:
NSString *tempPhone = <null>
Now I want to add if condition, for not null; I tried followings:
if (tempEmail != NULL)
if (tempPhone != Nil)
if (tempPhone != nil)
if (![tempPhone compare:#"<null>"])
if (tempPhone != (NSString*)[NSNull null])
I also Checked this Post.
None of them is working for me. Where I am going wrong??
Try the following code
id phoneNo = [personDict objectForKey:kPhoneKey];
if( phoneNo && ![phoneNo isKindOfClass:[NSNull class]] )
{
NSString *tempPhone = [NSString stringWithFormat:#"%#", [personDict objectForKey:kPhoneKey]];
NSLog(#"NSString *tempPhone = %#",tempPhone);
}
else
{
NSLog(#"NSString *tempPhone is null");
}
I think your personDict does not have an object for the key kPhoneKey, so it is returning nil. When you format nil using %#, you get the string "(null)".
id object = [personDict objectForKey:kPhoneKey];
if (object) {
NSString *tempPhone = [NSString stringWithFormat:#"%#", object];
NSLog(#"NSString *tempPhone = %#",tempPhone);
} else {
// object is nil
}
if (tempPhone != nil || [tempPhone isEqualToString:#"(null)"])
Works for me.
It's like this :
if (![tempPhone isKindOfClass:[NSNull class]] && tempPhone != nil)
if ([str_name isKindOfClass:[NSNull class]])
works for me...
I Checked the webResponse (JSON, in my case).
It was returning me the string with value:
<null>
So, The following condition worked for me:
if (![tempPhone isEqualToString:#"<null>"])
Thanks for all your time. Thanks.
Because compare method returns type NSComparisonResult which is defined as
enum _NSComparisonResult {NSOrderedAscending = -1, NSOrderedSame, NSOrderedDescending};
typedef NSInteger NSComparisonResult;
If the string is the same, it will return NSOrderedSame which have a NSInteger value of 0.
Thus, the following line actually means...
if (![tempPhone compare:#"<null>"]) // `tempPhone` is equals to `#"<null>"`
or in a more understandable explanation, if tempPhone value is equal to #"<null>".
You should write it as
if ([tempPhone compare:#"<null>"] != NSOrderedSame)
or
if (![tempPhone isEqualString:#"<null>"])
In case the string is having a null value. Example in NSLog.. Implement this way..
if ([StringName isKindOfClass:[NSNULL Class]]) {
}
else {
}
First we need to check string length.
if (tempPhone.length == 0)
`
{
//Your String is having empty value (E.x, (null))
}
else
{
// You having some values in this string
}`
I am develop a iPhone application, in which i need to use JSON to receive data from server.
In the iPhone side, I convert the data into NSMutableDictionary.
However, there is a date type data are null.
I use the following sentence to read the date.
NSString *arriveTime = [taskDic objectForKey:#"arriveTime"];
NSLog(#"%#", arriveTime);
if (arriveTime) {
job.arriveDone = [NSDate dateWithTimeIntervalSince1970:[arriveTime intValue]/1000];
}
When the arriveTime is null, how can i make the if statement. I have tried [arriveTime length] != 0, but i doesn't work, because the arriveTime is a NSNull and doesn't have this method.
the NSNull instance is a singleton. you can use a simple pointer comparison to accomplish this:
if (arriveTime == nil) { NSLog(#"it's nil"); }
else if (arriveTime == (id)[NSNull null]) { // << the magic bit!
NSLog(#"it's NSNull");
}
else { NSLog(#"it's %#", arriveTime); }
alternatively, you could use isKindOfClass: if you find that clearer:
if (arriveTime == nil) { NSLog(#"it's nil"); }
else if ([arriveTime isKindOfClass:[NSNull class]]) {
...
In a single line
arriveTime ? job.arriveDone = [NSDate dateWithTimeIntervalSince1970:[arriveTime intValue]/1000]; : NSLog(#"Arrive time is not yet scheduled");
Str is empty i mean i enter nothing in TextFied even then it's going in else case
NSString *Str= textField.text;
NSLog(#"%#",Str);
if([Str isEqualToString:#""])
{
NSLog(#"Hi");
}
what about
if ( [string length] <= 0 )
use this
if(str == NULL)
you can use
if(!textField.text.length)
or
if([txtField.text isEqualToString:#""])
The Below code will check for the empty textfield and even for the whitespaces.
NSString *firstNameWithNoSpaces = [firstNameTxtField.text stringByReplacingOccurrencesOfString:#" " withString:#""];
if (![firstNameWithNoSpaces length] <= 0 ) {
}
you can also use this
if([textfield.text isEqualToString:#""] || textfield.text== nil )
I want to ask a question about the objective C. Does the NSString * contains some functions to check the NSString * contains some string in the UITextField.text? For example
NSString *checkString = #"abcd%"
if(checkString contains '%') // I want this function
return YES;
else
return NO;
if([checkString rangeOfString:#"%"].location != NSNotFound)
// hooray!
You can use - (NSRange)rangeOfString:(NSString *)aString. The code will look something like:
NSRange range = [UITextField.text rangeOfString:#"!"];
if (range.length > 0){
NSLog(#"String contains '!'");
}
else {
NSLog(#"No '!' found in string");
}
The code from the previous post is not correct
NSRange range = [UITextField.text rangeOfString:#"!"];
if (range.length >= 0){
NSLog(#"String contains '!'");
}
else {
NSLog(#"No '!' found in string");
}
It should be "range.length > 0"