NSString or NSCFString in xcode? - iphone

I m taking a NSMutabledictionary object in NSString like this :
NSString *state=[d valueForKey:#"State"];
Now sometimes state may be null and sometimes filled with text.So Im comparing it.While comparing state becomes NSString sometimes and NSCFString othertimes..So unable to get the desired result..
if([state isEqualToString#""])
{
//do something
}
else
{
//do something
}
So while comparing it is returning nil sometimes.So immediately jumping into the else block.
I need a standard way to compare if the state is empty whether it is a NSString or NSCFString ...
How can I do it?

If you're unable to get the result you want, I can assure you it's not because you get a NSCFString instead of a NSString.
In Objective-C, the framework is filled with cluster classes; that is, you see a class in the documentation, and in fact, it's just an interface. The framework has instead its own implementations of these classes. For instance, as you noted, the NSString class is often represented by the NSCFString class instead; and there are a few others, like NSConstantString and NSPathStore2, that are in fact subclasses of NSString, and that will behave just like you expect.
Your issue, from what I see...
Now sometimes state may be null and sometimes filled with text.
... is that in Objective-C, it's legal to call a method on nil. (Nil is the Objective-C concept of null in other languages like C# and Java.) However, when you do, the return value is always zeroed; so if you string is nil, any equality comparison to it made with a method will return NO, even if you compare against nil. And even then, please note that an empty string is not the same thing as nil, since nil can be seen as the absence of anything. An empty string doesn't have characters, but hey, at least it's there. nil means there's nothing.
So instead of using a method to compare state to an empty string, you probably need to check that state is not nil, using simple pointer equality.
if(state == nil)
{
//do something
}
else
{
//do something
}

You can do this
if([state isEqualToString:#""])
{
//do something
}
else
{
//do something
}

You must have to type cast it to get the correct answer.
NSString *state = (NSString *) [d valueForKey:#"State"];
if(state != nil)
{
if(state.length > 0)
{
//string contains characters
}
}

Related

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

Testing object equality, i.e. the same physical address?

I am checking if an object I am getting back from the NSURLConnectionDataDelegate is the same object that I originally created. What I have been doing is:
// TESTING TO SEE IF THE RETURNED OBJECT IS THE SAME ONE I CREATED
if(connection == [self connectionPartial]) {
But was just curious is this is the same as doing:
if([connection isEqual:[self connectionPartial]]) {
It's not the same.
if(connection == [self connectionPartial]) {
This compares the address of the objects, eg. if the pointers point to the same instance.
if([connection isEqual:[self connectionPartial]]) {
This compares the contents of the objects. For instance for two separate NSString instances, this will return YES as long as the string content is the same:
NSString *s1 = #"Something";
NSString *s2 = #"Something";
BOOL sameInstances = (s1 == s2); // will be false, since they are separate objects.
BOOL sameContent = [s1 isEqual:s2]; // will be true, because they both are "Something"
The first snippet compares the values of the pointers themselves, just as if they were any primitive type like an int. If the addresses are the same, the expression will evaluate true.
The second sends the message isEqual: to one of the connection instances. Any class can override isEqual: to define "equality" with another instance. It's entirely possible for a class's implementation of isEqual: to be:
- (BOOL)isEqual: (id)obj
{
return arc4random_uniform(2) ? YES: NO;
}
So, no, for almost all classes they are not equivalent. (NSObject, which has the "default" implementation of isEqual:, uses the objects' hashes, which, again by default, are their addresses.)
It sounds like using the equality operator, ==, is correct in your case.

iPhone : Difference between nil vs Nil and true vs TRUE

What is difference between nil and Nil in iOS development?
And similarly what is difference between true and TRUE in iOS development?
I think this will help you understand the difference between nil and Nil.
Please find the below link:
Refer the answer of Damien_The_Unbeliever which states:
Googling "Nil vs nil" found this post http://numbergrinder.com/node/49, which states:
All three of these values represent null, or zero pointer, values. The
difference is that while NULL represents zero for any pointer, nil is
specific to objects (e.g., id) and Nil is specific to class pointers.
It should be considered a best practice of sorts to use the right null
object in the right circumstance for documentation purposes, even
though there is nothing stopping someone from mixing and matching as
they go along.
Link for that answer can be seen here:
What does 'Nil' represent in Obj-C?
EDIT-2:
Is there a difference between YES/NO,TRUE/FALSE and true/false in objective-c?
nil is the literal null value for Objective-C objects, corresponding to the abstract type id or any Objective-C type declared via #interface. For instance:
NSString *someString = nil;
NSURL *someURL = nil;
id someObject = nil;
if (anotherObject == nil) // do something
Nil is the literal null value for Objective-C classes, corresponding to the type Class. Since most code doesn’t need variables to reference classes, its use is not common. One example is:
Class someClass = Nil;
Class anotherClass = [NSString class];

Trouble comparing NSString to NSArray object

Ok, so I'm trying to check if an object from an NSArray equals something inputed by the User into a UITextField. It should work, but for some reason it dosn't. Here is my code:
if (theAnswer.text == [correctAnswers objectAtIndex:problemNumber]) {
NSLog(#"CORRECT");
}
else {
NSLog(#"wrong");
}
The console always give wrong.
I put this log in:
NSLog(#"%# %#", theAnswer.text, [correctAnswers objectAtIndex:problemNumber]);
And I get A A
wrong
printed everytime. Thanks for the help
Objective-C doesn't support the == operator for NSStrings. That will do a comparison of the pointers to the NSStrings and not the contents of the string itself.
Try
if([theAnswer.text isEqualToString:[correctAnswers objectAtIndex:problemNumber]]) {
}

If UITextField or NSString is empty

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:#""]) {
....
}