ios 在 Swift 中:Array VS NSArray VS [AnyObject] 之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28889705/
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
in Swift: Difference between Array VS NSArray VS [AnyObject]
提问by shle2821
As the title says, what's the difference between Array vs NSArray vs [AnyObject]?
正如标题所说,Array vs NSArray vs [AnyObject] 之间有什么区别?
Also, what is most recommended way of approaching this. What i mean recommended is, what's the easiest implementation. Thank you.
此外,什么是最推荐的方法来解决这个问题。我的意思是推荐的是,什么是最简单的实现。谢谢你。
回答by Krzak
Array
is a struct, therefore it is a value typein Swift.
NSArray
is an immutable Objective C class, therefore it is a reference typein Swift and it is bridged to Array<AnyObject>
.
NSMutableArray
is the mutable subclass of NSArray
.
Array
是struct,因此它是Swift 中的值类型。
NSArray
是一个不可变的 Objective C类,因此它是Swift 中的引用类型,并且它被桥接到Array<AnyObject>
.
NSMutableArray
是 的可变子类NSArray
。
var arr : NSMutableArray = ["Pencil", "Eraser", "Notebook"]
var barr = ["Pencil", "Eraser", "Notebook"]
func foo (var a : Array<String>)
{
a[2] = "Pen"
}
func bar (a : NSMutableArray)
{
a[2] = "Pen"
}
foo(barr)
bar(arr)
println (arr)
println (barr)
Prints:
印刷:
(
Pencil,
Eraser,
Pen
)
[Pencil, Eraser, Notebook]
Because foo
changes the local value of a
and bar
changes the reference.
It will also work if you do let arr
instead of var
as with other reference types.
因为foo
改变了局部值a
和bar
改变了引用。如果您这样做let arr
而不是var
与其他引用类型一样,它也将起作用。
回答by David Berry
Array
is a Swift construct, and generic struct, which means that it can be an array of any specific type (Int, String, AnyObject, etc.)
Array
是一个 Swift 结构和泛型结构,这意味着它可以是任何特定类型(Int、String、AnyObject 等)的数组
[T]
is syntactic sugar for Array<T>
[T]
是语法糖 Array<T>
AnyObject
is an object of any class, including Objective-C classes.
AnyObject
是任何类的对象,包括 Objective-C 类。
NSArray
is an Objective-C construct that can hold any Objective-C object and is transparently mapped to and from Array<AnyObject>
NSArray
是一个 Objective-C 构造,可以容纳任何 Objective-C 对象,并且透明地映射到和从 Array<AnyObject>
回答by lopes710
Using the Krzak answer, here is a practical example:
使用 Krzak 的答案,这是一个实际示例:
// 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<String>)
{
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.
回答by eharo2
Adding to @Krzak's excellent answer, this is why
添加到@Krzak 的优秀答案中,这就是原因
print(NSArray().object(at: 1)) // Triggers an UnmanagedException
2018-11-09 11:38:08.798088-0600 AppName[38786:10497909] * Terminating app due to uncaught exception 'NSRangeException', reason: '*-[__NSArray0 objectAtIndex:]: index 1 beyond bounds for empty NSArray'
2018-11-09 11:38:08.798088-0600 AppName[38786:10497909] * 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“*-[__NSArray0 objectAtIndex:]:索引 1 超出空 NSArray 的界限”
and
和
print(Array<Int>()[1]) // Halts with "Thread 1: Fatal error: Index out of range"
This different handling of the error helped me understanding the difference..... e
这种对错误的不同处理帮助我理解了差异..... e