Alamofire: [WIP] Swift 3.0 Migration

Created on 17 Jun 2016  Â·  85Comments  Â·  Source: Alamofire/Alamofire

Alamofire - Swift 3.0

The initial migration to Swift 3.0 has been completed, and can be found in the swift3 branch. Many thanks to the Apple Engineers in the labs at WWDC who assisted with some of the migration issues. :beers:

Please file Github issues for any problems discovered while using this branch.

This branch should be considered a work in progress, and will be updated as future Xcode 8 and Swift 3 seeds are released by Apple.

Official support for Swift 3.0 and Xcode 8 will be not available until the fall when Apple releases both.

Migration Notes

  • Many Foundation types are now value types, instead of reference types. Please audit your code to ensure all references to NSMutableURLRequest are now properly handled as var and are of type URLRequest.
  • Due to the Swift 3.0 naming conventions, many Alamofire API parameters that referenced an object in the URL Foundation loading system and began with capital letters now begin with lowercase letters. For example, URLRequest parameter names are now urlRequest to prevent confusion.

    More notes will be added as issues are discovered.

    Radars

The following radars were filed as a result of this migration and are currently being tracked by the ASF team.

  • 26850776: CharacterSet crashes when attempting to remove an item from the set in Swift 3.0
  • 26849668: NSURLProtocol APIs still contain NSMutableURLRequest parameter types and do not support the Swift 3.0 value types
  • 26870455: NSURLSession with background configuration does not function on the iOS simulator inside of test targets

TODO

  • [x] Update Project Recommended Settings
  • [x] Update Example
  • [x] Travis CI Support
  • [ ] Update Documentation/README
swift

Most helpful comment

install compatibility with cocoapods? @kcharwood

EDIT: use

  pod 'Alamofire',
  :git => 'https://github.com/Alamofire/Alamofire.git',
  :branch => 'swift3'

All 85 comments

install compatibility with cocoapods? @kcharwood

EDIT: use

  pod 'Alamofire',
  :git => 'https://github.com/Alamofire/Alamofire.git',
  :branch => 'swift3'

Using the provided code on the Alamofire page, when creating a generic ResponseObjectSerializable, I get the following error: Member 'failure' in 'Result<T, NSError>' produces result of type 'Result<Value, Error>', but context expects 'Result<T, NSError>'. This occurs on the following line: return .failure(.ObjectSerialization(reason: "JSON could not be serialized into response object")). I am using the swift 3.0 branch.

Could it be installed via carthage? As I'm getting compile errors

I am able to install using carthage, have you set the correct version of XCode in Terminal?

sudo xcode-select -s /Applications/Xcode-beta.app/Contents/Developer/

Indeed. Forgot to switch XCode version. All works fine.
Cartfile:
github "Alamofire/Alamofire" "swift3"

I am stuck with "error: Found an unexpected Mach-O header code: 0x72613c21" because of the pod, ideas will be highly appreciated.

@fancytapsio That is a CocoaPods issue, try https://github.com/CocoaPods/CocoaPods/issues/5598#issuecomment-230662508

@ronaldmannak Thank you, I needed that, it finally worked!

I have installed Cocoa pods in xcode 8.1. I add like this:
pod 'Alamofire',
:git => 'https://github.com/Alamofire/Alamofire.git',
:branch => 'swift3'
when I complie my app, Its getting so many error.

Branch has been updated for beta 3.

@dipang90 You likely need to update your version of Xcode 8 to the latest beta.

Xcode Beta3, using pod and getting ...
“Use Legacy Swift Language Version” (SWIFT_VERSION) is required to be configured correctly for targets which use Swift. Use the [Edit > Convert > To Current Swift Syntax…] menu to choose a Swift version or use the Build Settings editor to configure the build setting directly.

Can you explain how to use this please? I created a brand new project and used the Pod configuration that @dipang90 showed. When Xcode starts it immediately wants to do a conversion to Swift 3, and I get 346 issues in Alamofire framework.

I'm using Xcode 8 Beta 3

Make sure you do a pod update to pull the latest code. Then, if your app has already been converted to Swift 3, you can skip any conversion prompt that comes up. You'll also want to be using the CocoaPods 1.1.0 beta that was released last week, as it knows how to more properly deal with generating Xcode 8 compatible projects.

@jshier pod --version says 1.0.0. I did the pod install followed by a pod update, then opened my project. I clicked 'Later' to the conversion and when I build I get the error that @vladgon shows. Then I went ahead and ran the 'convert' and get the 348 errors in the Alamofire library.

