问题标签 [swift-protocols]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
488 浏览

swift - 如何在 Swift 中的泛型扩展中使用带有可选类方法的协议?

我正在尝试使用具有类方法的现有类的扩展,例如:

我在一个通用的扩展中使用这个协议,比如:

这应该可以,但是当我构建它时,Xcode 说“Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1”。如果我不在协议中使用“可选”,我不需要在扩展中解开 foo() 并且即使我删除了“self”,一切都会正常工作。谁能告诉我为什么以及如何使可选功能正常工作?提前致谢。

0 投票
1 回答
643 浏览

swift - 用 EXC_BAD_INSTRUCTION 在空扩展中声明符合 @objc 协议

在将 Swift 协议与数组结合使用时遇到了很多麻烦,但在操场上开始出现问题之前,我什至无法重现我的整个问题。这是一个最小的例子。

我有两个协议和一个Bus声明符合其中一个协议的类。此外,一个空的扩展Bus声明符合其他协议:

控制台输出可能看起来是随机的,但事实并非如此。如果我尝试创建以下实例,我会始终如一地得到它Bus

  • 如果我们注释掉所有@objc属性,错误就会消失
  • 如果我们不符合,错误就会消失Utterableextension Bus: Utterable{}

我的协议必须具有该属性@objc的原因是,否则 Obj-c 运行时会在尝试执行诸如var myDisplayables: [Displayable] = [ Bus() ]或以其他方式动态检查协议的一致性时抱怨

同样,请注意,这是一个最小的示例。

使用 Swift 1.2 更新: 现在似乎已修复。Xcode 建议进行这些更改“因为协议需要它”:

0 投票
1 回答
158 浏览

arrays - Creating an array of @objc protocol type from arrays of conformers

I have tried to do this so many ways but the swift compiler complains whatever I do. SourceKit and the compiler also crashes non-stop, so I can't even experiment anymore. Not even to insert some printlns. I'm tearing my hair.

I'm trying to construct a simple array for table view content. The "rows" are Presentable objects, which is just a collection of protocols.

1. This doesn't work:

(buses, metros etc. are all [Bus]?, [Metro]? etc. and those classes conform to all the protocols that are Presentable)

Console output:

