SachinR90
(Sachin Rao)
1
Hello,
I had a question regarding Extensions. I am not able to understand why the following scenario is getting compiled successfully
for example consider:
An extension to String in file in Project
extension String {
func localize()->String{
let localString = "Project String"
return localString
}
}
Same extension in a cocoaPod
extension String {
func localize()->String{
let podString = "Pod String"
return podString
}
}
When I run the project the extension method of String in pod is ignored, and the one from project is getting called. What I believe is that, compiler should throw, Invalid redeclaration of 'localize()'.
code tested in xCode 11.3, Swift 4.2
eskimo
(Quinn “The Eskimo!”)
2
What I believe is that, compiler should throw, Invalid redeclaration
of localize().
This doesn’t happen because your library and your main app are separate modules, and most things within Swift are scoped within their module. You’ll see exactly the same behaviour if you declare two functions with the same name, or two classes, or whatever.
Share and Enjoy
Quinn “The Eskimo!” @ DTS @ Apple
1 Like