[英]access a class type variable from another class in swift
I am trying to learn swift and i am reading this article https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html . 我正在尝试学习Swift,并且正在阅读这篇文章https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html 。 In
VideoMode
class an object like resolution
of type Resolution
is created as a property of VideoMode
class's . 在
VideoMode
类中,将创建诸如Resolution
类型的resolution
之类的对象作为VideoMode
类的属性。 After that an object let someVideoMode = VideoMode()
of VideoMode
class's is created and access width
property of Resolution
struct by someVideoMode.resolution.width
. 之后,创建一个对象,
let someVideoMode = VideoMode()
创建VideoMode
类的let someVideoMode = VideoMode()
,并通过someVideoMode.resolution.width
访问Resolution
结构的width
属性。 This concept is clear to me. 我很清楚这个概念。 But i am facing problem when i am reading this article https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/OptionalChaining.html#//apple_ref/doc/uid/TP40014097-CH21-ID245
但是我在阅读本文时遇到了问题https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/OptionalChaining.html#//apple_ref/doc/uid/TP40014097-CH21-ID245
In this article Person
class just create a property var residence: Residence?
在本文中,
Person
类仅创建一个属性var residence: Residence?
of Residence
class's. Residence
类的。 Not create an object. 不创建对象。 After that, create a object
john
of Person
class's and accessing property of Residence
class's. 之后,创建
Person
类的对象john
并访问Residence
类的属性。 What is happening here? 这是怎么回事 Please tell me, how
Person
class access property of Residence
class ? 请告诉我,怎么
Person
的类特性的访问Residence
类?
In second case we have optional
property and by default it initialize to nil
. 在第二种情况下,我们具有
optional
属性,默认情况下它将初始化为nil
。 If we look at what the optional type is, we will see that this is enum like: 如果我们看一下可选类型是什么,我们将看到它像枚举:
enum Optional<T> {
case Some(T)
case None
}
And it can be Some
Type like Int
for example or Residence
or None
and in this case it's have nil value. 它可以是
Some
类型,例如Int
或Residence
或None
,在这种情况下,它的值为nil。 And by default in your example it's None
and in this code from documentation: 默认情况下,在您的示例中为
None
并且在文档中的以下代码中:
if let roomCount = john.residence?.numberOfRooms { print("John's residence has \\(roomCount) room(s).") } else { print("Unable to retrieve the number of rooms.") }
it will print 它将打印
"Unable to retrieve the number of rooms."
“无法检索房间数。”
But if you init residence
like this: 但是,如果您像这样初始化
residence
:
let john = Person()
// Init residence
john.residence = Residence()
if let roomCount = john.residence?.numberOfRooms {
print("John's residence has \(roomCount) room(s).")
} else {
print("Unable to retrieve the number of rooms.")
}
it will print: 它会打印:
"John's residence has 1 room(s)."
“约翰的住所有1个房间。”
because the optional type enum
will return Some(Residence)
and you will have access to it value 因为可选类型
enum
将返回Some(Residence)
并且您可以访问它的值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.