Which one is a faster way?

Im wondering if there is a speed difference between the two. Im looking to make the most streamline code, thats all

mikesClosure = { [weak self] in
guard let object = self else {
return
}
print(“(object.name)”)
}

and

mikesClosure = { [unowned self] in
print(“(self.name)”)
}
}

Hi @lac_ekim, looking at your code I would probably guess the second statement would be slightly faster. The first example has a guard statement that simply checks for a condition but is also an extra line of code compared to the second example. Furthermore, “unowned self” will allow for immutability and the reference will never be nil so runtime doesn’t have to keep track of the reference, which may provide a slight performance advantage.

Best,
Gina

1 Like

thats what I thought but in the swift lesson this guy was raving over the guard to other statements. Thank you for your help.

This topic was automatically closed after 166 days. New replies are no longer allowed.