首页 > 解决方案 > String.removingPercentEncoding 在特别大的字符串上返回 nil

问题描述

我正在构建一个显示靠近用户的事件的应用程序。其中一些事件是从网络上抓取的,还有一些是从应用程序的用户那里发布到我的后端的。用户已发布到后端的内容会返回一个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适用于通常长度的字符串,但返回nil非常大的字符串(3,000 多个字符)?

标签: iosswiftstringencodingurlencode

解决方案


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

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

...to a 20% cancellation...

字符串的长度无关紧要。

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

您可以轻松检查它:

let str = "20%"

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

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


推荐阅读