实例引用真的可以在Swift中使用吗?

时间:2023-01-23 18:06:45

I wrote the Objective-C code first

我首先编写了Objective-C代码

NSMutableString *aStrValue = [NSMutableString stringWithString:@"Hello"];
NSMutableDictionary *aMutDict = [NSMutableDictionary dictionary];
[aMutDict setObject:aStrValue forKey:@"name"];

NSLog(@"Before %@",aMutDict);
[aStrValue appendString:@" World"];
NSLog(@"After %@",aMutDict);

I got the output as follows

我得到的输出如下

2015-09-17 14:27:21.052 ShareIt[4946:129853] Before {
    name = Hello;
}
2015-09-17 14:27:21.057 ShareIt[4946:129853] After {
    name = "Hello World";
}

Means when I append a string to a Mutable string which is actually referred into a MutableDictionary, the change is getting reflected in Dictionary too..

意味着当我将一个字符串附加到Mutable字符串(实际上被称为MutableDictionary)时,该变化也会在字典中反映出来。

But then I tried something same in Swift

但后来我在Swift尝试了同样的东西

var stringValue:String?
stringValue = "Hello"

var dict:Dictionary = ["name":stringValue!]
println(dict)
stringValue! += " World"
stringValue!.extend(" !!!!")
println(dict)

I seen the output in playground like this 实例引用真的可以在Swift中使用吗?

我在游乐场看到了这样的输出

My Questions are

我的问题是

  • Why the value that changed is not reflecting in a data structure like Dictionary.
  • 为什么更改的值不会反映在像Dictionary这样的数据结构中。

  • Does in Swift adding any key value really keeps the value or its reference, if it's keeping the reference like objective-C then here what is my mistake?
  • 在Swift中添加任何键值是否真的保留了值或其引用,如果它保持像Objective-C那样的引用那么这是我的错误?

3 个解决方案

#1


4  

Reference type

The different behaviours depends on the fact that in the Objective-C code you use NSMutableString that is a class. This means that aMutDict and aStrValue are references to the same object of type NSMutableString. So the changes you apply using aStrValue are visibile by aMutDict.

不同的行为取决于在Objective-C代码中使用NSMutableString这一类的事实。这意味着aMutDict和aStrValue是对NSMutableString类型的同一对象的引用。因此,使用aStrValue应用的更改是由aMutDict可见的。

Value type

On the other hand in Swift you are using the String struct. This is a value type. This means that when you copy the value from one variable to another, the change you do using the first variable are not visible to the second one.

另一方面,在Swift中,您使用的是String结构。这是一种值类型。这意味着当您将值从一个变量复制到另一个变量时,使用第一个变量执行的更改对第二个变量不可见。

The following example clearly describes the value type behaviour:

以下示例清楚地描述了值类型行为:

var word0 = "Hello"
var word1 = word0

word0 += " world" // this will NOT impact word1

word0 // "Hello world"
word1 // "Hello"

Hope this helps.

希望这可以帮助。

#2


4  

Strings in Swift (copy by value) are completely different than string in Objective C (copy by reference).

Swift中的字符串(按值复制)与Objective C中的字符串完全不同(通过引用复制)。

From Apple' Swift documentation:

来自Apple的Swift文档:

Strings Are Value Types

字符串是值类型

Swift’s String type is a value type. If you create a new String value, that String value is copied when it is passed to a function or method, or when it is assigned to a constant or variable. In each case, a new copy of the existing String value is created, and the new copy is passed or assigned, not the original version. Value types are described in Structures and Enumerations Are Value Types.

Swift的String类型是一种值类型。如果创建新的String值,则在将String值传递给函数或方法时,或者将其赋值给常量或变量时,将复制该String值。在每种情况下,都会创建现有String值的新副本,并传递或分配新副本,而不是原始版本。结构和枚举是值类型中描述了值类型。

