ios Swift 嵌套类属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26806932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift nested class properties
提问by NJGUY
Does swift not have nested classes??
swift 没有嵌套类吗?
For example, I can't seem to access the property test of the master class from the nested class.
例如,我似乎无法从嵌套类访问主类的属性测试。
class master{
var test = 2;
class nested{
init(){
let example = test; //this doesn't work
}
}
}
回答by rob mayoff
Swift's nested classes are not like Java's nested classes. Well, they're like one kind of Java's nested classes, but not the kind you're thinking of.
Swift 的嵌套类不像 Java 的嵌套类。嗯,它们就像 Java 的一种嵌套类,但不是您所想的那种。
In Java, an instance of an inner class automatically has a reference to an instance of the outer class (unless the inner class is declared static
). You can only create an instance of the inner class if you have an instance of the outer class. That's why in Java you say something like this.new nested()
.
在 Java 中,内部类的实例自动具有对外部类实例的引用(除非声明了内部类static
)。如果您有外部类的实例,则只能创建内部类的实例。这就是为什么在 Java 中你会说类似this.new nested()
.
In Swift, an instance of an inner class is independent of any instance of the outer class. It is as if all inner classes in Swift are declared using Java's static
. If you want the instance of the inner class to have a reference to an instance of the outer class, you must make it explicit:
在 Swift 中,内部类的实例独立于外部类的任何实例。就好像 Swift 中的所有内部类都是使用 Java 的static
. 如果您希望内部类的实例引用外部类的实例,则必须使其显式:
class Master {
var test = 2;
class Nested{
init(master: Master) {
let example = master.test;
}
}
func f() {
// Nested is in scope in the body of Master, so this works:
let n = Nested(master: self)
}
}
var m = Master()
// Outside Master, you must qualify the name of the inner class:
var n = Master.Nested(master:m)
// This doesn't work because Nested isn't an instance property or function:
var n2 = m.Nested()
// This doesn't work because Nested isn't in scope here:
var n3 = Nested(master: m)
回答by James Hazlett
This solution is sort of similar to how I use it in C#, and I have successfully tested it in Xcode.
这个解决方案有点类似于我在 C# 中使用它的方式,我已经在 Xcode 中成功测试了它。
Here's a breakdown of the process:
以下是该过程的细分:
- Your nested class needs to be made optional so you don't have to initialize it, so theres a '?' in the declaration; if you initialize both your parent class and your nested class, you end up with a 'recursion' effect and an error is generated
- Create a regular function that receives an argument of the same type as your main class
- Pass that argument to your nested class (this can go into the nested object's normal initializer). - Since objects are passed by reference by default, there's nothing special to get your nested class to link to the parent class
- Inside your nested class, you need a variable of the same type as your parent class
- 你的嵌套类需要成为可选的,这样你就不必初始化它,所以有一个 '?' 在声明中;如果同时初始化父类和嵌套类,最终会产生“递归”效果并生成错误
- 创建一个常规函数,该函数接收与主类相同类型的参数
- 将该参数传递给您的嵌套类(这可以进入嵌套对象的普通初始值设定项)。- 由于默认情况下对象是通过引用传递的,因此没有什么特别可以让您的嵌套类链接到父类
- 在嵌套类中,您需要一个与父类相同类型的变量
From here on out set up everything as you normally would.
从这里开始,像往常一样设置一切。
In the code execution area, your nested class object also needs to be regarded as optional (hence the '?'). If you forget about it, Xcode will add it anyways.
在代码执行区域,您的嵌套类对象也需要被视为可选的(因此是“?”)。如果您忘记了它,Xcode 无论如何都会添加它。
In this example, I wanted to design a keyword "set," so when I set variables, I can type:
在这个例子中,我想设计一个关键字“set”,所以当我设置变量时,我可以输入:
testClass.set.(and then a descriptive method name)
Here is the code, and its goal is to output "test" in the console, after the value is set via the nested object:
这是代码,它的目标是在通过嵌套对象设置值后在控制台中输出“test”:
class testClass
{
var test_string:String = ""
var set: class_set?
func construct_objects(argument: testClass)
{
self.set = class_set(argument: argument)
}
class class_set
{
var parent:testClass
init(argument: testClass)
{
parent = argument
}
func test_string_to_argument(argument: String)
{
parent.test_string = argument
}
}
}
var oTestClass = testClass()
oTestClass.construct_objects(oTestClass)
oTestClass.set?.test_string_to_argument("test")
print(oTestClass.test_string)
回答by yoAlex5
Nested for Swift and Java
为 Swift 和 Java 嵌套
- Swift has Nested Typesdefinitions
- Java has more complex hierarchy of nested class
Swift's Nested
is more similar to Java's Static Nested
, as a result you do not havean access to properties of outer class. To get access of outer class you can pass it as a parameter.
Swift's Nested
更类似于Java's Static Nested
,因此您无法访问外部类的属性。要访问外部类,您可以将其作为参数传递。