Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios print()或字符串插值如何计算不带';t实现自定义字符串转换_Ios_Swift - Fatal编程技术网

Ios print()或字符串插值如何计算不带';t实现自定义字符串转换

Ios print()或字符串插值如何计算不带';t实现自定义字符串转换,ios,swift,Ios,Swift,我做了一些工作,包括获取动态类型的字符串表示,这让我好奇它是如何工作的,以及为什么不同的值在调用不同时会打印出来。为什么会发生这种情况?这些值来自哪里 class TempClass {} print(TempClass()) // [Module].TempClass print(type(of: TempClass()) // TempClass print(TempClass.self) // TempClass print(TempClass().self

我做了一些工作,包括获取动态类型的字符串表示,这让我好奇它是如何工作的,以及为什么不同的值在调用不同时会打印出来。为什么会发生这种情况?这些值来自哪里

class TempClass {}

print(TempClass())          // [Module].TempClass
print(type(of: TempClass()) // TempClass
print(TempClass.self)       // TempClass
print(TempClass().self)     // [Module].TempClass
在类或类的实例上几乎没有自动完成(只是
self

我希望名称作为
字符串
变量,这似乎很奇怪:

// works
let name: String = "\(type(of: TempClass())"

// error: initializer 'init(_:)' requires that 'TempClass.Type' conform to 'LosslessStringConvertible'
let name: String = String(type(of: TempClass())

// error: type 'TempClass' has no member 'description'
let name: String = type(of: TempClass()).description

// error: type 'TempClass' has no member 'debugDescription'
let name: String = type(of: TempClass()).debugDescription

有人知道这里发生了什么吗?

我认为它调用了
字符串(description:)
,因为它给出了相同的值,文档中说

/// Use this initializer to convert an instance of any type to its preferred
/// representation as a `String` instance. The initializer creates the
/// string representation of `instance` in one of the following ways,
/// depending on its protocol conformance:
///
/// - If `instance` conforms to the `TextOutputStreamable` protocol, the
///   result is obtained by calling `instance.write(to: s)` on an empty
///   string `s`.
/// - If `instance` conforms to the `CustomStringConvertible` protocol, the
///   result is `instance.description`.
/// - If `instance` conforms to the `CustomDebugStringConvertible` protocol,
///   the result is `instance.debugDescription`.
/// - An unspecified result is supplied automatically by the Swift standard
///   library.

我想我们只是碰到了最后一行中提到的“未指定的结果”。

我认为它调用了
字符串(description:)
,因为它给出了相同的值,文档中说

/// Use this initializer to convert an instance of any type to its preferred
/// representation as a `String` instance. The initializer creates the
/// string representation of `instance` in one of the following ways,
/// depending on its protocol conformance:
///
/// - If `instance` conforms to the `TextOutputStreamable` protocol, the
///   result is obtained by calling `instance.write(to: s)` on an empty
///   string `s`.
/// - If `instance` conforms to the `CustomStringConvertible` protocol, the
///   result is `instance.description`.
/// - If `instance` conforms to the `CustomDebugStringConvertible` protocol,
///   the result is `instance.debugDescription`.
/// - An unspecified result is supplied automatically by the Swift standard
///   library.

我想我们只是达到了最后一行中提到的“未指定结果”。

Swift标准库和编译器对不适合像我们这样的凡人的信息是保密的。假设编译器知道程序中所有类型的信息,它可以生成一些假设的
\u privateSynthesistedStringConvertible
协议的实现。无论它以何种方式,它都不是一个公开的机制。请添加您的“编辑:”作为这个问题的答案!Swift标准库和编译器对不适合我们这样的凡人的信息是保密的。假设编译器知道程序中所有类型的信息,它可以生成一些假设的
\u privateSynthesistedStringConvertible
协议的实现。无论它以何种方式,它都不是一个公开的机制。请添加您的“编辑:”作为这个问题的答案!