Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert single file to Swift 3 in Xcode 8?

Is there any way in Xcode 8 or 8.1 to do a Swift 2.X -> Swift 3 migration for a single file in a project that's already been converted to Swift 3?

For technical debt/legacy build process reasons, we are currently having to maintain both Xcode 7 and Xcode 8 versions of our project. This is causing headaches when we have to merge new development from Xcode 7 over to Xcode 8. Since the project has already been fully converted to Swift 3 in our Xcode 8 branch (including a ton of hand fixes), we can't use the full-project automated converter. Therefore we are currently having to do a hand conversion from Swift 2.2 to Swift 3 for any new Swift files which were added in Xcode 7, which takes a significant amount of time.

I apologize if this has already been asked, I couldn't find any clear answer with Google.

like image 925
Chris Vig Avatar asked Nov 01 '16 14:11

Chris Vig


2 Answers

I have a project with multiple targets where each target is a slightly more advanced version of using OpenGL with Swift. It's one project with multiple tutorials, if you will. When swift 3 became available, I used the converter, and noticed it works on a per target basis. With that in mind I suggest:

  1. Create a dumb target in the project.
  2. Add any files you want updated to this target.
  3. Run the conversation editor
  4. Associate or place those files back into your already converted target.

I realize it's a bit of a hack, but if it works, why not? Hope that helps.

As a side note, if you have any code dealing with Unsafe[Mutable]Pointer and you utilize alloc() or malloc(), the converter does not replace the alloc()/malloc() with the new UnsafeMutablePointer methods--allocate() and initialize(). You are likely using these if you are working with any of the C API's.

like image 127
Buggus Mageevers Avatar answered Nov 08 '22 01:11

Buggus Mageevers


A thought (untested) is to delete the lines

LastSwiftMigration = 0800;

and/or

SWIFT_VERSION = 3.0;

from project.pbxproj and see if Xcode allows you to use the migrator again. Those lines were both added after I ran the migrator.

Then if it works, obviously, you need to deselect all the files you don't want migrated.

That's all I can think of anyway.

like image 36
Andreas Avatar answered Nov 08 '22 01:11

Andreas