最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift中string转换为NSData失败:Cannot convert value of type String to type NSData in coercion

Swift crifan 4072浏览 0评论

swift代码出错:

Login.swift:93:87: Cannot convert value of type ‘String’ to type ‘NSData’ in coercion

去折腾:
swift 将json字符串转换为json字典变量
期间,需要将字符串转换为NSData后再去解码。
但是直接强制转换:
let decodedJsonDict:NSDictionary = try NSJSONSerialization.JSONObjectWithData(respJsonStr as NSData, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
出错:
Cannot convert value of type ‘String’ to type ‘NSData’ in coercion
搜:
swift string to NSData
自己试了:
let respJsonData:NSData = respJsonStr.dataUsingEncoding(NSStringEncoding)
不行。
而且也不知道另外一个函数:
let respJsonData:NSData = respJsonStr.dataUsingEncoding(NSStringEncoding, allowLossyConversion: Bool)

的区别。

参考:
的:
NSUTF8StringEncoding
和:
的:
NSASCIIStringEncoding
但是NSStringEncoding加上点,后面都没有这两个选项。
最后用:
       letrespJsonData:NSData= respJsonStr.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion:false)!
        print(“respJsonData=\(respJsonData)”)
输出:
respJsonData=<7b226163 74697665 223a2074 7275652c 22636f6e 74616374 73223a20 5b5d2c22 646f635f 74797065 223a2022 75736572 222c2265 6d61696c 223a2022 63726966 616e4064 61727975 6e2e636f 6d222c22 6e616d65 223a2022 5c753637 34655c75 38333032 222c2270 61737377 6f726422 3a202231 31313131 31222c22 70686f6e 65223a20 22313530 35313436 34363534 222c2270 696e6e65 64223a20 5b5d7d>
接着要去研究:allowLossyConversion是什么意思
搜:
swift dataUsingEncoding
swift dataUsingEncoding allowLossyConversion
参考:

Returns an NSData object containing a representation of the receiver encoded using a given encoding.

Declaration

SWIFT

func dataUsingEncoding(_ encodingUInt,
allowLossyConversion lossyBool) -> NSData?

OBJECTIVE-C

- (NSData *)dataUsingEncoding:(NSStringEncoding)encoding
         allowLossyConversion:(BOOL)flag

Parameters

encoding

A string encoding.

flag

If YES, then allows characters to be removed or altered in conversion.

Return Value

An NSData object containing a representation of the receiver encoded using encoding. Returns nil if flag is NO and the receiver can’t be converted without losing some information (such as accents or case).

Discussion

If flag is YES and the receiver can’t be converted without losing some information, some characters may be removed or altered in conversion. For example, in converting a character from NSUnicodeStringEncoding to NSASCIIStringEncoding, the character ‘Á’ becomes ‘A’, losing the accent.

This method creates an external representation (with a byte order marker, if necessary, to indicate endianness) to ensure that the resulting NSData object can be written out to a file safely. The result of this method, when lossless conversion is made, is the default “plain text” format for encoding and is the recommended way to save or transmit a string object.
->明白了:
allowLossyConversion
是否 允许 有损耗的 转换
true-》允许有损耗的转换-》如果期间有部分内容无法转换,则尝试损失部分信息再去转换,比如:NSUnicodeStringEncoding 转到NSASCIIStringEncoding
则‘Á’ 就可以被 损失掉音标部分,而转换为 ‘A’
-》false表示:不允许有损耗的

转载请注明:在路上 » [已解决]swift中string转换为NSData失败:Cannot convert value of type String to type NSData in coercion

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.170 seconds, using 22.02MB memory