95

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
  • 33,269
  • 19
  • 164
  • 293
Mil0R3
  • 3,876
  • 4
  • 33
  • 61
  • 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. – mfaani Jan 30 '18 at 21:33
  • 1
    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. – MiTal Khandhar Feb 07 '18 at 06:35

4 Answers4

130

~> (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

Alladinian
  • 34,483
  • 6
  • 89
  • 91
  • 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? – Jakub Truhlář Oct 15 '16 at 11:09
  • @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. – Alladinian Oct 15 '16 at 11:19
  • 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) – Jakub Truhlář Oct 15 '16 at 12:08
  • 1
    @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. – user1105951 Mar 23 '17 at 01:00
  • 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) ?? – KarmaDeli Mar 02 '19 at 05:17
  • @KarmaDeli `0.2` as in `0.2.0` _not_ `0.0.2` – Alladinian Mar 02 '19 at 13:28
12
  • ~> 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.

Atef
  • 2,872
  • 1
  • 36
  • 32
6

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]:
Adnan Aftab
  • 14,377
  • 4
  • 45
  • 54
  • the link is broken, although there is a link to the current documentation – Antek Mar 05 '18 at 15:12
  • 3
    Instead of downvoting you could have update the link :) Some constructivne work. – Adnan Aftab Mar 06 '18 at 11:31
  • 2
    you're right, even though it's not me downvoting it! :) Sorry, should've done so. (i mean i should've edited, not downvoted ;) ) – Antek Mar 06 '18 at 13:50
  • 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` – Jonathan. Sep 29 '19 at 09:45
6

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.

thecloud_of_unknowing
  • 1,103
  • 14
  • 23