- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
威尔String(describing: MyNSObject.self)
总是返回“MyNSObject”?
一些开发者写道:
// identifier is "MyViewController"
let controller = storyboard.instantiateViewController(withIdentifier: String(describing: MyViewController.self)) as! MyViewController
// identifier is "MyTableViewCell"
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: MyTableViewCell.self), for: indexPath) as! MyTableViewCell
目前有效,因为使用 String(describing: MyNSObject.self)
正在返回 "MyNSObject"
.
但是我们是处于已定义行为还是未定义行为的情况下? public init<Subject>(describing instance: Subject)
的文档阅读:
/// - 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.
而且我担心我们可能处于未指定结果场景中,不是吗?
注意 1:我们的实际实现是使用 protocol
实现接近 static var identifier: String { return String(describing: Self.self) }
的扩展.
注2:NSStringFromClass(Self.self as AnyClass).substring(from: NSStringFromClass(Self.self as AnyClass).range(of: ".")!.upperBound)
可能是 String(describing: Self.self)
的潜在定义明确的等价物, 但它看起来真的很丑。
注意 3:甚至覆盖 description
和 debugDescription
的 MyViewController
和 MyTableViewCell
显然不足以改变 String(describing: Self.self)
.
目前使用 Swift 3.1,并将在可用时迁移到 Swift 4。
最佳答案
我现在能记得的唯一描述此行为的官方文档是在 Xcode 发行说明中找到的:
Swift Enhancements and Changes
...
Type names and
enum
cases nowString
without qualification by default.debugPrint
orString(reflecting:)
can still be used to get fully qualified names. For example:enum Fruit { case Apple, Banana, Strawberry }
print(Fruit.Apple) // “Apple”
debugPrint(Fruit.Apple) // “MyApp.Fruit.Apple”)(21788604)
(这在 Xcode 7.0 的部分描述了 Swift 1.2 到 2.0 的增强和变化。所以,convert to String
意味着使用 String。 init(_:)
,现在是 String.init(describing:)
.)
因此,Swift 的一些核心成员意识到了这种有意更改的行为。如果 Swift 团队会更改它,我希望在 swift.org 中进行一些讨论或至少发布一些公告。
关于ios - `String(describing: MyNSObject.self)` 总是返回 "MyNSObject"吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43222505/
让我们写一个简单的类在我的脑海中解释: class SomeClass { var happyToUsed = 10 } 并创建一个对象 let someObject = SomeClass(
采用 self 的方法与采用 &self 甚至 &mut self 的方法有什么区别? 例如 impl SomeStruct { fn example1(self) { } fn ex
请观察以下代码(Win10上的python 3.6,PyCharm),函数thread0(self)作为线程成功启动,但是 thread1(self)似乎与thread0(self)不同已设置。 se
backbone.js 开始于: //Establish the root object, `window` (`self`) in the browser, or `global` on the s
做的事: self = self.init; return self; 在 Objective-C 中具有相同的效果: self.init() 快速? 例如,在这种情况下: else if([form
我查看了关于堆栈溢出的一些关于使用[weak self]和[unowned self]的问题的评论。我需要确保我理解正确。 我正在使用最新的 Xcode - Xcode 13.4,最新的 macOS
我面临在以下模型类代码中的 self.init 调用或分配给 self 之前使用 self 的错误tableview单元格项目,它发生在我尝试获取表格单元格项目的文档ID之后。 应该做什么?请推荐。
晚上好。 我对在 Swift 中转义(异步)闭包有疑问,我想知道哪种方法是解决它的最佳方法。 有一个示例函数。 func exampleFunction() { functionWithEsca
我需要在内心深处保持坚强的自我。 我知道声明[weak self]就够了外封闭仅一次。 但是guard let self = self else { return }呢? ,是否也足以为外部闭包声明一
代码 use std::{ fs::self, io::self, }; fn rmdir(path: impl AsRef) -> io::Result { fs::remo
我检查了共享相同主题的问题,但没有一个解决我遇到的这种奇怪行为: 说我有一个简单的老学校struct : struct Person { var name: String var age:
我应该解释为什么我的问题不是重复的:TypeError: can only concatenate list (not “str”) to list ...所以它不是重复的,因为该帖子处理代码中出现的
我有一个 trait,它接受一个类型参数,我想说实现这个 trait 的对象也会符合这个类型参数(使用泛型,为了 Java 的兼容性) 以下代码: trait HandleOwner[SELF
这个问题在这里已经有了答案: Why would a JavaScript variable start with a dollar sign? [duplicate] (16 个答案) 关闭 8
我总是找到一些类似的代码newPromise.promiseDispatch.apply(newPromise, message),我不明白为什么不使用newPromise.promiseDispat
我看到类似的模式 def __init__(self, x, y, z): ... self.x = x self.y = y self.z = z ... 非
mysql查询示例: SELECT a1.* FROM agreement a1 LEFT JOIN agreement a2 on a1.agreementType = a2.agreementTy
self.delegate = self; 这样做有什么问题吗?正确的做法是什么? 谢谢,尼尔。 代码: (UITextField*)initWith:(id)sender:(float)X:(flo
为什么要声明self在类中需要的结构中不需要?我不知道是否还有其他例子说明了这种情况,但在转义闭包的情况下,确实如此。如果闭包是非可选的(因此是非转义的),则不需要声明 self在两者中的任何一个。
这个问题已经有答案了: What does the ampersand (&) before `self` mean in Rust? (1 个回答) 已关闭去年。 我不清楚 self 之间有什么区别
我是一名优秀的程序员,十分优秀!