skip to Main Content

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?

enter image description here

enter image description here

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search