我在iOS游乐场编写了一个结构,想要自定义它的打印格式。
struct Point {
let x: Int, y: Int
}
extension Point: CustomStringConvertible {
var description: String {
switch (x, y) {
case let (x, 1..<10):
print("y in the range")
return "(\(x), 1..<10)"
default:
return "(\(x), \(y))"
}
}
}
let p = Point(x: 1, y: 1)
print(p)
结果是
我无法理解即使我只打了一次print
,但打印了y in the range
条消息4次。
答案 0 :(得分:3)
如果您正在使用游乐场,则可以多次计算值的描述,因为它将在多个位置显示(例如,在右侧)。
如果您在更受控制的环境中执行代码(例如在编译代码中或在终端的REPL中),您会注意到while() { //your outer loop
while() { //your innner loop
if (!(($key=="addressID") AND (!array_key_exists($value, $aAddress))))
{
//do code here
}
else {
flag = 1;
break;
}
}
if(flag==1) {
flag = 0;
continue;
}
else {
// your echo code goes here
}
}
只会打印一次。
此外,您应该避免计算属性中的副作用(如y in the range
语句)。