Conflicting definition of Swift struct and array

后端 未结 6 1029
孤街浪徒
孤街浪徒 2021-02-01 09:56

In Swift Programming Language, it says:

  1. “all of the basic types in Swift—integers, floating-point numbers, Booleans, strings, arrays and dictio

6条回答
  •  悲哀的现实
    2021-02-01 10:53

    Array is indeed a struct, a value type. However, they went into great lengths to make it behave like a reference type (special copy behaviour, subscripts can modify even constant arrays, they even defined === operator especially for arrays!).

    This was probably done because of performance reasons but the resulting behavior is buggy and hard to work with.

    To quote Apple (https://devforums.apple.com/thread/228695)

    Array semantics were in flux at the time of Beta 1, and have been revised to provide full value semantics like Dictionary and String. This will be available in later betas.

    -Chris

    So, we can expect the Array behavior to change greatly in future betas.

提交回复
热议问题