简体   繁体   English

[Classname]() 和 Classname() 之间有什么区别?

[英]what is difference between [Classname]() and Classname() in swift?

I want to know difference between我想知道两者之间的区别

    var arr = ExamCategory() 
             &
    var arrExamCategories = [ExamCategory]()

Please Why this is declared like this and which should be use for better?请问为什么这样声明,应该使用哪个更好?

My ExamCategory class is like this.我的ExamCategory类是这样的。

class ExamCategory: NSObject {

    var CategoryId: Int?
    var CategoryName:String?
    var TotalSection: Int?
    var ImageUrl: String?

    override init() {

    }

    init(dict:[String:Any]) {
        super.init()

        CategoryId = dict["categoryId"] as? Int
        CategoryName = dict["categoryName"] as? String
        TotalSection = dict["totalSection"] as? Int
       ImageUrl = dict[""] as? String
    }
  .....And So On
}

Can anyone describe and differentiate both in details?谁能详细描述和区分两者?

Both have different usage, they cannot be compared with each other.两者用途不同,不能相互比较。 When you need to store multiple elements, you should use array but if you need single element you should choose single default initiazer.当您需要存储多个元素时,您应该使用数组,但如果您需要单个元素,您应该选择单个默认启动器。 It depends upon your need.这取决于您的需要。 Both are best according to their usage in swift programming.根据它们在 swift 编程中的用法,两者都是最好的。

ExamCategory() is a default initializer for your class which invokes init() for your class when called using classname with round backets () and returns you an instance of your class. ExamCategory()是您的类的默认初始值设定项,它在使用 classname 和 round backets () 调用时为您的类调用init() () 并返回您的类的实例。

In your example ExamCategory() invokes initializer for class ExamCategory and return you a single instance of the class, which your have stored in var arr在您的示例中, ExamCategory()调用类ExamCategory初始化ExamCategory并返回该类的单个实例,该实例已存储在var arr

var arr = ExamCategory() 

while...尽管...
[ExamCategory]() is an array initializer of type ExamCategory . [ExamCategory]()是一个类型为ExamCategory的数组初始值ExamCategory It (can/must) holds/stores an instance of class ExamCategory or its sub classes.它(可以/必须)持有/存储类ExamCategory或其子类的实例。

In your example, [ExamCategory]() is an array for elements of ExamCategory .在您的示例中, [ExamCategory]()是一个包含ExamCategory元素的ExamCategory You can insert/add, above var arr in this array.您可以在此数组中的var arr上方插入/添加。

var arrExamCategories = [ExamCategory]()
arrExamCategories.append(arr)

Here is apple document for Initialization and array (Collection Types)这是初始化数组(集合类型)的苹果文档

var arr = ExamCategory() 

It means that: ExamCategory class has been initialized and reference of this class has been stored in arr variable.这意味着: ExamCategory 类已被初始化,并且该类的引用已存储在 arr 变量中。

var arrExamCategories = [ExamCategory]()

It means that: You have initialize an array which contains objects of ExamCategory class.这意味着:您已经初始化了一个包含 ExamCategory 类对象的数组。

ex: arrExamCategories.append(arr) // var arr = ExamCategory()

For more details please refer apple's swift documentation.有关更多详细信息,请参阅苹果的 swift 文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM