Putting it all together

To reinforce what we 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 or not. An IPv4 address is the address assigned to a computer that uses the Internet Protocol 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 } func validOctet(octet: String) -> Bool { guard let num = Int(String(octet)) where ...

Get Swift: Developing iOS Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.