It seems to compile after running

sudo gem install cocoapods --pre
pod update

Thanks, @jshier and @vladgon. I did the gem install to get the BETA version and now it's working properly.

Though I will point out, if you still go ahead and do a convert it'll then start failing again, so just make sure you never convert the alamofire pod :)

Worked for me after installing the --pre version of cocoapods.

Ouch, Xcode 8 beta4 is out and breaks the swift3 branch :/

@Grmiade yes, I was trying today to update and after losing some hours I decided to keep with Swift 2.2 and xc 7.3 until everything is ok.. =/

It seems like the current version is based on Beta 4. When I compile today, there are a fews errors.(such as.. NSError is migrating to Error.)

Hi @vladgon
I installed the --pre version, but it didn't work.
Can you tell me which is the right order?
My order: pod init -> modify the podfile -> pod install -> pod update ?
I use Xcode beta 3, cocoapods-1.1.0.beta.1.

@KhoaVanNguyen You need to say something more than "It didn't work". What, exactly, is happening for you? Using the same steps as you I was able to use this version successfully. However, note my statement from above that you can NOT run the converter on the Alamofire framework or it will then indeed break things.

@grosch
I’m sorry. I just think I get stuck on that step, so I didn’t write the details.
Here what I did, can you figure out what I did wrong?
I install cocoapods. Then install —pre version by:
sudo gem install cocoapods --pre
then
I create a xcode project without a Git repo. I modify the Podfile
Here are my Podfile

# Uncomment this line to define a global platform for your project
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!
target 'Test' do
pod 'Alamofire', 
  :git => 'https://github.com/Alamofire/Alamofire.git', 
  :branch => 'swift3' 
end’

I install by:
pod install
Then update by:
pod update
I also try:

pod init -> modify the podfile -> sudo gem install cocoapods --pre -> pod install -> pod update

Then I open .xcworkspace and get this error:
my error
Of course, I try many scenarios like uninstall cocoapods and repeat the process. Sometimes, Xcode appears a pop-up to request converting to Swift 3 but I cancel it.

Are you sure you're running the beta 4 version of Xcode? When you go to Xcode -> About does it say Version 8.0 beta 4?

No, I run beta 3

Well there you go :) Alamofire has been updated for Beta 4 already. If you're using the Beta Xcode versions, you pretty much have to upgrade every other Monday when they release a new version.

Thank you. I don't know about this. It means there will be a Xcode beta 5 next Monday. And I have to update to latest version of Xcode beta to install Alamofire with Swift 3, right?

Well, "maybe". I think Apple has been releasing a new Xcode beta every other Monday. The Alamofire guys have been keeping up with the upgrades. So yes you should always upgrade to the new Beta when they come out for your NON-PRODUCTION work, but that doesn't mean that the Alamofire developers are going to immediately upgrade this branch to work with it.

create swift4.0 branch in pod file? In using Xcode 10 beta 4

Swift 3 isn't even done and you want them to start a Swift 4? Wow

I mean when Xcode update with beta version then swift 3.0 is ok or we have create new branch 4.0 ?

You should only be using this specific branch of Alamofire for your testing with the BETA version of Xcode. When Xcode 8 goes production, then they'll migrate this branch into the master branch of Alamofire. Once the Swift team starts doing things with Swift 4, the Alamofire people will eventually create a branch for that, I'm sure. But that's a long time away I would think.

I've noticed that when using the swift3 branch with Carthage I cannot use target 8.4 anymore. I must select deployment target 9.0+. Is that a bug/am I doing anything wrong? As far as I know, Alamofire was iOS 8+ compatible...

I've noticed that Apple just released Xcode beta5.

Hi, thanks for your job. There is a problem of compatibility with iOS 8 and the used of URLSessionStreamTask (iOS 9.0+) in SessionManager and SessionDelegate.

screen shot 2016-08-11 at 08 44 44

Thanks

The Swift 2.3 branch will most likely be released as Alamofire 3.5.0 and will support iOS 8 - iOS 10 on both Swift 2.2 and Swift 2.3. Alamofire 4.0.0 will only support iOS 9+. Please see #1367 for a more in-depth explanation.

@kapellanos @cnoon I'm getting similar errors, even though my app is iOS 9-only. I think this is because the Podfile still indicates iOS 8 support, which means CocoaPods sets the deployment version to 8.0. It looks like #1401 should fix it, though that has a couple other changes as well.

