If you run the following function in vs code, you will get a compilation error that the addingPercentEncoding function does not exist, but if you import the cocoa framework, it will run normally. What’s the difference between the two?
That method – .addingPercentageEncoding is part of the NSString bridging, which adds the behavior of NSString to the swift String implementation. This functionality is introduced by importing Foundation. The import of Cocoa also imports Foundation.
The .urlQueryAllowed is a part of NSCharacterSet, which gets imported into the swift class CharacterSet by bridging also.
The bridging here introduces a lot of functionality that doesn’t need to be reimplemented in the base class.
The addingPercentEncoding(withAllowedCharacters:) function is a part of the Foundation framework in Swift, which provides a set of common data types and APIs for working with strings, dates, URLs, and more. The Cocoa framework, on the other hand, is a higher-level framework that builds on top of Foundation, and provides additional functionality for developing macOS and iOS applications.
When you import Cocoa, you are also importing Foundation, because Cocoa depends on Foundation. This is why you are able to use the addingPercentEncoding(withAllowedCharacters:) function without any compilation errors.
2
Answers
That method –
.addingPercentageEncoding
is part of theNSString
bridging, which adds the behavior ofNSString
to the swift String implementation. This functionality is introduced by importingFoundation
. The import ofCocoa
also importsFoundation
.The
.urlQueryAllowed
is a part ofNSCharacterSet
, which gets imported into the swift classCharacterSet
by bridging also.The bridging here introduces a lot of functionality that doesn’t need to be reimplemented in the base class.
The addingPercentEncoding(withAllowedCharacters:) function is a part of the Foundation framework in Swift, which provides a set of common data types and APIs for working with strings, dates, URLs, and more. The Cocoa framework, on the other hand, is a higher-level framework that builds on top of Foundation, and provides additional functionality for developing macOS and iOS applications.
When you import Cocoa, you are also importing Foundation, because Cocoa depends on Foundation. This is why you are able to use the addingPercentEncoding(withAllowedCharacters:) function without any compilation errors.