2. This "works" (i.e. doesn't crash at runtime) but I seem to get no objects in my new array:

3. This works fully:

All I want is to take my buses, metros, trains, trams, ships and put them in a [Presentable] each, without a wall of code. I'm starting to believe it's impossible in Swift, because it feels like I've rewritten these loops in every conceivable way.

What am I missing? Why can't I seem to iterate successfully instead of repeating all this code?


Update

This is what happens with Davids code:

Same console output as above, but this time it crashes when trying to access the TableSection::rows (that has happened to me before as well). This makes it crash:

Console (I printed the rows array from the Variables View):

Is it just me or don't these numbers add up?

0 投票
3 回答
6450 浏览

swift - 我可以在 Swift 中扩展元组吗?

我想在 Swift 中为(例如)两个值的元组编写一个扩展。例如,我想写这个swap方法:

这样这s将是(String, Int)具有 value的类型("one", 1)。(我知道我可以很容易地实现一个swap(t)函数,但这不是我感兴趣的。)

我可以这样做吗?我似乎无法在extension声明中写出正确的类型名称。

另外,我想答案是一样的,我可以让一个 2 元组采用给定的协议吗?

0 投票
1 回答
622 浏览

ios - 使用 Swift 协议继承

我正在尝试创建一个使用传统多态性来补偿蓝牙 LE、蓝牙等设备的委托,并且似乎无法获得正确的语法来进行转换。

这是我的父协议和类:

现在这里是简化的子类和协议:

它在该函数的第一行引发以下错误:

这对我来说没有意义,因为它应该允许像通常的对象一样投射到孩子身上。

如果我将 @objc 放在 BluetoothDelegate 前面,我会收到以下错误:

有人对此有任何想法吗?

0 投票
1 回答
579 浏览

swift - 在Swift中类本身的协议中使用类中声明的枚举?

我们如何在 Swift 中做类似前向声明的事情?

我在类之前声明了一个协议,但是协议需要使用在类之前声明的枚举(recordType):

现在 Swift 编译器抱怨Use of undeclared type 'recordType'错误。在 Objective-C 中,我们会放置某种前向声明,那么 Swift 呢?

作为一个附注问题,您看到上面我必须声明 case Types,而不是 Type,因为“Type”显然是枚举的保留 case-word。有什么办法可以克服这个吗?(当然,除了像我一样更改名称)

0 投票
0 回答
24 浏览

swift-protocols - Swfit: assgin an Objective C ViewController delete get error: "Type 'xxViewController' does not confirm to protocol"

我有一个“ HomeViewController.h”包含一个属于“”的容器视图PublishViewController.swift。当我单击容器视图上的按钮时,我想弹出“ AddEventViewController.h”。

但是当我指定 "HomeViewController作为 的代表时AddEventViewController,我得到的错误是

“类型 'HomeViewController' 未确认协议“AddEventViewControllerDelegate”

我错过了什么或者这是错的?

这是我的代码: HomeViewController.h @interface HomeViewController ()

AddEventViewController.h

PublishViewController.swift

Xcode截图在此处输入图像描述

0 投票
1 回答
293 浏览

swift - 返回一个自定义的 GeneratorType

我有一个类,它应该提供一个基于集合的随机生成器。
由于它是一个随机生成器(next() 永远不会返回 nil,除非集合为空),我不希望能够将此生成器用作 sequenceType(不支持“for in”以避免无限循环)

我似乎无法正确获取方法签名。

这是我构建的框架,我包含了 3 次尝试及其相应的编译器错误。

0 投票
1 回答
251 浏览

swift - How to wrap protocol with optional methods?

Task is simple: I need to create wrapper that delegates everything to wrapped object. Only the type of wrapped object is a protocol that contains optional methods. My wrapper is to implement the same protocol.

What do I do about optional methods? I can:

  1. I can implement them in my wrapper. But if they are not implemented by the object that gets wrapped what do I return from this implementations?
  2. Not implement them. In that case the wrapper object will not provide some of functionality of the wrapped object which I don't want.

Are there any better options? Please note that I am working in Swift.

0 投票
2 回答
3687 浏览

ios - Swift 协议中的 IBActions 有什么更好的策略?

我正在创建一个 Swift 项目,我想定义一个特定的协议来强制其他组件实现一个animate方法:

问题是我希望这个方法是一个IBAction,但是我从 XCode 得到这个错误:

只有实例方法可以声明为“IBAction”

我的问题是,你将如何实施这样的事情?

我考虑过:

  1. Remove @IBAction,但是我需要记住在每个实现的类中添加它。不是很优雅且容易出错。
  2. 创建一个基类而不是协议,但随后我强制所有组件对我的基类进行子类化,而不是他们自己选择的子类,因此它不是一个有效的选项。

还有其他想法吗?


编辑:回复下面的评论。

on 协议的想法IBAction是因为在项目中会有很多不同的开发者实现小的 UI 组件,所有这些组件都有 animate 方法。这些组件可以通过编程方式或通过 Interface Builder 添加,它们总是非常方便,IBAction因为我计划从 IB 文件中组合它们以最大限度地简化视图控制器(这显然是一个仅视图任务)。

因此,下面提出的在控制器中添加一个仅调用animate组件的方法的解决方案并不好,因为它是冗余代码并且使您的控制器更加依赖您的视图。

让开发人员记住IBAction在方法上添加关键字的想法是可行的,但正如我所说的那样容易出错(我的意思是会有一些人忘记它),我想确保这是始终可从 IB 访问。它还增加了额外的认知负担,因为我需要IBAction在协议中记录这种缺失并要求实现者手动添加它。

我知道这不是在 iOS 和 中工作的常见方式UIKit,但这就是我发布问题的原因,也许有人有其他想法。