String.removingPercentEncoding对特别大的字符串返回nil

时间:2019-05-18 04:10:04

标签: 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。下例中的字符串包含3123个字符。

示例:

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吗?

1 个答案:

答案 0 :(得分:1)

我尝试使用长度超过3,000的随机String进行了数百万次的尝试,并且无法重现相同的问题。

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

...to a 20% cancellation...

字符串的长度无关紧要。

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

您可以轻松检查它:

let str = "20%"

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

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