1401 has been landed and the swift3 branch should be fully compatible with Xcode 8 beta 5. Please note, however, that the swift3 branch has become our Alamofire 4 development branch, so you'll likely see some breaking changes as we develop towards the AF4 release.

I'm getting errors now about requiring a higher deployment target. I'm targeting iOS 9 and OS X 10.10 in my Podfile. Are you removing support for those? That's going to hurt :(

Not for iOS 9, but for 10.10, yes. Latest 2 OS version, as Apple recommends.

@jshier Was afraid you'd say that. I have a client who is stuck on 10.10 for at least another year as the primary 3rd party software they use from a major vendor isn't upgrading, and they aren't big enough to force the issue. Suggestions on how I handle something like that? I have to keep his project at 10.10, and I depend on Alamofire, but I also need to upgrade to Xcode 8 when it releases for everyone else.

Continue using Swift 2.3, as Alamofire 3 will continue supporting the current versions, just with no new features.

Branch has been updated for beta 6?

Not yet, the changes in beta 6 are extensive and will require a bit of work to get right. I'll see if I can get the changes up tonight or tomorrow.

@jshier Any updates regarding beta 6? Let me know if you need any help.

I stuck with beta 6

I did all

in my main directory
sudo gem install cocoapods --pre
than move to my project directory
pod init
edit my podfile

# Uncomment this line to define a global platform for your project source 'https://github.com/CocoaPods/Specs.git' platform :ios, '9.0' use_frameworks! target 'MyAlamo' do pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :branch => 'swift3' end

than

pod install

and after install i
pod update

snimek obrazovky 2016-08-16 v 12 09 04

snimek obrazovky 2016-08-16 v 12 05 02

@RobinKment : As was said two replies before yours, beta 6 introduced many changes for Swift 3 which affected Alamofire and therefore there's nothing wrong with your install or Podfile, you just have to wait until the swift3 branch is updated for these changes.

@thebluepotato yes i red that but i didn't know if to delete my post or not probably good was to delete it. Sorry

@jshier Any updates on this?

@danstepanov As you can see in the commits, the Swift 3 branch has been updated for beta 6. Happy testing!

The swift3 branch has been updated for beta 6. Be aware there have some refactors, as Any is now favored over AnyObject.

Using beta 6, I am having problems with cocoapods linking with AlamofireNetworkActivityIndicator. If I remove AlamofireNetworkActivityIndicator pod everything works.

This is the error:

- `Alamofire (from `https://github.com/Alamofire/Alamofire.git`, branch `swift3`)` required by `Podfile`
- `Alamofire (~> 3.3)` required by `AlamofireNetworkActivityIndicator (1.0.1)`

We haven’t updated AFNAI to beta 6 yet @jmbeneUF.

@jmbeneUF AFNAI has been updated for beta 6. Make sure you integrate it using the swift3 branch.

I am receiving multiple compiler errors. I am using the swift 3 branch.
screen shot 2016-08-18 at 11 11 01 pm

Make sure you've updated your checkout of the swift3 branch and are using beta 6.

I am unable to build a project using CocoaPods. I'm using Xcode 8 beta 6 and my Podfile has:
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :branch => 'swift3'

First there is a warning about required architectures 'i386 x86_64', so I set Build Active Architectures Only to "No".

Then there's a warning about Use Legacy Swift Language Version so I set that to "No".

I'm left with an error on import Alamofire in one of my Swift files that states "Module file was created by an older version of the compiler; rebuild 'Alamofire' and try again".

UPDATE:
I was not using CocoaPods beta. Thanks @jshier !

Ensure you are using the latest CocoaPods beta (1.1.0.beta.1) and clear your derived data.

Got so many errors while building :
screen

This is my podfile :

platform :osx, "10.12"
use_frameworks!

target 'MyApp' do
pod 'SwiftyJSON', :git => 'https://github.com/SwiftyJSON/SwiftyJSON.git', :branch => 'swift3'
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :branch => 'swift3'
end

I am using Cocoapods beta and Xcode Version 8.0 beta (8S128d).
MacOS version : bĂŞta 10.12 (16A294a)

Thanks

@Lawris you need to use beta 6 in order to build the swift3 branch right now.

@cnoon Excellent, it works now ! Thanks !

Hey guys,
I am using latest pre release beta of CocoaPods, and Xcode Version 8.0 beta 6 (8S201h)

My pod file:

Uncomment this line to define a global platform for your project

platform :ios, '9.0'

