0

I'm converting the codes to Swift 5 since Xcode 10.2.1 will no longer support Swift 3.

I have an user-defined runtime variables in storyboard. It was worked in Swift 3 but not in Swift 5.

Key Path | Type | Value
type | String | A

class Mains: UITableViewController, XMLParserDelegate {
  ...    
  var type = String()
  ...

  func loadBuses(){

    let url:String="http://example.com/Main.php?type="+type
    let urlToSend: URL = URL(string: url)!
    ...
  }

  ...
}

In Swift 3, it was functioned and url return "http://example.com/Main.php?type=A". But in Swift 5, actual output of url just return "http://example.com/Main.php?type=".

In there any alternatives which I can still use user-defined runtime attributes in storyboard for the class? Thank you.

2 Answers2

1

You should be seeing an error message in the Console:

this class is not key value coding-compliant for the key type

This error is a clue to the problem. User Defined Runtime Attributes work through an Objective-C / Cocoa feature, key-value coding. This requires that Objective-C be able to see the property you're trying to set. It is up to you, in modern Swift, to make a custom property accessible to Objective-C by marking it with the @objc attribute:

@objc var type = String()

Your code will then work as it did before.


(But drewster's suggestion is also good. @IBInspectable uses the same mechanism as User Defined Runtime Attributes. If you mark something as @IBInspectable it is marked as @objc automatically under the hood (the same is true of @IBOutlet, which also uses this mechanism). And an Inspectable property gives you a much nicer user interface in IB; you can set your property directly in the Attributes inspector, instead of having to mess with the User Defined Runtime Attributes table.)

matt
  • 515,959
  • 87
  • 875
  • 1,141
0

You need to do something like:

@IBInspectable var type = String()
drewster
  • 5,460
  • 5
  • 40
  • 50