不了解swift编程中的初始化过程 [英] Did not understand process of initialize in swift programming

查看:15
本文介绍了不了解swift编程中的初始化过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Swfit,我从下面的链接开始学习
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html

I am learning Swfit and i start studied from below link
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/
https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html

我对 swift 中的 Initialization 有疑问.

I have question regarding Initialization in swift.

我的理解如下

1] 它就像一个 swift 类的构造函数.
2] 我们必须初始化其中的所有属性.
3] 我们必须调用超类的 init() 方法.
4] 在调用 init() 之前,我们必须初始化每个属性.
5]我们可以在初始化之后使用超类的任何成员或方法.

1] It works like a constructor for swift classes.
2] We have to initialize all properties in it.
3] We have to call init() method of super class.
4] Before calling init() we have to initialize each and every property.
5] We can use any member or method of super classes after initialize it.

在以上 5 点的基础上,我创建了一个类.
但由于第 3,4 和 5 点出现问题.

课程

On the bases of above 5 points I create a classes.
but having a problem because of point 3,4 and 5.

Classes

/*
AdminManagmentSystem
- Some class which will consume lots of memory during init
*/
class AdminManagmentSystem {

    var adminKey : String
    init(key:String)
    {
        self.adminKey = key;
        println("Consume lots of memory");
    }
}

/*
Person
- A base class which can create key.
*/
class Person {

    // Some property which will user to create private key.
    private var privateKey : String = "private"
    init()
    {
        privateKey = "private";
    }

    // function which will calculate key (Comman for all person type)
    private  func calculatekey() -> NSString
    {
        return self.privateKey + " key";
    }
}

/*
Admin
- A sub class which have object of AdminManagmentSystem
*/
class Admin : Person {

    // Constant variable
    let adminmanagmennt : AdminManagmentSystem

    override init()
    {
        self.adminmanagmennt = AdminManagmentSystem(key: ""); // Line1 : Consume lots of memory
        super.init(); // Line2 : its compalsurry to call super.init
        var adminKey = super.calculatekey(); // Line3 : We can use any member or method of supper after callign init().
        self.adminmanagmennt = AdminManagmentSystem(key: adminKey); // Line4 : Again consume lots of memory
    }
}


下载项目
https://www.dropbox.com/s/afohuxpxxkl5b3c/understandInheritance.zip?dl=0<小时>问题
Line1Line4 中,我必须创建消耗大量内存的 AdminManagmentSystem 对象.


Download project
https://www.dropbox.com/s/afohuxpxxkl5b3c/understandInheritance.zip?dl=0


Problem
In Line1 and Line4 i have to create object of AdminManagmentSystem which consume a lot memory.

问题
1] 为什么 swift 强制要求在调用 super.init() 之前初始化每个属性?
2] 如果我使我的属性保持不变,为什么 swift 允许我在 init 方法中多次初始化它?
3] 为什么我必须在 init() 之前写 override 关键字?

推荐答案

回答 1 -

来自 Swift 编程语言 -

From the Swift Programming Language -

如上所述,一个对象的内存只被完全考虑一旦其所有存储属性的初始状态为已知.为了满足该规则,指定的初始化程序必须确保它自己的所有属性都已初始化在它从链条上移开之前."

"As mentioned above, the memory for an object is only considered fully initialized once the initial state of all of its stored properties is known. In order for this rule to be satisfied, a designated initializer must make sure that all its own properties are initialized before it hands off up the chain."

摘自:Apple Inc.The Swift Programming Language".电子书.https://itun.es/au/jEUH0.l

Excerpt From: Apple Inc. "The Swift Programming Language." iBooks. https://itun.es/au/jEUH0.l

因此,在对象完全初始化之前,该对象处于未定义状态,这可能是不安全的",因此 Swift 要求在阶段 1 中初始化所有属性.

So, until the object is fully initialised, the object is in an undefined state which may be "unsafe", so Swift requires all properties are initialised in phase 1.

回答2

初始化函数是一种特殊情况 - 它的工作是将对象设置为其初始状态,因此您可以修改常量"属性,因为对象仍在创建过程中 -

An initialiser function is a special case - its job is to set the object to its initial state, so you can modify 'constant' properties as the object is still in the process of being created -

您可以随时修改常量属性的值初始化,只要被时间设置为一个确定的值初始化完成."

"You can modify the value of a constant property at any point during initialization, as long as it is set to a definite value by the time initialization finishes."

摘自:Apple Inc.The Swift Programming Language".电子书.https://itun.es/au/jEUH0.l

Excerpt From: Apple Inc. "The Swift Programming Language." iBooks. https://itun.es/au/jEUH0.l

在对 3 的回答中,因为您要覆盖与超类具有相同签名的方法(无参数 init 函数).override 关键字向编译器表明您知道自己在做什么.如果编译器默默地让你重新声明一个超类方法,你可能没有意识到你正在这样做并得到意想不到的结果.

In answer to 3, because you are overriding a method with the same signature as the super class (the no argument init function). The override keyword indicates to the compiler that you know what you are doing. If the compiler silently let you re-declare a super class method you may not realise that you are doing it and get unexpected results.

针对内存消耗的问题,ARC会快速回收分配给对象第一个实例的内存.如果这是一个性能问题,那么重构 AdminManagmentSystem 类是相当简单的,以便有一个函数来重置现有实例上的键 -

In answer to the question regarding the consumption of memory, ARC will quickly reclaim the memory that was allocated for the first instance of the object. If this is a performance issue then it is fairly simple to refactor the AdminManagmentSystem class so that there is a function to reset the key on an existing instance -

class Admin : Person {

    // Constant variable
    let adminmanagmennt : AdminManagmentSystem

    override init()
    {
        self.adminmanagmennt = AdminManagmentSystem(key: ""); // Line1 : Consume lots of memory
        super.init(); // Line2 : its compalsurry to call super.init
        var adminKey = super.calculatekey(); // Line3 : We can use any member or method of supper after callign init().
        self.adminmanagmennt.key=adminKey;  // You can use a property observer function if a simple assignment isn't enough
    }
}

这篇关于不了解swift编程中的初始化过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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