[swift-evolution] [PITCH] WatchKit API Design Change to use properties instead of methods to set properties

michie.swift at riffic.io michie.swift at riffic.io
Wed Jun 22 18:03:20 CDT 2016


  PROPOSING: UPDATE WATCHKIT API DESIGN TO USE PROPERTIES INSTEAD OF METHODS
TO SET PROPERTIES

  by Michie - https://github.com/michieriffic


INTRODUCTION

WKInterfaceLabel is still using methods for set Text (setText), set Text
Color (setTextColor), set Attributed Text (setAttributedText) to set
properties of WKInterfaceLabel. I think it's more appropriate to make these
into properties rather than methods just like in the UILabel. That would
make it more consistent with building apps for both iOS, MacOS, WatchOS,
and more.

Also, other objects in WatchKit needs to be updated too to use properties
instead of methods to set properties so we can easily access it using dot
notation. WKInterfaceLabel is just an example.

MOTIVATION

While creating an app for watchOS, it has been a habit for me to use the
dot notation to access a property of an object and set it using "=". And
text, textColor, and attributedText are properties rather than methods of
an object.

PROPOSED SOLUTION & DETAILED DESIGN

public class WKInterfaceLabel : WKInterfaceObject {

   public var text: String?
   public var textColor: UIColor?
   @NSCopying public var attributedText: AttributedString?

}

/INSTEAD OF/

public class WKInterfaceLabel : WKInterfaceObject {

   public func setText(_ text: String?)
   public func setTextColor(_ color: UIColor?)
   public func setAttributedText(_ attributtedText: AttributedString?)

}

IMPACT ON EXISTING CODE

Impact: /Would be more consistent and natural to the Swift language when
building apps for WatchOS/

BEFORE:

  watchLabel.setText("Text String")
  watchLabel.setTextColor(UIColor.red())

  AFTER:

  watchLabel.text = "Text String"
  watchLabel.textColor = UIColor.red()

  Will some Swift applications stop compiling due to this change? /Possible/

Will applications still compile but produce different behavior than they
used to? /No, if everything was migrated properly./

Is it possible to migrate existing Swift code to use a new feature or API
automatically? /Yes./

ALTERNATIVES CONSIDERED

Just use what's currently available.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160622/d5c4f427/attachment.html>


More information about the swift-evolution mailing list