To validate if a string is a valid UTF-8 encoded string in Swift, we can make use of the String
API's utf8
view and iterate over it to check if each byte of the string is valid.
Here is an implementation of a function that takes a string and returns a boolean value indicating if it is a valid UTF-8 encoded string:
main.swift1799 chars48 lines
The function checks if each byte of the string belongs to a valid UTF-8 character or not. If all the bytes in the string are valid UTF-8 characters, the function returns true
, otherwise, it returns false
.
gistlibby LogSnag