将NSCFString转换为NSString [英] Convert NSCFString to NSString

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

问题描述

我从服务器获取字典

myDictionary = 
{
"rank":"1",
"color":"red",
"position":"middle"
}

现在我想在if条件下检查关键位置的值

Now I want to check the value of key "position" in if condition

我这样做

if ([[myDictionary valueForKey:@"position"] isEqualToString:@"middle"]) {
        //do Some stuff
} else{
        //do some other stuff
}

但是 [myDictionary valueForKey:@position] 的数据类型是 _NSCFString ,所以它没有正确地比较值并且永远不会进入if循环,即使值正确。

but data type of [myDictionary valueForKey:@"position"] is _NSCFString, so it does not compare value properly and never goes in if loop even the value is correct.

如何将其转换为 NSString 以便我可以比较一下条件吗?

how do I convert it into NSString so that I could compare it in if condition ?

我见过这些问题..

NSString实例将其类报告为NSCFString

从NSCFString获取NSString

xcode中的NSString或NSCFString?

来自这些问题我刚才知道
NSString 实际上是不同类型的字符串对象的容器类。通常,NSString构造函数确实返回一个实际类型为 NSCFString 的对象,它是Core Foundation的一个瘦包装器 CFString struct c>。

from these question I just came to know that NSString is really a container class for different types of string objects. Generally an NSString constructor does return an object that is actually of type NSCFString, which is a thin wrapper around the Core Foundation CFString struct.

但他们没有帮助我......没有人实际上告诉如何转换成NSString,所以请不要将其标记为重复。

推荐答案

你不要不需要将 _NSCFString 转换为 NSString 。作为 NSString 的子类,它保证响应 -isEqualToString:(以及的NSString )。您的问题不是来自字符串,可能来自 myDictionary 。尝试记录字典中的所有键,并确保其表现符合预期。

You don't need to convert _NSCFString to NSString. As a subclass of NSString it's guaranteed to respond to -isEqualToString: (and every other method in NSString). Your issue is not coming from the string, likely it is coming from myDictionary. Try logging all the keys in the dictionary and make sure it is behaving as expected.

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

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