O'Reilly logo

Mastering Swift 4 - Fourth Edition by Jon Hoffman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Putting it all together

To reinforce what we have learned in this chapter, let's look at one more example. For this example, we will create a function that will test to see if a string value contains a valid IPv4 address. An IPv4 address is the address assigned to a computer that uses the Internet Protocol (IP) to communicate. An IP address consists of four numeric values ranging from 0-255, separated by a dot (period). An example of a valid IP address is 10.0.1.250:

func isValidIP(ipAddr: String?) ->Bool { guard let ipAddr = ipAddr else { return false } let octets = ipAddr.characters.split { $0 == "."}.map{String($0)} guard octets.count == 4 else { return false } for octet in octets { guard validOctet(octet: octet) else { return false } ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required