In this story, we will learn how to use UITextField “One Time Code” mechanism.

Photo by NeONBRAND on Unsplash

Many times we have seen applications to require an SMS security code fill, before continue with a specific feature. In this story we will see how we can use UITextField’s “One Time Code” Content Type, in order to autofill an SMS security code.

Start a new Project and add a UITextField in the Storyboard.

In this story we will see how we can Mock a Network Call so we can write Tests.

Photo by Nina Ž. on Unsplash

Writing Tests in a project is something your team must do.
Tests will help you catch bugs early and save development and QA time.

When we make a network call in our Swift project, we do not want to test the real Server communication. Tests must not depend on external faulty resources. If the network go down if the middle of a test, you will get a failure and this failure does not mean there is something wrong with your code.

In order to avoid this we will have to Mock the network call, and provide the network result with…

In this story we will see how we can change a method’s implementation in RunTime.

Photo by Shahadat Shemul on Unsplash

Objective-C runtime is powerful enough to give us the tools to change a method’s implementation in runtime through Method swizzling.

Using Method swizzling we change the implementation of an existing selector at runtime.

Let’s see how we can use it in Swift. Our class must inherit from NSObject in order to use Objective-C runtime.

class Object: NSObject {}

Our object Class will have a method called printResult. printResult method must be dynamic because we will change it at runtime.
For now when we call it will simply prints “Original Result”

class Object: NSObject { @objc dynamic func printResult() {…

Use Boxing technique to share the Instance of a Struct

Photo by Erda Estremera on Unsplash

As we know Structs in Swift are Value Type Objects.
Meaning when you assign or pass a Struct object to another property or argument it creates a copy of the original one.

Let’s test it out:

User {
var name: String
user1 = User(name: "Mark")
var user2 = user1
//3 = "John"
print("user1 name: \(")
print("user2 name: \(")

We create a Struct named User (1).

We define a property user1 as User with name Mark.
Then we define a property user2 equal to user1.

We change user2 name to John (3)

The print…

In this story we will see how we can initialize a Class from it’s Type

Photo by Clément H on Unsplash

Let’s say we have a Class:

class MyClass {}

And we want to create an Instance from it’s Type!

Let’s create a helper generic function:

func initializeObject<T>(fromType type: T.Type) -> T {    return ???}

This generic function take as an argument an Instance Type and return an Instance.
We can call it like this:

var object1 = initializeObject(fromType: MyClass.self)

But what should this function return?

func initializeObject<T>(fromType type: T.Type) -> T {    //1
return T.init() //error

We cant use init() because we have not set it as required in our Class implementation. …

Photo by Taras Shypka on Unsplash

Imagine that you have an array like this:

let array: [Any] = [nick, john, nick, “String”, 3]

and you want to remove all elements of a specific Type! What will we do?

Let’s create an example…

class Nick {}struct John {}let nick = Nick()let john = John()let array: [Any] = [nick, john, nick, "String", 3, "AnotherString"]print(array)

In this example we have a Class Nick and a Struct John.

Finally our array contains 2 Nick objects, 1 John, 2 Strings and 1 Integer.

The print result is:

[__lldb_expr_12.Nick, __lldb_expr_12.John(), __lldb_expr_12.Nick, "String", 3, "AnotherString"]

Let’s say we…

Valsamis Elmaliotis

iOS Engineer @ Advantage FSE

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store