- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
在 Swift 中有数百种使用协议(protocol)和 Self
来伪造存在性的解决方案,但它们大多指的是 Swift 2 和 Swift 3 可能带来的光明 future ......现在 Swift 4 已经出来了,对泛型进行了很好的补充。但我找不到任何建议如何将其放入缺失的存在问题中。
关于如何以 Swift 4 方式解决这个问题有什么想法吗?
例子:
import UIKit
protocol Bla {
func compare(other: Self)
}
extension CGFloat : Bla {
func compare(other: CGFloat) {
print("Extended CGFloat")
}
}
extension UIEdgeInsets : Bla {
func compare(other: UIEdgeInsets) {
print("Extended UIEdgeInsets")
}
}
/* Possible, but what if we want to CGFloat _and_ UIEdgeInsets inside here?
Well, that would _not_ work! */
class Possible<T: Bla> {
var array: [T]!
}
/* This is open to everything...
And therefore fails to compile, no dynamic type info at runtime I guess. */
class Fail {
var array: [Bla]!
}
// Works, but I don't need that.
let list = Possible<CGFloat>()
// I need that:
let list = Fail()
let f: CGFloat = 1.23
let edge = UIEdgeInsets()
list.array.append(f)
list.array.append(edge)
最佳答案
从根本上说,这是做不到的。如果你可以:
class Fail {
var array: [Bla]!
}
然后您可以尝试编写如下代码:
func compareAll(foo: Fail)
{
for x in foo.array
{
x.compare(other: y)
}
}
y
是什么类型?该协议(protocol)规定它必须与采用该协议(protocol)的对象的类型相同,但您直到运行时才知道 x
的类型。无法编写 y 同时为 UIEdgeInsets
和 CGFloat
的代码。
我认为您可以通过使 compare
通用化来消除对 Self
的依赖,从而使其工作。您的协议(protocol)将如下所示:
protocol Bla {
func compare<T: Bla>(other: T)
}
compare
的实现必须测试 other
的类型并转换为正确的类型。
extension CGFloat: Bla
{
func compare<T: Bla>(other: T)
{
if let casted = other as? CGFloat
{
// do whatever
}
}
}
我认为这种方法比使用类型删除(参见 Daniel Hall 的回答)更好,原因如下:
compare
的间接访问中。 当 other
是任意类型时,compare
函数可以工作,而不仅仅是相同类型的 self
如果有意义的话例如
func compare<T: Bla>(other: T)
{
if let casted = other as? CGFloat
{
// do whatever
}
else if let casted = other as? UIEdgeInsets
{
// Do something else
}
}
当然,如果只是给定协议(protocol)而您无法更改它,类型删除是您唯一的选择。
关于Swift 4 使用协议(protocol)和 `Self` 伪造存在的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48074674/
我对 FakeItEasy(或其他模拟对象,因为我相信它们非常相似)有疑问。以下是我的 puesdocode: public class Service { public void Check
我想使用鼠标/键盘伪造操作(或触摸)事件。当我尝试使用以下方法引发事件时: RoutedEventArgs e = new RoutedEventArgs(ManipulationStartedEve
出于某种原因,我的 SVN 存储库的本地副本停止将父目录识别为工作副本。我通常会通过再次 checkout 到另一个文件夹并用我更改的文件覆盖新的工作副本来解决此问题。然后我会从新文件夹中进行提交。
我正在尝试设置我的开发实例,以便我可以伪造一些我拥有的网址。我的网站有许多网址,根据您访问的网址,我的网站的行为会因域的不同而有所不同。 我编辑了我的 C:\WINDOWS\system32\driv
当用户登录并选中“记住我”框时,我会为它生成一个 key (md5 上非常随机的数字)并保存在它的 cookie 上。如果用户未登录,我的代码会检查“记住我的 key ”cookie,如果它与用户匹配
有没有办法强制 Oracle 也“看到”一个表和相关索引比它们实际更大? 换句话说,有没有办法“伪造”数据库统计信息,因此基于成本的优化器会在几乎为空的数据库上做出决策,这更接近于在真实的大型生产数据
这是我使用 tsqlt 的第一天,所以你可能会看到一些含糊的陈述。 我正在尝试测试一个具有 Try Catch Block 的存储过程,但测试中的实际语句是插入和更新命令。 现在我想测试如果出现 Er
我从mockito开始,想知道如何假装添加观察者。我想编写一个测试来确保观察者计数在函数调用后增加。 示例测试代码: MyClassUnderTest instance = new MyClassUn
我是一名 C# 游戏开发人员,我有一个安全功能,我的服务器动态创建一个包含一些 key 的 DLL,并将这个 DLL 上传到 amazon s3,然后向人们提出挑战随机的。当客户收到此质询时,他们有
我正在尝试“伪造”一个 Canvas ,目的是将这个伪造的 Canvas 交给一个可能是任意的框架,以对所有直线、曲线和 moveTo 进行后处理。 为了解决这个问题,我尝试了这段代码,它确实有效,但
我的应用程序需要 SQL Server 2000 作为数据库存储。我真的不想使用 SQL Server 2000,但我可以改用 MySQL Server。 应用程序使用 ODBC 连接到 SQL Se
我有一个下拉菜单,需要一个带有左右边距的滚动条。我正在使用-webkit-scrollbar,但据我所知,它只支持沿滚动轴的边距,所以我一直在用容器内元素的右边距来近似水平边距,并在外部 div,如您
作为我学生小组业余项目的一部分,我正在创建微 Controller 有线网络的模拟,以测试我们编写的算法。每个 Controller 都连接到多个数据端口,每个端口都有一个输入和输出流。我通过给每个端
我已经在 Forge 中安装了自定义 SSL 证书。现在我的网站宕机了 -_-。 Site is not available connection refused 我已经重新启动了我的服务器,但没有任
我正在开发具有 ListView 和详细 View 的应用程序,并且我从 ListView 到详细 View 设置动画。在执行此操作时,我想在某个阶段隐藏状态栏(最好同时在后台显示 ListView
我想用它在 MS-Test 单元测试中伪造 System.Net.Mail.SmtpClient。为此,我添加了一个 System.dll 的 Fakes Assembmly。然后我创建一个 Shim
在我的 Playframework 2.4 项目中,我有这样的方法: public static Result resetValue(int client) { String received
这是我渲染场景的过程: 绑定(bind) MSAA x4 GBuffer(4 种颜色附件、位置、法线、颜色和无光照颜色(仅天空盒。我还有一个深度组件/纹理)。 绘制天空盒 绘制地理 将所有颜色和深度分
我不太确定 $_SESSION 在 PHP 中是如何工作的。我假设它是浏览器上的 cookie 与服务器上的唯一 key 匹配。是否可以伪造并绕过仅使用 session 来识别用户的登录。 如果 $_
大家好,我是沙漠尽头的狼。 本文首发于 Dotnet9 ,介绍使用 Lib.Harmony 库拦截第三方 .NET 库方法,达到不修改其源码并能实现修改方法逻辑、预期行为的效果,并
我是一名优秀的程序员,十分优秀!