target 'Sharqona Afif' do
# Comment this line if you're not using Swift and don't want to use dynamic frameworks
use_frameworks!
pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :branch => 'swift3'
end

screen shot 2016-08-25 at 15 14 00

Still Xcode forces me to convert
Any Suggestions ?

@Turgunov try add code below to your Podfile, then pod install

post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            config.build_settings['SWIFT_VERSION'] = '3.0'
        end
    end
end

Instead of doing that, which you shouldn't need to, ensure you're using the latest beta and do a pod update so that it regenerates your project integration. If that still doesn't work you can do pod deintegrate and then pod update to redo the integration.

So, I am able to build my project in Swift 3 Xcode beta 6 and sudo gem install cocoapods --pre, using this branch in my podfile.

pod 'Alamofire', :git => 'https://github.com/Alamofire/Alamofire.git', :branch => 'swift3'

All good.

But I cannot use Alamofire as an object, for instance in Xcode.

Alamofire.request(.GET, "https://httpbin.org/get") throws a Swift compiler error:

Ambiguous reference to member 'request (_:withMethod:Parameters:encoding:headers:)'

@davidoliverdoswell If you look at the "ambiguous" member, or try to write Alamofire.requ... and look at Xcode's suggestions you'll see what's changed : the URL is now the first parameter and the method the second one. Therefore it's Alamofire.request("https://httpbin.org/get", withMethod: .get).

@thebluepotato that did it! Thanks!

Hi guys, I'm getting these errors. How do i fix it?. I'm using Xcode beta 6.
screen shot 2016-08-29 at 4 44 43 pm
screen shot 2016-08-29 at 4 44 00 pm

same like @chieudai14kytu

You need to remove Manager.swift file in source with pods i think.

When i open your projet, this file is not visible in projet, but in the finder, i can see it.

@lowki93 It works now. Thank you.

@chieudai14kytu your welcome ;)

@lowki93
have the same issue as @chieudai14kytu. After removing it into a trash get an error

Command /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1
help please.

don't remove in folder.

Clone the repo, move to branch swift3, and import .workspace in your project

In swift3 branch now that Alamofire.Method has become an opaque pointer, can somebody tell me how to handle it in an URLRequestConvertible Router class?

screen shot 2016-08-31 at 15 15 05

screen shot 2016-08-31 at 15 18 26

In original post, there is a checkbox for Documentation/README

Is it time to file a separate standalone issue for that? If there is not already someone working on updated README, etc., I think its own issue could be useful for tracking.

@fprosper If I'm not mistaken, the change isn't that Method is an OpaquePointer but that according to Swift 3.0 API guidelines, enum cases are to be written in lowerCamelCase (just like you did in your own enum I see).
Therefore the methods are .get, .put, .post, etc.

@thebluepotato you are right!

Thank you @thebluepotato, just noted, thanks!

I'm going to go ahead and close this issue out. Thank you everyone for all your feedback and reporting and especially you @kcharwood 🍻 for kicking all this off in the first place! We're getting really close to having everything locked down and will be starting on all the documentation updates here shortly.

AF 4 is coming!!! 🎉🎉🎉

@thebluepotato but i get error :

 Type 'Method' (aka 'OpaquePointer') has no member 'get'

any suggestion?

@mychaelgo Method has been renamed to HTTPMethod, to disambiguate it from the type you're seeing.

@jshier oh thank you for mention it!

How to call POST method with encoding as JSON, with some header and Passing some parameter

screen shot 2016-09-19 at 1 30 29 am

How can I call this method with JSON response?

Thanks for any help in advance

[!] /usr/bin/git clone https://github.com/Alamofire/Alamofire.git /var/folders/_2/cfgf2q2x7n7g3881kmbqvh4m0000gp/T/d20160919-5162-1u99gbn --template= --single-branch --depth 1 --branch swift3

Cloning into '/var/folders/_2/cfgf2q2x7n7g3881kmbqvh4m0000gp/T/d20160919-5162-1u99gbn'...
warning: Could not find remote branch swift3 to clone.
fatal: Remote branch swift3 not found in upstream origin

Was this page helpful?
0 / 5 - 0 ratings

Related issues

matthiasp42 picture matthiasp42  Â·  22Comments

mh8a picture mh8a  Â·  24Comments

jebeomgyeong picture jebeomgyeong  Â·  29Comments

Isuru-Nanayakkara picture Isuru-Nanayakkara  Â·  21Comments

blixt picture blixt  Â·  44Comments