2

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?

Sindhia
  • 411
  • 3
  • 12
  • 33

3 Answers3

2

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
}
zneak
  • 134,922
  • 42
  • 253
  • 328
  • So what shall I do and how to compare? – Sindhia Dec 05 '12 at 06:14
  • @Sindhia, I was editing my answer when you commented, you should have your answer now. – zneak Dec 05 '12 at 06:18
  • as u have said if I compare it with if(state == nil) eventhough state doesnot contain any text,it is jumping to else block.and when i see the console for description of state it says...Printing description of state.Thats it.no null or nothing..What to do ? – Sindhia Dec 05 '12 at 06:26
  • @Sindhia, how do you print `state`? You didn't show that in your question. – zneak Dec 05 '12 at 07:10
  • I mean if I mouseover that state i can see .So rightclicking and printing decsription – Sindhia Dec 05 '12 at 07:59
  • beside I could recommend the `state.length > 0` as well (or `... == 0`), it can give a bit more information of the `state` than `state == nil`, if you need more information, of course. – holex Dec 05 '12 at 09:28
  • It's true that `[state length] > 0` should work for all cases, since `[nil length]` will return zero, too. So I guess that would be the most concise way of doing it. – zneak Dec 05 '12 at 16:05
1

You can do this

if([state isEqualToString:@""])
{
//do something 
}
else
{
//do something
}
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
-1

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
      }
  }
junaidsidhu
  • 3,539
  • 1
  • 27
  • 49
  • 5
    No, you don't have to typecast. In C and Objective-C, a typecast from a pointer type to another pointer type only shuts up the compiler, it does nothing at runtime. – zneak Dec 05 '12 at 06:10
  • k if typecasted how to compare?if(state==nil)?or something elz? – Sindhia Dec 05 '12 at 06:15
  • No im not asking this.Actually state should compared whether it is NSString or NSCFString returning null or nil or empty . – Sindhia Dec 05 '12 at 06:48