In the Introduction to Swift WWDC session, a read-only property description is demonstrated:

class Vehicle {
var numberOfWheels = 0
var description: String {
return "\(numberOfWheels) wheels"
}
} let vehicle = Vehicle()
println(vehicle.description)

Are there any implications to choosing the above approach over using a method instead:

class Vehicle {
var numberOfWheels = 0
func description() -> String {
return "\(numberOfWheels) wheels"
}
} let vehicle = Vehicle()
println(vehicle.description())

It seems to me that the most obvious reasons you would choose a read-only computed property are:

Semantics - in this example it makes sense for description to be a property of the class, rather than an action it performs.

Brevity/Clarity - prevents the need to use empty parentheses when getting the value.

Clearly the above example is overly simple, but are there other good reasons to choose one over the other? For example, are there some features of functions or properties that would guide your decision of which to use?

N.B. At first glance this seems like quite a common OOP question, but I'm keen to know of any Swift-specific features that would guide best practice when using this language.

Computed read-only property vs function in Swift的更多相关文章

  1. Computed Properties vs Property Requirements - protocol

    In addition to stored properties, classes, structures, and enumerations can define computed properti ...

  2. Computed property names

    [Computed property names] That allows you to put an expression in brackets [], that will be computed ...

  3. swift语言点评五-Function

    一.函数类型 Every function in Swift has a type, consisting of the function’s parameter types and return t ...

  4. Knockout 新版应用开发教程之Computed Observables

    Computed Observables 如果你有监控属性firstName和lastName的话,此时如果你想要显示全名? 这个时候computed(以前叫做依赖)监控属性就出马了,这是一个函数用来 ...

  5. ES6 new syntax of Arrow Function

    Arrow Function.md Arrow Functions The basic syntax of an arrow function is as follows var fn = data ...

  6. Specify Computed Columns in a Table

    https://docs.microsoft.com/en-us/sql/relational-databases/tables/specify-computed-columns-in-a-table ...

  7. vue系列---理解Vue中的computed,watch,methods的区别及源码实现(六)

    _ 阅读目录 一. 理解Vue中的computed用法 二:computed 和 methods的区别? 三:Vue中的watch的用法 四:computed的基本原理及源码实现 回到顶部 一. 理解 ...

  8. Swift学习 (一)

    以后会自己总结学习Swift的笔记与深化.希望能够帮助已经有Objective-C经验的开发者更快地学习Swift.我们一起学习,同时也品味到Swift的精妙之处. 结论放在开头:我认为Swift比O ...

  9. computed属性和watcher

    computed属性 在模板中使用表达式是非常方便直接的,然而这只适用于简单的操作.在模板中放入太多的逻辑,会使模板过度膨胀和难以维护.例如: <div id="example&quo ...

随机推荐

  1. @Html.Raw()

    在用VS 2015写代码时,匹配邮箱的正则表达式 /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/​ @报错  主要因为 @是MVC里的关键字,所以不能直 ...

  2. app活动页面上的痛点

    app项目上需要做一个小的活动,先看下页面布局 需求是这5个板块逐个展示,展示一块的时候,页面整体向上滚动一定的距离. 刚开始考虑的时候,是准备依赖css3属性的transition实现的,包括顺序延 ...

  3. 对于似1/(1+x^4)型的不定积分的总结

    最近在求解一道不定积分的经典例题时遇到了一点小麻烦.的确,在处理1/(1+x^4)积分的时候,需要一定的技巧性,不然会使计算量变得庞大. 下面,我简单的总结了类似结构不定积分的求解方法,希望大家看完之 ...

  4. MYSQL相关完整笔记

    useradd mysql -s /sbin/nologin cat/etc/passwd | grep mysqlcat /etc/group | grep mysql 源目录 cd /usr/sr ...

  5. (分享)java向MySQL插入当前时间的四种方式和java时间日期格式化的几种方法(案例说明)

    原文地址 http://blog.csdn.net/yangkai_hudong/article/details/18705713

  6. RestTemplate配置

    什么是RestTemplate? RestTemplate是Spring提供的用于访问Rest服务的客户端,RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效 ...

  7. spring-amqp 动态创建queue、exchange、binding

    pom.xml <!-- mq 依赖 --> <dependency> <groupId>com.rabbitmq</groupId> <arti ...

  8. [PCL]点云渐进形态学滤波

    PCL支持点云的形态学滤波,四种操作:侵蚀.膨胀.开(先侵蚀后膨胀).闭(先膨胀后侵蚀) 在#include <pcl/filters/morphological_filter.h>中定义 ...

  9. Ubuntu 下安装 MySQL Workbench

    打开终端输入命令: sudo  dpkg -i mysql-workbench-community-6.0.9-1ubu1204-i386.deb 如果安装不成功的提示,可以输入以下命令: apt-g ...

  10. oracle 9i相关问题

    Oracle 9i在连接数据库的时候需要加上双引号,如sqlplus “sys/oracle@orcl as sysdba” Oracle 9i不支持bigfile大的表空间创建,oracle9i或以 ...