Swift’s copy-by-default String behavior ensures that when a function or method passes you a String value, it is clear that you own that exact String value, regardless of where it came from. You can be confident that the string you are passed will not be modified unless you modify it yourself.

Swift的默认复制String行为确保当函数或方法向您传递String值时,很明显您拥有该精确的String值,无论它来自何处。除非您自己修改,否则您可以确信传递的字符串不会被修改。

Behind the scenes, Swift’s compiler optimizes string usage so that actual copying takes place only when absolutely necessary. This means you always get great performance when working with strings as value types.

在幕后,Swift的编译器优化了字符串的使用,因此只有在绝对必要时才会进行实际复制。这意味着在使用字符串作为值类型时,您总能获得出色的性能。

#3


2  

In swift, String is a Struct. Structs are not reference types in Swift, thus it's copied when you setting it to a dictionary.

在swift中,String是一个Struct。结构不是Swift中的引用类型,因此在将其设置为字典时会复制它。

#1


4  

Reference type

The different behaviours depends on the fact that in the Objective-C code you use NSMutableString that is a class. This means that aMutDict and aStrValue are references to the same object of type NSMutableString. So the changes you apply using aStrValue are visibile by aMutDict.

不同的行为取决于在Objective-C代码中使用NSMutableString这一类的事实。这意味着aMutDict和aStrValue是对NSMutableString类型的同一对象的引用。因此,使用aStrValue应用的更改是由aMutDict可见的。

Value type

On the other hand in Swift you are using the String struct. This is a value type. This means that when you copy the value from one variable to another, the change you do using the first variable are not visible to the second one.

另一方面,在Swift中,您使用的是String结构。这是一种值类型。这意味着当您将值从一个变量复制到另一个变量时,使用第一个变量执行的更改对第二个变量不可见。

The following example clearly describes the value type behaviour:

以下示例清楚地描述了值类型行为:

var word0 = "Hello"
var word1 = word0

word0 += " world" // this will NOT impact word1

word0 // "Hello world"
word1 // "Hello"

Hope this helps.

希望这可以帮助。

#2


4  

Strings in Swift (copy by value) are completely different than string in Objective C (copy by reference).

Swift中的字符串(按值复制)与Objective C中的字符串完全不同(通过引用复制)。

From Apple' Swift documentation:

来自Apple的Swift文档:

Strings Are Value Types

字符串是值类型

Swift’s String type is a value type. If you create a new String value, that String value is copied when it is passed to a function or method, or when it is assigned to a constant or variable. In each case, a new copy of the existing String value is created, and the new copy is passed or assigned, not the original version. Value types are described in Structures and Enumerations Are Value Types.

Swift的String类型是一种值类型。如果创建新的String值,则在将String值传递给函数或方法时,或者将其赋值给常量或变量时,将复制该String值。在每种情况下,都会创建现有String值的新副本,并传递或分配新副本,而不是原始版本。结构和枚举是值类型中描述了值类型。

Swift’s copy-by-default String behavior ensures that when a function or method passes you a String value, it is clear that you own that exact String value, regardless of where it came from. You can be confident that the string you are passed will not be modified unless you modify it yourself.

Swift的默认复制String行为确保当函数或方法向您传递String值时,很明显您拥有该精确的String值,无论它来自何处。除非您自己修改,否则您可以确信传递的字符串不会被修改。

Behind the scenes, Swift’s compiler optimizes string usage so that actual copying takes place only when absolutely necessary. This means you always get great performance when working with strings as value types.

在幕后,Swift的编译器优化了字符串的使用,因此只有在绝对必要时才会进行实际复制。这意味着在使用字符串作为值类型时,您总能获得出色的性能。

#3


2  

In swift, String is a Struct. Structs are not reference types in Swift, thus it's copied when you setting it to a dictionary.

在swift中,String是一个Struct。结构不是Swift中的引用类型,因此在将其设置为字典时会复制它。