[Objective-C] How do I install and manage packages using package managers in Objective-C?

Objective-C does not have its own dedicated package manager like npm (Node.js) or pip (Python). However, you can use package managers like CocoaPods or Carthage to manage dependencies in Objective-C projects.

  1. CocoaPods:

    • Install CocoaPods by running the following command in Terminal:
      1$ sudo gem install cocoapods
      
    • Create a new Podfile in your project's root directory:
      1$ cd /path/to/your/project
      2$ pod init
      
    • Open the Podfile using a text editor and add your dependencies. For example, to add the Alamofire library, use:
      1target 'YourProjectName' do
      2  use_frameworks!
      3  pod 'Alamofire', '~> 5.4'
      4end
      
    • Save the Podfile and run the following command to install the dependencies:
      1$ pod install
      
    • From now on, always open your Xcode project using the .xcworkspace file created by CocoaPods.
  2. Carthage:

    • Install Carthage using Homebrew by running the following command in Terminal:
      1$ brew install carthage
      
    • Create a new Cartfile in your project's root directory:
      1$ cd /path/to/your/project
      2$ touch Cartfile
      
    • Open the Cartfile using a text editor and add your dependencies. For example, to add the Alamofire library, use:
      1github "Alamofire/Alamofire" ~> 5.4
      
    • Save the Cartfile and run the following command to fetch and build the dependencies:
      1$ carthage update
      
    • Drag the built frameworks from the Carthage/Build folder into your Xcode project.

Both CocoaPods and Carthage manage dependencies by creating workspace files that include your project and the external libraries. This ensures proper linking and compatibility with your Objective-C project.