Xcode9 migrate project from swift3 to swift4

Hi,I get some errors when I migrate the app “Groundhog”(WatchOS by Tutorials, Chapter 23, HealKit) from swift 3 to swift 4.
After I migrate the code to swift 4, when I build this project, Xcode 9 reports these errors in Groundhog → Groundhog WatchKit Extension → Workouts → Views → TimeLabel.swift.

The code is here:

let paragraphStyle = NSMutableParagraphStyle()strong text
paragraphStyle.alignment = .right
var fontAttributes: [String:AnyObject] = [NSAttributedStringKey.foregroundColor.rawValue : UIColor.white]
fontAttributes[NSAttributedStringKey.font] = UIFont(name: “Avenir”, size: CGFloat(40.0)) //Error: Cannot subscript a value of type ‘[String : AnyObject]’ with an index of type ‘NSAttributedStringKey’
fontAttributes[NSParagraphStyleAttributeName] = paragraphStyle //Error: Cannot subscript a value of type ‘[String : AnyObject]’ with an index of type ‘NSAttributedStringKey’

let attrString = NSAttributedString(string: timeString, attributes: fontAttributes)       _**//Error: Cannot convert value of type '[String : AnyObject]' to expected argument type '[NSAttributedStringKey : Any]?'**_
attrString.draw(in: CGRect(origin: CGPoint(x: 0, y: 0), size: imageSize))

No idea how to fix this.

Thanks in advance.

Fixed.

var fontAttributes: [NSAttributedStringKey : Any] = [NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue): UIColor.white]
fontAttributes[NSAttributedStringKey.font] = UIFont(name: “Avenir”, size: CGFloat(40.0))
fontAttributes[NSAttributedStringKey.paragraphStyle] = paragraphStyle

let attrString = NSAttributedString(string: timeString, attributes: fontAttributes)

Hi @haowu, i’m glad you fixed the issue that you had however I must warn you that the watchOS by tutorials book still hasn’t been updated for Xcode 9 and Swift 4 yet unfortunatly :confused:

The recommendation is that you follow the book using Swift 3 in Xcode 8.1 so if you try to follow using watchOS 4/Swift 4/Xcode 9 then you might run into more issues like this. We’ll try to do our best to help out here but I just wanted to give you a heads up :slight_smile:

Hi there,

Do you have any expected date for the update of watchOS for Xcode 9 and Swift 4?

I have been using it happily until the update, but it looks like a lot things broke with the update. For example, I can no longer get values from the Queries when running on the device. On the simulator it still works fine.

Thanks,
Bruno

This topic was automatically closed after 166 days. New replies are no longer allowed.