String.removingPercentEncoding returns nil 特别大的字符串
String.removingPercentEncoding returns nil on particularly large strings
我正在构建一个应用程序来显示靠近用户的事件。其中一些事件是从网络上抓取的,一些是应用程序用户发布到我的后端的。用户发布到后端的那些 return 使用 description
字符串 属性 进行了 URL 百分比编码(这与将表情符号存储在我的SQL 数据库,完全是另一回事)。
当从我的 API 中提取事件时,一些事件 将 有百分比编码,而其他 不会 。通常,在未编码的字符串上使用 String.removingPercentEncoding
时,字符串中的任何内容都不会发生变化。
示例:
let example = "This is an example text"
let decodedExample = example.removingPercentEncoding!
print(decodedExample)
上面的代码returns This is an example text
.
现在,对于 没有 百分比编码的特别长的事件描述字符串,我们使用相同的方法并期望它只是 return 相同的字符串。然而,我发现,在这些非常长的字符串之一上调用 .removingPercentEncoding
实际上是 returning nil
。以下示例中的字符串有 3,123 个字符。
示例:
print("Encoded event description: ", event.description!)
print("Decoded event description: ", self.event.description.removingPercentEncoding ?? "Decoding failed")
以上代码returns:
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: 0 – 0\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
适用于通常较长的字符串,而 returns nil
适用于非常大的字符串(3,000 多个字符)吗?
我已经尝试了数百万次长度超过 3,000 的随机字符串,但无法重现同样的问题。
您的 event.description!
有一个 non-escaped 百分号,这就是 removingPercentEncoding
失败的原因。
...to a 20% cancellation...
字符串的长度无关紧要。
通常,当原始字符串包含 non-escaped 个百分比符号时,removingPercentEncoding
会失败。
您可以轻松查看:
let str = "20%"
print(str.removingPercentEncoding ?? "*fail*") //->*fail*
如果原始字符串可能包含百分号,总是应用removingPercentEncoding
不是一个好的策略。
我正在构建一个应用程序来显示靠近用户的事件。其中一些事件是从网络上抓取的,一些是应用程序用户发布到我的后端的。用户发布到后端的那些 return 使用 description
字符串 属性 进行了 URL 百分比编码(这与将表情符号存储在我的SQL 数据库,完全是另一回事)。
当从我的 API 中提取事件时,一些事件 将 有百分比编码,而其他 不会 。通常,在未编码的字符串上使用 String.removingPercentEncoding
时,字符串中的任何内容都不会发生变化。
示例:
let example = "This is an example text"
let decodedExample = example.removingPercentEncoding!
print(decodedExample)
上面的代码returns This is an example text
.
现在,对于 没有 百分比编码的特别长的事件描述字符串,我们使用相同的方法并期望它只是 return 相同的字符串。然而,我发现,在这些非常长的字符串之一上调用 .removingPercentEncoding
实际上是 returning nil
。以下示例中的字符串有 3,123 个字符。
示例:
print("Encoded event description: ", event.description!)
print("Decoded event description: ", self.event.description.removingPercentEncoding ?? "Decoding failed")
以上代码returns:
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: 0 – 0\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
适用于通常较长的字符串,而 returns nil
适用于非常大的字符串(3,000 多个字符)吗?
我已经尝试了数百万次长度超过 3,000 的随机字符串,但无法重现同样的问题。
您的 event.description!
有一个 non-escaped 百分号,这就是 removingPercentEncoding
失败的原因。
...to a 20% cancellation...
字符串的长度无关紧要。
通常,当原始字符串包含 non-escaped 个百分比符号时,removingPercentEncoding
会失败。
您可以轻松查看:
let str = "20%"
print(str.removingPercentEncoding ?? "*fail*") //->*fail*
如果原始字符串可能包含百分号,总是应用removingPercentEncoding
不是一个好的策略。