简体   繁体   English

Swift如何比较来自NSString的字符串

[英]Swift how to compare string which come from NSString

I get a return number(0 for success, 1 for failure) form sever made by php so I want get this number to judge my operation succeeded or not. 我得到了一个返回的数字(成功为0,失败为1),它是由php切断的,所以我想获取此数字来判断我的操作是否成功。 I use this : 我用这个:

var str1 :String = NSString(data: d, encoding: NSUTF8StringEncoding)!
let str2 = "1"
println(str1) // output is 1
println(str2) // output is 1

if(str1==str2){println("same")} //but the two is not same

so I debug for this and I get this result: //str1 _countAndFlags UWord 13835058055282163717 -4611686018427387899 //str2 _countAndFlags UWord 1 1 因此我对此进行调试,然后得到以下结果:// str1 _countAndFlags UWord 13835058055282163717 -4611686018427387899 // str2 _countAndFlags UWord 1 1

And I try to use toInt. 我尝试使用toInt。 I get 1383... form str3 and 1 form str4 So how can I do to solve this problem. 我得到1383 ...形式str3和1形式str4所以我该怎么做才能解决这个问题。 Thank you very much. 非常感谢你。

It sounds like you have some whitespace in your string. 听起来您的字符串中有一些空格。 To spot this using println , you could try println(",".join(map(str1,toString))) . 要使用println发现它,您可以尝试println(",".join(map(str1,toString))) If you see any commas at all, that's the problem. 如果您根本看不到逗号,那就是问题所在。

The easiest way to fix this (it may be better to kill the whitespace at the source) is to use stringByTrimmingCharactersInSet : 解决此问题的最简单方法(最好在源代码处消除空格)是使用stringByTrimmingCharactersInSet

let str1: String = NSString(data: d, encoding: NSUTF8StringEncoding)
                   ?.stringByTrimmingCharactersInSet(
                     NSCharacterSet.whitespaceAndNewlineCharacterSet())
let str2 = "1"
if str1==str2 { println("same") }

Note a few other changes: 注意其他一些变化:

  • let rather than var since it doesn't look like you need to change str1 after it's declared let而不是var因为看起来它不需要在声明后更改str1
  • No force-unwrap ( ! ) at the end of the creation of the NSString . NSString创建结束时不强制展开( ! )。 Never force-unwrap something that might be nil, you will get a runtime error! 切勿强行拆开可能为零的内容,否则会出现运行时错误!
  • ?. to optionally call the trim if it isn't nil. 如果不是nil,则可以选择调用修剪。

Note, this means str1 is a String? 注意,这意味着str1String? not a String but that's fine since you can compare optionals with non-optionals (they'll be equal if the optional contains a value equal to the non-optional, but not if the optional contains nil ) 不是String但这很好,因为您可以将可选对象与非可选对象进行比较(如果可选对象的值等于非可选对象的值,则它们相等,但是如果可选对象包含nil的值则不相等)

If what you actually want is an Int , just add a let int1 = str1?.toInt() . 如果您真正想要的是Int ,只需添加let int1 = str1?.toInt() This will still be an optional – if there is a reasonable default in case of nil , you could do let int1 = str1?.toInt() ?? 0 这仍然是可选的–如果在nil情况下存在合理的默认值,则可以let int1 = str1?.toInt() ?? 0 let int1 = str1?.toInt() ?? 0 and it will be non-optional with a value of 0 in case of nil . let int1 = str1?.toInt() ?? 0 ,如果为nil ,它将是非可选的,值为0

在swift 2中,可以使用let str = "string" let nsstr:NSString = "string" if nsstr.containsString(str){ print(true) }else{ print(false) }对我let str = "string" let nsstr:NSString = "string" if nsstr.containsString(str){ print(true) }else{ print(false) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM