Tag: 迅捷

在swift中使用协议作为数组types和函数参数

我想创build一个可以存储符合某个协议的对象的类。 对象应该存储在一个types数组中。 根据Swift文档协议可以作为types使用: 因为它是一种types,所以可以在许多其他types允许的地方使用协议,包括: 作为函数,方法或初始值设定项中的参数types或返回types 作为常量,variables或属性的types 作为数组,字典或其他容器中的项目的types 但是,以下生成编译器错误: 协议“SomeProtocol”只能用作通用约束,因为它具有自我或相关types的要求 你应该如何解决这个问题: protocol SomeProtocol: Equatable { func bla() } class SomeClass { var protocols = [SomeProtocol]() func addElement(element: SomeProtocol) { self.protocols.append(element) } func removeElement(element: SomeProtocol) { if let index = find(self.protocols, element) { self.protocols.removeAtIndex(index) } } }

如何在Swift中创build延迟?

我想暂停我的应用程序在某一点上。 换句话说,我希望我的应用程序执行代码,但是在某个时候,暂停4秒,然后继续执行其余的代码。 我怎样才能做到这一点? 我正在使用Swift。

在Swift中迭代一个字典

对于Xcode在Swift编程语言指南中给我这个实验的答案,我有点困惑: // Use a for-in to iterate through a dictionary (experiment) let interestingNumbers = [ "Prime": [2, 3, 5, 7, 11, 13], "Fibonacci": [1, 1, 2, 3, 5, 8], "Square": [1, 4, 9, 16, 25] ] var largest = 0 for (kind, numbers) in interestingNumbers { for number in numbers { if number > largest […]

通过触摸任何地方使用SwiftclosuresiOS键盘

我一直在寻找这个,但我似乎无法find它。 我知道如何解雇使用Objective-C的键盘,但我不知道如何使用Swift做到这一点? 有人知道吗?

我如何看到我正在使用哪个版本的Swift?

我刚刚在Xcode中创build了一个新的Swift项目。 我想知道它使用的是哪个版本的Swift。 我如何在Xcode或terminal中看到我在项目中使用的是什么版本的Swift?

按索引移动数组中的元素

给定n个元素的数组,即 var array = [1, 2, 3, 4, 5] 我可以写一个扩展到Array所以我可以修改数组来实现这个输出: [2, 3, 4, 5, 1] : mutating func shiftRight() { append(removeFirst()) } 有没有一种方法可以实现这样一个function,可以将数组按任何索引(正数或负数)进行移位。 我可以使用if-else子句以命令式的风格来实现这个function,但是我正在寻找的是function实现。 该algorithm很简单: 将数组拆分成由索引提供的两个 将第一个数组附加到第二个数组的末尾 有什么办法来实现它的function风格? 我已经完成的代码: extension Array { mutating func shift(var amount: Int) { guard -count…count ~= amount else { return } if amount < 0 { amount += count } […]

转换为swift 3后,视图控制器中出现奇怪的generics函数

在我的项目中,在转换为swift 3之后,在我的ViewController类之前出现了一个新的函数: fileprivate func < <T : Comparable>(lhs: T?, rhs: T?) -> Bool { switch (lhs, rhs) { case let (l?, r?): return l < r case (nil, _?): return true default: return false } } 这个function是做什么的? 为什么我需要它?

在Swift中捕获NSException

下面的代码在Swift引发NSInvalidArgumentExceptionexception: task = NSTask() task.launchPath = "/SomeWrongPath" task.launch() 我怎样才能抓住例外? 据我所知,在Swift中尝试/ catch是针对Swift中引发的错误,而不是从像NSTask(我猜是用ObjC编写的)对象引发的NSExceptions。 我是新来的Swift,所以可能是我失去了一些明显的东西… 编辑 :这里是一个错误的雷达(专为NSTask): openradar.appspot.com/22837476

比较没有时间分量的NSDates

在一个快速的操场上,我一直在使用 NSDate.date() 但是,这总是伴随着时间元素出现。 对于我的应用程序,我需要忽略时间元素。 这可能在Swift中吗? 怎么办? 即使我可以将时间元素设置为每个date的同一时间也是如此。 此外,我试图比较两个date,目前我使用下面的代码: var earlierDate:NSDate = firstDate.earlierDate(secondDate) 这是唯一的方法,或者我可以这样做,忽略时间元素? 例如,如果他们是同一天,但不同的时间,我不想要一个结果。

快速sorting3

如何将下面的函数转换为swift 3 ? 目前得到一个Binary operator '..<' cannot be applied to operands of type 'Int' and 'Self.IndexDistance'错误的Binary operator '..<' cannot be applied to operands of type 'Int' and 'Self.IndexDistance' 。 extension MutableCollection where Index == Int { /// Shuffle the elements of `self` in-place. mutating func shuffleInPlace() { // empty and single-element collections don't shuffle if […]