可选绑定的命名约定 [英] Naming convention for optional binding

查看:53
本文介绍了可选绑定的命名约定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最初使我不愿在代码中加入太多可选绑定的一件事是添加了更多的变量名.例如,我通常会这样写:

One thing that originally discouraged me from incorporating too much optional binding in my code was the addition of more variable names. For example, I'd generally write:

if bananasInBarrel != nil{
  print("We have \(bananasInBarrel!) bananas in the barrel.")
}

因为替代方法似乎有些混乱:

Because the alternative seemed to get a bit messy:

if let safeBananas = bananasInBarrel{
  print("We have \(safeBananas) bananas in the barrel.")
}

那是很多香蕉.我已经看到人们使用像b这样的新变量名(在更大的代码块中可能很难阅读),但是我想知道变量名样式是否有一个公认的标准与可选绑定一起使用?感谢您的阅读.

That's a lot of bananas. I've seen people use something like b as the new variable name (which could get hard-to-read in a larger block of code), but I'm wondering if there's a generally accepted standard for the style of variable name to use with optional binding? Thanks for reading.

推荐答案

只需使用相同的名称:

if let bananasInBarrel = bananasInBarrel {
  print("We have \(bananasInBarrel) bananas in the barrel.")
}

不要使用匈牙利符号-如果您使用的是未包装的可选内容,则编译器会抱怨.

Don't use hungarian notation - the compiler will complain if you are using an unwrapped optional.

这篇关于可选绑定的命名约定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