Custom pattern in Swift

Tram Ho

  • One of the outstanding features of Swift is that many of the original features are implemented by Swift instead of having to customize the hardware in complier. In theory, it is very convenient and gives us flexibility in customizing the way that works effectively for work.
  • In this article we will delve deeper into choosing and customizing appropriate patterns in Swift to see how we can build complete custom patterns and some interesting techniques we can use. after that.

1 / Simple example:

  • Pattern matching is the work of matching values ​​with predefined pattern usually to determine which branch of code will perform the job. For example, every time we switch based on the available values:

  • Above we used the enum Section to identify two previously created pattern ( featured and recent ), this is a common use for pattern matching in swift, it sometimes causes undesirable effects in when the code.
  • To make it clear, we have defined the struct Pattern , we will use it to define our closure . This Closure will take the appropriate value and return the Bool result:

  • struct above struct may seem simple, but it completely allows us to define custom pattern arrangements by using generic type constaints to add static factory methods to create our pattern .

  • All pattern matching in Swift is very powerful, with operator ~= used as the left argument and the right values.

2 / Mix and customize accordingly:

  • If we are working in an app as a social network, when using the LoggedInUser struct to track the date of the logged-in user as id, friendId:

  • Now we want to build a view controller to display any number of users as a list but we want to render different icons depending on the type of user. That decision is made by the switch statement :

3 / Pattern comparison:

  • Continue to expand the Pattern by adding functions. We will write extensions using the Comparable protocol, which includes two method :

  • The above code will be useful when we want to compare more or less valuable values. In this example we can test if the user exceeds the game threshold by using the switch :

4 / Covert key path in the patterns:

  • Another use for creating extremely useful patterns is to use the key-path :

  • With the above code we can blend key-path with pattern types that allow us to solve complex logic code just by using switch statement .
  • Here we have decided how to parse a line of text in a list dependent on the first character by using the Character type to create a key-path pattern, combined with patterns that match the enum Kind

  • For the above code to work smoothly, define another operator == will return a Pattern combining KeyPath and a constant as follows:

  • Now that we have combined the key-path with the value generated from the pattern we can simply implement the way we calculate the level of the delivery value with the Destination as follows:

Share the news now

Source : Viblo