ios - String.removingPercentEncoding 在特别大的字符串上返回 nil

标签 ios swift string encoding urlencode

我正在构建一个应用程序,显示靠近用户的事件。其中一些事件是从网络上抓取的,有些是从应用程序的用户发布到我的后端的。用户发布到后端的内容会返回一个 description 字符串属性,该属性是 URL 百分比编码的(这与在我的 SQL 数据库中存储表情符号有关,这是一个完全不同的故事) .

当从我的 API 中提取事件时,某些事件具有百分比编码,而其他事件则不会。通常,当对未编码的字符串使用 String.removingPercentEncoding 时,字符串中不会发生任何变化。

示例:

let example = "This is an example text"
let decodedExample = example.removingPercentEncoding!
print(decodedExample)

上面的代码返回This is an example text

现在,对于没有具有百分比编码的特别长的事件描述字符串,我们使用相同的方法并期望它只会返回相同的字符串。然而,我发现,对这些非常长的字符串之一调用 .removingPercentEncoding 实际上返回 nil。以下示例中的字符串有 3,123 个字符。

示例:

print("Encoded event description: ", event.description!)
print("Decoded event description: ", self.event.description.removingPercentEncoding ?? "Decoding failed")

上面的代码返回:

Encoded event description:  The Experience:\nThe Pavel Barber Hockey School is coming to St. Louis May 17th - 19th, 2019! Join head instructor Pavel Barber, a world renown stickhandling and shootout specialist, and friends for an experience that will challenge these young athletes to get better and encourage mental and physical skill development in a fun and creative environment. \nThe Features:\n\n\n4 Hours On-Ice Skill Development: 1 Hour Friday, 2 Hours Saturday, 1 Hour Sunday\n\n\n4 Hours Off-Ice Skill Development w/ Our Floorball+ HKY Training Program: 1 Hour Friday, 2 Hours Saturday, 1 Hour Sunday\n\n\n1 Personal Growth Group Session Saturday and Sunday\n\n\nScrimmage Game on Sunday\n\n\nPavel Barber Jersey and Socks Included\n\n\nLunch Included Saturday and Sunday\n\n\n \nThe Schedule:Friday, May 17th, 2019 - Day 1 - 5:30 pm - 8 pmSaturday, May 18th, 2019 - Day 2 - 7:30 am - 2 pmSunday, May 19th, 2019 - Day 3 - 7:30 am - 12:30 pm*Times and location subject to change. \n \nOther School Info:\n\n\nSpace is limited and will be filled on a first come, first serve basis\n\n\nAge groups include 7-10 year olds, 11-12 year olds, 13-15 year olds\n\n\nSkill Level Requirements - Kids should be proficient in the basics of hockey before attending the school\n\n\nRefund Policy: Refunds will only be allowed 30 days prior to the start of the school. All refunds are subject to a 20% cancellation fee.\n\n\nThis is not an overnight camp. All kids will need to be picked up and dropped off daily.\n\n\nPlease send your child with the following items each day of the school: workout clothes (such as shorts, t-shirt and gym shoes) for the off-ice sessions and sunscreen as there will be some outdoor activity. Water will be available for the players for all on-ice and off-ice training sessions.\n\n\nPavel Barber and Floorball Merch will be available at the school as well, but we recommend adding to your order now to ensure sizing and product availability\n\n\n \nAbout Pavel Barber:Pavel Barber has quickly emerged as one of the most sought after stick handling and skill development insturctors in the world. He is an internet hockey legend for his incredible hands, creative shootout moves and dangles and his skill development work. His YouTube channel has over 23 MILLION views on it and growing. Barber specializes in stick handling, shoout moves and creative skill development for both hockey and floorball. In fact, he is obsessed with it. He has studied talent generation across all fields and enjoys passing on that knowledge and training hockey players of all ages and skill levels.\nIn 2016, Barber was selected to the Team Canada National Floorball team that competed in Riga at the World Floorball Championships. \nBarber is a GoPro sponsored athlete and has played Indoor Hockey and Field Hockey for Team Canada between 2010 and 2015.\nOriginally from Toronto, Barber currently resides in Vancouver (or anywhere he can have a stick and puck/ball in his hands).\n\nPrice: $350 – $450\n\nFor more info, click here: https://www.eventbrite.com/e/pavel-barber-hockey-school-st-louis-registration-46948450078?aff=ebdssbdestsearch

Decoded event description:  Decoding failed

知道为什么 String.removingPercentEncoding 适用于通常很长的字符串,但在非常大的字符串(3,000+ 个字符)上返回 nil 吗?

最佳答案

我已经尝试了数百万次长度远超过 3,000 的随机字符串,但无法重现相同的问题。

您的 event.description! 有一个非转义的百分号,这就是 removingPercentEncoding 失败的原因。

...to a 20% cancellation...

字符串的长度无关紧要。

通常,当原始字符串包含非转义百分比符号时,removingPercentEncoding 会失败。

您可以轻松检查:

let str = "20%"

print(str.removingPercentEncoding ?? "*fail*") //->*fail*

如果原始字符串可能包含百分号符号,始终应用removingPercentEncoding并不是一个好的策略。

关于ios - String.removingPercentEncoding 在特别大的字符串上返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56195888/

相关文章:

ios - 实例化后立即访问解析对象 ID

java - 如何从字符串数组中删除索引?

ios - Avspeechsynthesizer 语音音频文件,UILocalNotification

objective-c - 为什么我的原型(prototype)单元格显示为空白?

objective-c - 使用 JSON (iOS) 的 Cocoa 错误 3840

ios - UINavigationBar 变黑

ios - 如何在具有两个 Storyboard iOS Swift 的项目中初始化 View Controller

ios - 如何在 Swift 中编辑函数输入?

android - Android中能否从strings.xml以外的资源文件中加载字符串资源?

java - 如何检查 String 是否包含 Java 中的右组括号