String matching in iOS returning 0x00000001? - ios

i have a simple string checking code, which will check NSString which coming from my server to the NSString which i hard coded in my xcode.
Please check the code
if([[array valueForKey:#"type"] isEqualToString:#"type"] ) {
//Failed
}
input values are these
[array valueForKey:#"type"] is a string from server 'type'
When i did this in xcode console
po [[array valueForKey:#"type"] isEqualToString:#"type"]
i got output as
0x00000001
Both strings are same but then what is this 0x00000001??

What are you doing now in printing the result of the comparison, to check the value of string you have to print [array valueForKey:#"type"] value alone, or print value of the all items of the array just to be sure.
Hope that helps.

[array valueForKey:#"type"] is going to return a value most likely different than a string. Assuming array is a NSDictionary filled with NSIntegers then the integer for key "type" is not going to be equal to the string "type".

I thought you got a hexadecimal value. All hexadecimal values start from 0x. You got value equals to 1. In if statement 1 equals to true. So your compared strings are equal. This 0x0000000000000001 value comes from a compiler and that way compiler designed.

Here, [array valueForKey:#"type"]
array must be NSDictionary... because array don't have "valueForKey:" method..
Better you first save in some var and then do comparison.

Related

How to Pass Empty Values to Array?

I have Data. It has empty value and the values are string.In that some of the strings are empty.It does not have value.Now I want to pass empty string values to Array.Application is crashing if I pass empty values(null and empty) to Array.How to check and send value to Array.Can anyone help me please?
STEP 1: check string is empty or not
- (NSString *)checkEmpty:(NSString *)check
{
#try
{
if (check.length==0)
check = #" ";
if([check isEqual:[NSNull null]])
check = #" ";
}
#catch (NSException *exception)
{
check = #" ";
}
}
STEP 2:Adding the String to Array
[array addObject:[self checkEmpty:strValue]];
If the string value is empty,it takes as above coding after that it passes or adds to array.If the string has value it directly adds to the array.
You can use
array_filter();
Which manage empty array.
If I have understood your problem correctly then you are trying to insert a nil value to an array
You cannot insert a nil or a Null value to an array an empty string would be #"".
If this is not what you are looking for please be more elaborate and attach the piece of code along with your question as your question is not clear

iOS converting value in NSDictionary with (int) fail

I had a NSDictionary contains 2 key/value pairs:
NSDictionary *dic = #{#"tag":#2, //NSNumber
#"string":#"someString"}; //NSString
NSLog(#"%i",(int)[dic objectForKey:#"tag"]); //print out 34
NSLog(#"%i",[dic objectForKey:#"tag"] intValue]); //print out 2
Why does "converting id value to int with (int)"get me the wrong result but not the other way? are they in different levels of conversion?
Why does "converting id value to int with (int)"get me the wrong result but not the other way? are they in different levels of conversion?
id is a pointer type. id pointers point to Objective-C objects in memory. By casting id to (int), you are merely reinterpreting (some of) the pointer's bit pattern as an int, which is quite meaningless. You have to call the proper conversion methods of NSString and NSNumber if you want to reliably get the primitive values out of the Objective-C object.
If you ever seemingly get the "correct" value of 2 in the case of pointer-casting with NSNumber, that may be because the Objective-C runtime makes use of an optimization technique called tagged pointers, whereby small objects are not really created and allocated, but their semantics (the number's bits which the NSNumber object stores) is stuffed into the unused bits of the pointer.
#2 is not an int but a NSNumber you can't cast an NSNumber into an int. You have to use intValue method to get the correct result.
The method objectForKey: returns a pointer to the NSNumber object #2, not the value stored in the object itself. So you're typecasting the pointer, not the value 2. In the last line you don't typecast the object but you access a property called intValue which returns the value of the object expressed as an int.
NSDictionary contains Object with Key value pairs,but you passed int(#2) into object
NSDictionary *dic = #{#"tag":#2, //NSNumber
#"string":#"someString"};
so Change int to NSNumber like
NSDictionary *dic = #{#"tag":[NSNumber numberWithInt:2];,#"string":#"someString"};
and you can get it..
int number = [[dict objectForKey:#"tag"] intValue];

String Passes Empty Test, Returns null

I am testing to see if a string is stored in NSUserDefaults. In theory, I thought the following was the answer:
if ( ![[[NSUserDefaults standardUserDefaults] authorName] isEqual: #""] )
{
NSLog(#"The default must be set, the string is not equal to empty.");
NSString *authorName = [[NSUserDefaults standardUserDefaults] authorName];
}
In practice, it always tested that something was there, but then returned a value of "(null)", when I try to use it. Can someone tell me why?
Ultimately, I think I've solved the problem with the following, but I'm not clear why the first test case did not work as I expected.
if ( [[[NSUserDefaults standardUserDefaults] authorName] length] != 0 )
Thanks.
The nil string is not equal to the empty string. That is why the first test passes but returns nil - because NSUserDefaults will return nil if no value is stored.
The reason why comparing length to zero works, is because when calling a method on a nil reference in objective-c, nil is returned. This is different from most other language, which would instead throw an exception in this case. Because length is NSInteger, nil becomes zero.
Comparing length to zero should be safe, but what you really is trying to test is whether a string is stored for the given key in the defaults. In that case, you can simply compare to nil:
if ([[NSUserDefaults standardUserDefaults] authorName] != nil)
{
....
}

how to handle boolean value with AFNetworking parsing JSON

I have some JSON that comes back like this:
"items":[
{
"has_instore_image": false
}
]
If I output the value like this:
NSLog(#"has_instore_image val: %#", [item objectForKey:#"has_instore_image"]);
I get
has_instore_image val: 0
but if I test like this:
if([item objectForKey:#"has_instore_image"]==0){
NSLog(#"no, there is not an instore image");
}else{
...
It always goes to the else statement... hmmm.. How would you suggest I get the BOOL value and test? I've read through the BOOL questions here and am just confused this is not working as I'd anticipate.
thx
NSDictionary's instance method objectForKey returns an id, not a primitive value.
If it's a boolean, int, float, etc number-like value in JSON, it will serialized to an NSNumber by Apple's NSJSONSerialization class and most/all other common JSON parsers in iOS.
If you want to get the BOOL value out of it, you can do something like this:
BOOL has_instore_image = [[item objectForKey:#"has_instore_image"] boolValue];
You are comparing a pointer with an integer here
[item objectForKey:#"has_instore_image"]==0
You should use
[item objectForKey:#"has_instore_image"].integerValue==0
Also note that a BOOL of NO equals 0.
The NSLogstatement in your code prints a 0, but only because if you give NSLogan object as parameter, the objects descriptionis called.
i will suggest to hold these id type (returned from dictionary) to NSNumber .
NSNumber *boolNum=(NSNumber*)[item objectForKey:#"has_instore_image"];
after that you can get bool value from boolNum
[boolNum boolValue]
try this
if([boolNum boolValue]==NO){
NSLog(#"no, there is not an instore image");
}else
{
}

Why is this code not recognising the NSString as being equal?

This is the code I have:
NSLog(#"name: %#", name);
NSLog(#"service: %#", service.name);
if (name == service.name) {
NSLog(#"Test");
}
Name is "Andrew’s MacBook Pro".
Service is "Andrew’s MacBook Pro"
And yet I don't get a "Test" from NSLog. Any ideas why this could be?
use [string isEqualToString:#"any string"]
See a very useful discussion here: Understanding NSString comparison
For string comparisons, use [name isEqualToString:service.name]
Using == will compare to see if both pointers point to the same object, not if they point to objects with the same contents. Even if both pointers contain the same string, that does not mean they point to the same object.
If two people both have the same car, and so have the same key to unlock it, both keys are not equal and will not open both cars; each will only open the car for which it was made. If one person has a car but has an extra key made, they are equal because they open the same car (object). You can think of the pointers in this way.
You are comparing two objects not two string. Try [string isEqualToString:#"another string"].
In Objective C use [string1 isEqualToString:#"string2"]; for string comparison.
Here is the code :
NSLog(#"name: %#", name);
NSLog(#"service: %#", service.name);
if ([name isEqualToString:service.name])
NSLog(#"Strings are Equal");
else
NSLog(#"Strings are Not Equal");

Resources