what is the usage of "~>" in cocoapods

20,052

Solution 1

~> (the optimistic operator) is used when you want to specify a version 'up to next major | minor | patch'. For example:

~> 0.1.2 will get you a version up to 0.2 (but not including 0.2 and higher)

~> 0.1 will get you a version up to 1.0 (but not including 1.0 and higher)

~> 0 will get you a version of 0 and higher (same as if it was omitted)

where 0.1.2 would mean 'I want this exact version'

Here is some more info on this

Solution 2

  • ~> 6.0 will get you the latest version before the next version which is 7.0 but not including the 7.0 version.

  • ~> 6.0.0 will get you the latest version before the next version which is 6.1.0 but not including the 6.1.0 version.

🎉

Solution 3

optimistic operator ~>:

'~> 0.1.2' Version 0.1.2 and the versions up to 0.2, not including 0.2 and higher
'~> 0.1' Version 0.1 and the versions up to 1.0, not including 1.0 and higher
'~> 0' Version 0 and higher, this is basically the same as not having it.
For more information, regarding versioning policy, [see][1]:

Solution 4

While most of the above answers are correct, they answer the question using examples rather than actually explaining what is going on, making it difficult to understand the concept. The key thing to know in order to understand how the optimistic operator works is that cocoapods looks to see how specific the version number is to determine how it will interpret the optimistic operator.

A version number's syntax conveys info re. the type of update a developer is releasing. That syntax, from left to right, refers to Major.Minor.Patch updates.

Cocoapods looks to see what is the most specific element in the version number to determine how it will interpret the optimistic operator. So if the version number contains info about patch updates - i.e it looks something like this: ~> 1.1.2 - then patch updates are what cocoapods focuses on when implementing the optimistic operator. Likewise, if the version number is only as specific as minor updates - i.e it looks something like this: ~> 1.2 - then cocoapods will focus only on updates that are either minor OR patch updates when implementing the optimistic operator.

A version number of ~> 1.0.1 tells cocoapods to install the most recent version update, so long as that update is a PATCH update; Major or Minor updates should be ignored.

A version number of ~> 1.1 tells Cocoapods to install the most recent updates that are available, so long as those updates are either PATCH or MINOR updates, and it should ignore any Major updates.

Share:
20,052

Related videos on Youtube

Mil0R3
Author by

Mil0R3

Updated on July 05, 2022

Comments

  • Mil0R3
    Mil0R3 almost 2 years

    I want to know what the "~>" is used for,cause I find they are the same below:

    pod 'AFNetworking','~> 2.0.3'
    
    pod 'AFNetworking','2.0.3'
    
    • mfaani
      mfaani over 6 years
      You would basically do this so your app won't break with any higher versions (possibly incompatible pods). Still it can apply/benefit from the 'minor' updates.
    • MiTal Khandhar
      MiTal Khandhar over 6 years
      If you are specifying version using ~> suppose in ur pod file i.e. pod 'AFNetworking','~> 2.0.3'then it will install if there is new version of 2.0.x and up to 3.0 , where as if you are specifying with 'AFNetworking' , ' 2.0.3' it will Install that version only.
  • Jakub Truhlář
    Jakub Truhlář over 7 years
    I'm curious about one thing, If I specify lets say: '~> 7.6' I should get every version from 7.6.0 to 8.0 (not included). But when run real pod install, I get 7.6 even when the 7.8 version exists. You said it also means "I want this exact version", what is the point then. How to get the lates one fitting the ~> rule?
  • Alladinian
    Alladinian over 7 years
    @JakubTruhlář For the exact version I was referring to a specifier without ~>. Also note that cocoapods will resolve the dependencies based on several other factors like target sdk, version compatibility with other pods, swift version etc.
  • Jakub Truhlář
    Jakub Truhlář over 7 years
    One thing comes to my mind. Since Cocoapods 1.0, source repo is not updated within pod install. So if version 7.8 from the example above is released after my last source repo update, I will get the last one source repo knows (e.g. 7.6)
  • user1105951
    user1105951 about 7 years
    @JakubTruhlář, this is from CocoaPods site : Use pod install to install new pods in your project. Even if you already have a Podfile and ran pod install before; so even if you are just adding/removing pods to a project already using CocoaPods. Use pod update [PODNAME] only when you want to update pods to a newer version.
  • Antek
    Antek about 6 years
    the link is broken, although there is a link to the current documentation
  • Adnan Aftab
    Adnan Aftab about 6 years
    Instead of downvoting you could have update the link :) Some constructivne work.
  • Antek
    Antek about 6 years
    you're right, even though it's not me downvoting it! :) Sorry, should've done so. (i mean i should've edited, not downvoted ;) )
  • KarmaDeli
    KarmaDeli about 5 years
    Please help me understand... ~> 0.1.2 will get you a version up to 0.2 -- I'm confused because 0.2 seems to be way lower then the optimism version semantics seems to be asking for. As in, if you want for version 0.1.2, which in my understanding would be the 12th iteration, (0.0.1, 0.0.2, 0.0.3 and so on) why would you ask for version 0.0.2 (which in my mind is the 2nd iteration) ??
  • Alladinian
    Alladinian about 5 years
    @KarmaDeli 0.2 as in 0.2.0 not 0.0.2
  • Jonathan.
    Jonathan. over 4 years
    This is not useful starting from 0, it would be more helpful to know how it works with like ~> 1.0.0, ~> 1.0 and ~> 1