V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
limon
V2EX  ›  Swift

传数组,然后发现被坑了

  •  
  •   limon · 2014-06-06 23:38:22 +08:00 · 5459 次点击
    这是一个创建于 3833 天前的主题,其中的信息可能已经有所发展或是发生改变。
    例子如下:
    var a = [1, 2]
    var b = a

    a[0] = 0
    println("a = \(a), b = \(b)")

    a.append(3) # or a+=3
    println("a = \(a), b = \(b)")

    输出:
    a = [0, 2], b = [0, 2] #改了下a,还是同一对象
    a = [0, 2, 3], b = [0, 2] #呀,分道扬镳了= =!

    ===================================
    ruby之类
    a = [1, 2]
    b = a

    a[0] = 0
    print a, b, "\n"

    a.push(3)
    print a, b, "\n"
    输出:
    [0, 2][0, 2]
    [0, 2, 3][0, 2, 3]

    insert,remove也类似,应该这里是建立了一个新对象。和ruby,python,js的直觉不一样啊 = =
    17 条回复    2014-06-07 22:21:13 +08:00
    sojingle
        1
    sojingle  
       2014-06-07 00:00:35 +08:00
    “A value type is a type that is copied when it is assigned to a variable or constant, or when it is passed to a function.

    You’ve actually been using value types extensively throughout the previous chapters. In fact, all of the basic types in Swift—integers, floating-point numbers, Booleans, strings, arrays and dictionaries—are value types, and are implemented as structures behind the scenes.”

    Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/cn/jEUH0.l

    Swift—integers, floating-point numbers, Booleans, strings, arrays and dictionaries 这些都是值类型...
    txx
        2
    txx  
       2014-06-07 00:02:57 +08:00
    請看 wwdc 402就知道了 struct 是個 value not ref每次都是 copy 的
    gluttony
        3
    gluttony  
       2014-06-07 00:03:51 +08:00
    The Swift Programming Language 明确说明了数组的这种行为:

    If you assign an Array instance to a constant or variable, or pass an Array instance as an argument to a function or method call, the contents of the array are not copied at the point that the assignment or call takes place. Instead, both arrays share the same sequence of element values. When you modify an element value through one array, the result is observable through the other.

    For arrays, copying only takes place when you perform an action that has the potential to modify the length of the array. This includes appending, inserting, or removing items, or using a ranged subscript to replace a range of items in the array. If and when array copying does take place, the copy behavior for an array’s contents is the same as for a dictionary’s keys and values, as described in Assignment and Copy Behavior for Dictionaries.

    https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/ClassesAndStructures.html#//apple_ref/doc/uid/TP40014097-CH13-XID_109
    exoticknight
        4
    exoticknight  
       2014-06-07 00:04:47 +08:00
    如果需要ref怎么办……
    yelite
        5
    yelite  
       2014-06-07 00:07:05 +08:00
    var b = a
    这个时候 b 和 a 是不同的 array,只是 array 内部指向的数据是同样的?
    limon
        6
    limon  
    OP
       2014-06-07 00:25:20 +08:00
    多谢楼上几位,刚才看到Collection,Class部分还没读,现在明白了。Array是值传递,但是估计为了减少copy,只有在改变长度的时候才做copy的动作,所以一开始下标访问还是指向同一个地方,不过这个行为不知道真的要中招。。。
    limon
        7
    limon  
    OP
       2014-06-07 00:26:20 +08:00
    另外,我发现不光是改变长度才会copy,用range改变内容也会copy,即使长度没变。
    limon
        8
    limon  
    OP
       2014-06-07 00:29:30 +08:00
    @exoticknight 我也在想这个,先慢慢往下读。。。
    ultragtx
        9
    ultragtx  
       2014-06-07 01:51:58 +08:00
    可以继续用NSMutableArray/NSArray
    Livid
        10
    Livid  
    MOD
       2014-06-07 02:15:45 +08:00
    Python 的是这样的:

    >>> a = [1,2]
    >>> b = a
    >>> a[0] = 0
    >>> a
    [0, 2]
    >>> b
    [0, 2]
    >>> a.append(3)
    >>> a
    [0, 2, 3]
    >>> b
    [0, 2, 3]
    manfay
        11
    manfay  
       2014-06-07 08:47:20 +08:00   ❤️ 1
    一直觉得Python真是把这些细节整理得很清晰,所有变量全是reference,所有数组、字典、函数甚至数字都是object,因此不管变量a指向什么类型,b = a 之后,b is a就为True。
    coosir
        12
    coosir  
       2014-06-07 09:25:29 +08:00 via iPad
    @limon 应该理解为指针和range的同时传递吧。
    limon
        13
    limon  
    OP
       2014-06-07 09:42:24 +08:00 via iPad
    @coosir 什么意思,没明白。 我是指a[0...1]=[3,4]这样的情况,也会发生copy。
    limon
        14
    limon  
    OP
       2014-06-07 09:47:34 +08:00 via iPad
    @Livid 嗯,其他语言都没这种特性。 我觉得这个设计并不好,可能重构的时候修改了前面的代码影响了后面的赋值。
    lxfxf
        15
    lxfxf  
       2014-06-07 10:06:40 +08:00
    书里面讲的很清楚...unshare,或者copy过去。如果不是改变数组长度的操作,都是reference。
    limon
        16
    limon  
    OP
       2014-06-07 10:23:16 +08:00
    @lxfxf 哦 谢了
    leedstyh
        17
    leedstyh  
       2014-06-07 22:21:13 +08:00
    Go也是这样,你了解了底层实现后,就知道这不是坑了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3523 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 10:55 · PVG 18:55 · LAX 02:55 · JFK 05:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.