Xcode中的NSString或NSCFString? [英] NSString or NSCFString in xcode?

查看:95
本文介绍了Xcode中的NSString或NSCFString?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要像这样在NSString中获取一个NSMutabledictionary对象:

NSString *state=[d valueForKey:@"State"];

现在状态有时可能为null,有时会填充文本.因此,我正在比较它.虽然比较状态有时变为NSString,有时变为NSCFString..所以无法获得所需的结果.

if([state isEqualToString@""])
{
//do something 
}
else
{
//do something
}

因此,在比较时有时返回nil.因此请立即跳转到else块.

我需要一种标准的方法来比较状态是否为空,无论是NSString还是NSCFString ...

我该怎么办?

解决方案

如果您无法获得所需的结果,我可以向您保证,不是因为您得到了NSCFString而不是NSString. /p>

在Objective-C中,该框架填充了集群类.也就是说,您在文档中看到了一个类,实际上,它只是一个 interface .相反,框架具有自己的这些类的实现.例如,如您所指出的,NSString类通常由NSCFString类表示;相反,通常由NSCFString类表示.还有其他一些,例如NSConstantStringNSPathStore2,它们实际上是NSString的子类,并且其行为将与您期望的一样.

从我所见,您的问题...

现在状态有时可能为null,有时会填充文本.

...是在Objective-C中,在nil上调用方法是合法的. (Nil是其他语言(例如C#和Java)中null的Objective-C概念.)但是,当您这样做时,返回值始终为零.因此,如果字符串是nil,则即使与nil比较,使用方法对其进行的任何相等比较都将返回NO.即使这样,也请注意,空字符串nil并不相同,因为nil可以看作是没有任何东西.一个空字符串没有字符,但是,至少它在那里. nil表示什么都没有.

因此,您可能不需要使用简单的指针相等性来检查state不是nil,而不是使用一种方法将state与空字符串进行比较.

if(state == nil)
{
    //do something 
}
else
{
    //do something
}

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
}

这篇关于Xcode中的NSString或NSCFString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