Difficulties Converting to Swift 3

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

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) :/ .

iOS 10 and swift 3 conversion questions

The advice is largely the same as for all Xcode beta versions: Treat beta versions of Xcode (esp Swift 3) as a "sandbox" in which you can play, but don't convert production projects. Or, more accurately, feel free to convert it so you can play in your sandbox so you'll be prepared for when Swift 3 comes out of beta, but do so knowing that the current beta Swift 3 is subject to further changes. Make sure you preserve your existing production code/projects until the Xcode 8 is out of beta). Never plan on being able to open the Swift 3 project in Xcode 7.x. Keep Swift 3 code and original Swift 2.x projects completely segregated.

Re your iOS 9 app on iOS 10, I'm surprised that you're seeing problems with your iOS 9 app (esp with iOS 10 beta 7), because most of those kinks have been worked out by now. If something isn't working right, that's a bit of an edge-case, because most well written iOS 9 code works seamlessly with iOS 10. There are API changes, but an app built for iOS 9 generally runs fine on an iOS 10 device. There are very few things that would result in those sorts of visual artifacts/errors that you describe. You're having problems, you should post a new question with a MCVE illustrating the problem.

Trouble Converting Code to Swift 3.0, Type Any Has no Subscript

AnyObject has become Any. You probably want this method changed to return [String : Any]. Likely your cellDescriptors array should change to Any as well; this will have some cascading changes but should get you closer to compiling.

Convert from swift 3 to swift 2.3 or get source code from app on the iPhone

Unfortunately code migrators only run one direction (move to the more recent version). You're not going to find any way to automatically convert Swift 3 code to an earlier version.

As @rmaddy points out, the typical approach here is simply to roll back to an older version of your source tree from whatever your source control system is. If you don't have a source control system, you absolutely should (git is easy for simple projects, free, and integrates with Xcode). If you have any backups system then you should turn to that. (To your specific question about getting source code from the device, unfortunately it doesn't work that way-- what ends up on your device is only the compiled end product.)

Failing all of that, if you still have a pile of broken Swift 3 code, your best option is very likely to press forward with Xcode 8/Swift 3 and deal with the errors/warnings. That's a normal part of handling drastic changes to the language and they will generally be trivial to "fix" and make compatible/correct.

How to fix C-style for statement has been removed in Swift 3?

I don't think this can be written using for anymore, but you can use while loop to get the job done:

var nSize = merkleTree.count
while nSize > 1 {
// loop body

nSize = (nSize + 1) / 2
}

I would expect stride not to work in this case, because as your error states, you cannot use nSize as the stride parameter - nSize is iterating variable that gets declared based on the range, so you need the range to exist. At least that's my interpretation of the error (I know that theoretically you can generate range based on the previously generated item, but obviously stride does not work that way).

I believe you can find a way to generate a proper array of values using reduce (because I was able to, see below, maybe you can make it simpler), or by implementing your own stride that would accept a closure instead of a step (which would allow you to compute next item based on previous one), but both approaches are more complicated and obscure than using the simple while loop, so I personally prefer the while loop.

My not so nice reduce implementation (in result it uses an array and not a range, since by looking at NSRange I don't think you can create a range that does not step by 1):

let merkleTree = [1,2,3,4,5,6,7,8,9]

let numberOfDivisions = Int(log2(Double(merkleTree.count))) + 1
let startValue = merkleTree.count
let nSizes = (0..<numberOfDivisions).reduce([startValue]) { (result, next) -> [Int] in
var newResult = result
newResult.append((result.last! + 1) / 2)
return newResult
}
print(nSizes)
// and now you can for-in it:
for nSize in nSizes {
// ...
}


Related Topics



Leave a reply



Submit