Convert Single File to Swift 3 in Xcode 8

Convert single file to Swift 3 in Xcode 8?

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.

Conversion to Swift 3 with Xcode 8

I know that it is sad, but there is no other solution than changing your code manually. It is the same with constrains which are also different and you have to change them all (if they are wrong) :/ .

Difficulties converting to Swift 3

Most of them are easy fixes, simply by tapping the red button, and having Xcode fix it for you! Others include:

CGRect

Swift 2:

let frame = CGRectMake(0, 0, 20, 20)

Swift 3:

let frame = CGRect(x: 0, y: 0, width: 20, height: 20)

CGPoint

Swift 2:

let point = CGPointMake(0, 0)

Swift 3:

let point = CGPoint(x: 0, y: 0)

CGSize

Swift 2:

let size = CGSizeMake(20, 20)

Swift 3:

let size = CGSize(width: 20, height: 20)

CGRectGetMidX

Swift 2:

CGRectGetMidX(view)

Swift 3:

view.midX

CGRectGetMidY

Swift 2:

CGRectGetMidY(view)

Swift 3:

view.midY

UIColor

Swift 2:

let color = UIColor.redColor()

Swift 3:

let color = UIColor.red

"NS"

Swift 2:

NSTimer
NSData
NSError

Swift 3:

Timer
Data
Error

UserDefaults

Swift 2:

NSUserDefaults.standardUserDefaults().//something

Swift 3:

UserDefaults.standard.//something

Build time is too long in Xcode 8.3 with swift 3 in particular file

I tried debugging your issue. This is what I found out:

if let pdfData = data as? CFData {

}

The above line for casting object of type Data to CFData is where it's taking too much time to build.

Replacing that with the following piece of code significantly reduces your build time.

let pdfNsData: NSData = NSData(data: data) // convert `Data` to `NSData`

if let cfPdfData: CFData = pdfNsData as? CFData {
// cast `NSData` to `CFData`

}

NSData and CFData are toll-free bridged.

Please let me know if there's any doubt

ReactiveSwift not working with Xcode 8.1. Conversion to Swift 3 leads to 50 errors. Fixes?

OK I solved it! I updated Cocoapods from 1.0.1 to 1.2.0.beta.1. Followed the process suggested by @Rob and voila! no errors. After struggling with the major syntax changes in ReactiveCocoa 5.0 and sparse documentation (so far), I got the UI binding test to work using a label and textfield and ..... textField.reactive.continuousTextValues.observeValues { text in self.label.text = text as String!} .....Thanks to @Rob for his help and support!

Write to text file and persist change across app restart in Xcode 8 and Swift 3

What you're trying to do here makes no sense and is strictly forbidden:

if let fileURL = Bundle.main.url(forResource: "data", withExtension: "txt") {
do {
try newData.write(to: fileURL, atomically: false, encoding: String.Encoding.utf8)
}

Your running app may not alter a file inside your app bundle! That's a gross security violation. Copy the file out to your sandboxed Documents folder and work with it there. It will then persist across relaunching the app.



Related Topics



Leave a reply



Submit