what is the usage of "~>" in cocoapods
Asked Answered
P

4

97

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'
Paperhanger answered 26/11, 2013 at 9:58 Comment(2)
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.Dannydannye
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.Paletot
S
135

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

Shaduf answered 26/11, 2013 at 10:7 Comment(6)
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?Nazareth
@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.Shaduf
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)Nazareth
@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.Territorial
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) ??Danner
@Danner 0.2 as in 0.2.0 not 0.0.2Shaduf
D
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.

🎉

Duhon answered 2/1, 2019 at 10:46 Comment(0)
B
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]:
Basion answered 26/11, 2013 at 10:6 Comment(4)
the link is broken, although there is a link to the current documentationSchenck
Instead of downvoting you could have update the link :) Some constructivne work.Basion
you're right, even though it's not me downvoting it! :) Sorry, should've done so. (i mean i should've edited, not downvoted ;) )Schenck
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 ~> 1Devaluation
V
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.

Valente answered 12/11, 2019 at 15:54 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.