Computed read-only property vs function in Swift
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的更多相关文章
- Computed Properties vs Property Requirements - protocol
In addition to stored properties, classes, structures, and enumerations can define computed properti ...
- Computed property names
[Computed property names] That allows you to put an expression in brackets [], that will be computed ...
- swift语言点评五-Function
一.函数类型 Every function in Swift has a type, consisting of the function’s parameter types and return t ...
- Knockout 新版应用开发教程之Computed Observables
Computed Observables 如果你有监控属性firstName和lastName的话,此时如果你想要显示全名? 这个时候computed(以前叫做依赖)监控属性就出马了,这是一个函数用来 ...
- ES6 new syntax of Arrow Function
Arrow Function.md Arrow Functions The basic syntax of an arrow function is as follows var fn = data ...
- Specify Computed Columns in a Table
https://docs.microsoft.com/en-us/sql/relational-databases/tables/specify-computed-columns-in-a-table ...
- vue系列---理解Vue中的computed,watch,methods的区别及源码实现(六)
_ 阅读目录 一. 理解Vue中的computed用法 二:computed 和 methods的区别? 三:Vue中的watch的用法 四:computed的基本原理及源码实现 回到顶部 一. 理解 ...
- Swift学习 (一)
以后会自己总结学习Swift的笔记与深化.希望能够帮助已经有Objective-C经验的开发者更快地学习Swift.我们一起学习,同时也品味到Swift的精妙之处. 结论放在开头:我认为Swift比O ...
- computed属性和watcher
computed属性 在模板中使用表达式是非常方便直接的,然而这只适用于简单的操作.在模板中放入太多的逻辑,会使模板过度膨胀和难以维护.例如: <div id="example&quo ...
随机推荐
- OC中的字典
// ********************不可变最字典***************** /* NSDictionary * dic = [NSDictionary dictionaryWithO ...
- iOS基于MVC的项目重构总结
关于MVC的争论 关于MVC的争论已经有很多,对此我的观点是:对于iOS开发中的绝大部分场景来说,MVC本身是没有问题的,你认为的MVC的问题,一定是你自己理解的问题(资深架构师请自动忽略本文). 行 ...
- linux io stack
- stream数据流
首先必须先要了解Buffer,Buffer是js和c++的结合体,类数组,通常与Stream一起用: 1. (1).Buffer是个类,因此可以构建成对象 (2).buf具有数组的性质 字符串转换成二 ...
- C# 实现函数回调
public class Lib { public delegate void UserFunctionCB(); private static UserFunctionCB m_userFnCB; ...
- 通过pustil模块取pid及对应的pidname
通过pustil模块取pid及对应的pidname import psutil import json def getpid(): reslut = psutil.pids() return resl ...
- 可爱的Python_课后习题_CDay−5 Python 初体验和原始需求
计算今年是否是闰年.判断闰年条件,满足年份模400 为0,或者模4 为0 但模100不为0. def is_learp_year(year): """判断年份是否为润年& ...
- rplidar & hector slam without odometry
接上一篇:1.rplidar测试 方式一:测试使用rplidar A2跑一下手持的hector slam,参考文章:用hector mapping构建地图 但是roslaunch exbotxi_br ...
- Android四大组件之Activity
实验内容 了解Activity的四个状态 Activity的生命周期 启动另外一个Activity 实验要求 编码实现观察Activity的生命周期函数执行过程 编码实现启动另外一个Activity ...
- Css定位之relative_慕课网课程笔记
前言 最近在慕课网上跟着张鑫旭大神重新学习一遍CSS相关的知识,以下是学习的笔记以及个人一些理解 relative对绝对定位的限制 1.限制绝对定位 绝对定位的top.left.right和botto ...