Swift编程语言指南中有如下示例:
class Person {
let name: String
init(name: String) { self.name = name }
var apartment: Apartment?
deinit { println("\(name) is being deinitialized") }
}
class Apartment {
let number: Int
init(number: Int) { self.number = number }
var tenant: Person?
deinit { println("Apartment #\(number) is being deinitialized") }
}
var john: Person?
var number73: Apartment?
john = Person(name: "John Appleseed")
number73 = Apartment(number: 73)
//From Apple's “The Swift Programming Language” guide (https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AutomaticReferenceCounting.html)
然后,在给这个人分配公寓时,他们使用感叹号来“打开实例”:
john!.apartment = number73
“展开实例”是什么意思?为什么有必要?它与仅仅做以下事情有什么不同:
john.apartment = number73
我对Swift语言很不熟悉。我只是想把基本的东西记下来。
更新:
我遗漏了一个很大的谜团(答案中没有直接说明——至少在写这篇文章的时候没有),那就是当你做以下事情的时候:
var john: Person?
这并不意味着“约翰是人的类型,它可能是nil”,因为我最初的想法。我只是误解了《人与人》是完全不同的类型。一旦我明白了,所有其他的?,!疯狂,以及下面这些精彩的回答,让我觉得更有道理。
以下是我认为的区别:
var john: Person?
意味着john可以是nil
john?.apartment = number73
编译器将这一行解释为:
if john != nil {
john.apartment = number73
}
而
john!.apartment = number73
编译器将简单地解释这一行:
john.apartment = number73
因此,使用!将打开if语句,并使其运行得更快,但如果John为nil,则会发生运行时错误。
这里的wrap并不是指它是内存包装,而是指它是代码包装,在这种情况下,它是用if语句包装的,因为苹果非常关注运行时的性能,他们想给你一种方法让你的应用以最好的性能运行。
更新:
Getting back to this answer after 4 years, as I got the highest reputations from it in Stackoverflow :)
I misunderstood a little the meaning of unwrapping at that time. Now after 4 years I believe the meaning of unwrapping here is to expand the code from its original compact form. Also it means removing the vagueness around that object, as we are not sure by definition if it is nil or not. Just like the answer of Ashley above, think about it as a present which could contain nothing in it. But I still think that the unwrapping is code unwrapping and not memory based unwrapping as using enum.
问问你自己
这种类型的人吗?有公寓会员/房产?或
这种类型的人有公寓成员/房产吗?
如果你不能回答这个问题,那么继续阅读:
要理解,你可能需要对泛型有超基础的理解。在这里看到的。Swift中的很多东西都是用泛型编写的。可选的包括
下面的代码可以从斯坦福大学的视频中获得。强烈推荐你看前5分钟
“Optional”为enum,只有2个大小写
enum Optional<T>{
case None
case Some(T)
}
let x: String? = nil //actually means:
let x = Optional<String>.None
let x :String? = "hello" //actually means:
let x = Optional<String>.Some("hello")
var y = x! // actually means:
switch x {
case .Some(let value): y = value
case .None: // Raise an exception
}
可选的绑定:
let x:String? = something
if let y = x {
// do something with y
}
//Actually means:
switch x{
case .Some(let y): print)(y) // or whatever else you like using
case .None: break
}
当你说var的时候john: Person?你的意思是:
enum Optional<Person>{
case .None
case .Some(Person)
}
上面的枚举是否有名为apartment的属性?你看到它了吗?根本就不在那儿!然而,如果你打开它ie做人!然后你就可以……它在引擎盖下面做的是:Optional<Person>。一些(人(名字:“John Appleseed”))
你是否定义了var john: Person而不是:var john: Person?那么你就不再需要了!使用,因为Person本身有一个apartment的成员
作为以后讨论为什么使用!打开包装有时不建议看这个问答