in Swift: Difference between Array VS NSArray VS [AnyObject]

前端 未结 4 1666
时光说笑
时光说笑 2020-12-04 14:17

As the title says, what\'s the difference between Array vs NSArray vs [AnyObject]?

Also, what is most recommended way of approaching this. What i mean recommended is

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 15:15

    Using the Krzak answer, here is a practical example:

    // Let´s create an Array as a struct showing alternative ways
        var arrStruct = ["Pencil", "Eraser", "Notebook"]
        // or var arrStruct: [String] = ["Pencil", "Eraser", "Notebook"]
        // or var arrStruct: Array = ["Pencil", "Eraser", "Notebook"]
        // or var arrStruct = Array(["Pencil", "Eraser", "Notebook"])
        // All this alternative ways create an array as struct
    
        // Now let´s create a function that modifies this array struct
        func modifyArr(alternativeArr: [String]) 
        // or func modify(alternativeArr: Array)
        {
            alternativeArr[2] = "Pen" // compilation error
            // This won´t work. In swift >= 3.0 all func parametes are a let variable, 
            // this means alternativeArr is defined as a let. What one has to do is 
            // create a local variable and copy the value.
    
            var localAlternativeArr = alternativeArr
            // or var localAlternativeArr: [String] = alternativeArr
            // or var localAlternativeArr: Array = alternativeArr
    
            // now we can change it.
            localAlternativeArr[2] = "Pen"
            print(localAlternativeArr) // ["Pencil", "Eraser", "Pen"]
            print(alternativeArr) // ["Pencil", "Eraser", "Notebook"]
        }
    
        modifyArr(alternativeArr: arrStruct)
        print(arrStruct) // ["Pencil", "Eraser", "Notebook"]
    
        // Since the arrStruct is a struct every time we assign to another variable or 
        // pass it as a func argument a copy is made.
    
    
    
    // Now let´s create as an NSMutableArray
        var arrClass: NSMutableArray = ["Pencil", "Eraser", "Notebook"]
        // or var arrStruct = NSMutableArray(array: ["Pencil", "Eraser", "Notebook"])
        // All this create an NSMutableArray as a class
    
        // Now let´s create a function that modifies this array struct
        func modifyArr(alternativeArr: NSMutableArray)
        {
            alternativeArr[2] = "Pen"
            print(alternativeArr)
            // (
            //   Pencil,
            //   Eraser,
            //   Pen
            // )
        }
    
        modifyArr(alternativeArr: arrClass)
        print(arrClass)
        // (
        //   Pencil,
        //   Eraser,
        //   Pen
        // )
    
        // Since the arrClass is a class everytime we assign to another variable or 
        // pass it as a func argument is passed by reference. Means that any change
        // inside modifyArr is going to change the arrClass outside. The change 
        // is made in the same pointer.
    

提交回复
热议问题