斯威夫特弦与弦!与字符串? [英] Swift string vs. string! vs. string?

查看:87
本文介绍了斯威夫特弦与弦!与字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读

示例:

struct PancakeHouse {
  let name: String // this doesn't have '?' nor '!'
  let photo: UIImage?
  let location: CLLocationCoordinate2D?
  let details: String
}

我最大的困惑是什么时候我们想使用可选"?

对于您的PancakeHouse结构,name是非可选的.这意味着其name属性 不能为nil.每当PancakeHouse的实例被初始化时,编译器将强制要求将name初始化为非nil值.为此,它要求在为PancakeHouse定义的所有初始化器中设置name.

对于@IBOutlet属性,这是不可能的.当未存档/加载Interface Builder文件(XIB或Storyboard)时,将设置IB文件中定义的出口,但这总是在 对象初始化后(例如,在加载视图期间)发生.因此,在初始化之后但必须在设置插座之前需要一定的时间,在此期间,插座将为零. (还有一个问题,即可能没有在IB文件中设置出口,并且编译器没有/不能检查它.)这解释了为什么@IBOutlet属性必须是可选的. /p>

@IBOutlet的隐式展开可选(!)和常规可选(?)之间的选择取决于您.从本质上讲,参数是使用!允许您将属性视为非可选属性,因此永远不会为零.如果由于某种原因它们为零,则通常将其视为程序员错误(例如,插座未连接,或者您在视图加载完成之前对其进行了访问等),在这种情况下,在开发过程中崩溃导致的故障将帮助您发现问题.错误更快.另一方面,将它们声明为常规的可选内容,则需要任何使用它们的代码来显式处理由于某些原因而可能未设置它们的情况. Apple选择默认将隐式解包作为默认值,但是有一些Swift程序员出于自身原因对@IBOutlet属性使用常规的可选内容.

I have read this question and some other questions. But they are somewhat unrelated to my question

For UILabel if you don't specify ? or ! you will get such an error:

@IBOutlet property has non-optional type 'UILabel'

Which then Xcode gives you 2 choices to have it fixed, you can do:

fix-it Add ? to form the optional type UIlabel?
fix-it Add ! to form the implicitly unwrapped optional type UIlabel?

However for string you can just type string without ? or ! and you won't get an error why is that?

What happens if the name isn't set? Then we would have a nil isn't using ?, ! and Swift all about satisfying 'type-safety'?

Example:

struct PancakeHouse {
  let name: String // this doesn't have '?' nor '!'
  let photo: UIImage?
  let location: CLLocationCoordinate2D?
  let details: String
}

My major confussion is when would we want to not use Optional?

解决方案

For your PancakeHouse struct, name is non-optional. That means that its name property cannot be nil. The compiler will enforce a requirement that name be initialized to a non-nil value anytime an instance of PancakeHouse is initialized. It does this by requiring name to be set in any and all initializers defined for PancakeHouse.

For @IBOutlet properties, this is not possible. When an Interface Builder file (XIB or Storyboard) is unarchived/loaded, the outlets defined in the IB file are set, but this always occurs after the objects therein have been initialized (e.g. during view loading). So there is necessarily a time after initialization but before the outlets have been set, and during this time, the outlets will be nil. (There's also the issue that the outlets may not be set in the IB file, and the compiler doesn't/can't check that.) This explains why @IBOutlet properties must be optional.

The choice between an implicitly unwrapped optional (!) and a regular optional (?) for @IBOutlets is up to you. The arguments are essentially that using ! lets you treat the property as if it were non-optional and therefore never nil. If they are nil for some reason, that would generally be considered a programmer error (e.g. outlet is not connected, or you accessed it before view loading finished, etc.), and in those cases, failing by crashing during development will help you catch the bug faster. On the other hand, declaring them as regular optionals, requires any code that uses them to explicitly handle the case where they may not be set for some reason. Apple chose implicitly unwrapped as the default, but there are Swift programmers who for their own reasons, use regular optionals for @IBOutlet properties.

这篇关于斯威夫特弦与弦!与字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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