Updated 12 August 2022
We will be learning how we can use ‘some’ and ‘any’ in Swift.
‘some’ keyword was introduced in 5.1 however, the ‘any’ keyword was introduced in Swift 5.6
In Swift 5.7 we can use these two keywords in the function’s parameter position.
So let’s discuss in detail regarding these keywords.
Here is an example code snippet of how we can use the ‘some’ and ‘any’ keywords with functions.
1 2 3 4 5 6 7 |
func exampleFunctionOne(with abc: <strong>some</strong> AnyProtocolName) { } // declared a function with parameter using 'some' keyword func exampleFunctionOne(with abc: any AnyProtocolName) { }// declared a function with parameter using 'any' keyword |
Above all, let’s define a protocol as mentioned in the above code snippet.
1 2 3 4 5 |
protocol AnyProtocolName { associatedtype AnyType var xyz: String { get } func protocolfunction(with abc: AnyType) } |
Now we will define the struct that uses the AnyProtocolName protocol as below.
Please be noted that the data types of the anyStructOne and anyStructTwo are different, which are anyStructThree & anyStructFour
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
struct anyStructOne: AnyProtocolName { let name = "abc" func protocolfunction(with abc: anyStructThree) { } } struct anyStructTwo: AnyProtocolName { let name = "def" func protocolfunction(with abc: anyStructFour) { } } struct anyStructThree { let name = "Webkul" } struct anyStructFour { let name = "Mobikul" } |
We can use the ‘some’ keyword in the function’s parameter position which is the same as making the function generic. However, it is used together with a protocol to create an opaque type that represents something that is conformed to a specific protocol.
1 2 3 |
func abc(_ def: some AnyProtocolName) { }// use some keyword to make the function generic |
In addition, ‘some’ keyword can be used with variables as described below.
1 |
var myVariable: some AnyProtocolName = anyStructTwo() |
Here, we are telling the compiler that we are working on a specific concrete type.
So it will throw a compile time error when we change the type.
1 2 |
myVariable = anyStructOne() //Compile error: Cannot assign value of type 'anyStructOne' to type 'some AnyProtocolName' |
In Swift 5.6, the ‘any’ keyword is not mandatory when creating an existential type, but in Swift 5.7, you will get a compile error.
Here is an example of how we can use the ‘any’ keyword.
1 |
let myVar: any AnyProtocolName = anyStructTwo() |
Unlike the ‘some’ keyword we can use ‘any’ keyword for protocols with associated types.
1 2 3 4 5 6 7 8 9 10 11 |
var myVar: any AnyProtocolName = anyStructTwo() myVar = anyStructOne() func anyFunction(value: Bool) -> any AnyProtocolName { if value { return anyStructTwo() } else { return anyStructOne() } }//No compile time error when returning different kind of type. |
If you want to learn more please visit here
Please visit my other blogs here.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.