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

前端 未结 4 1664
时光说笑
时光说笑 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条回答
  •  时光取名叫无心
    2020-12-04 15:07

    Adding to @Krzak's excellent answer, this is why

    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'

    and

    print(Array()[1])  // Halts with "Thread 1: Fatal error: Index out of range"
    

    This different handling of the error helped me understanding the difference..... e

提交回复
热议问题