我有以下代码:
func roughlyEq<T: FloatingPoint>(_ a: T, _ b: T) -> Bool {
return abs(a - b) < (0.01 as! T)
}
当我使用浮动类型的参数调用它时,在运行时会发生崩溃:
Could not cast value of type 'Swift.Double' (0x10043e6a8) to 'Swift.Float' (0x10043e5e0)
这是没有意义的-- 0.01确实可以转换成浮点,比如当我编写Float(0.01)时。为什么会发生这个错
如何使用自定义函数utils扩展Swift的Array<T>或T[]类型?
浏览一下Swift的API文档,就会发现数组方法是T[]的扩展,例如:
extension T[] : ArrayType {
//...
init()
var count: Int { get }
var capacity: Int { get }
var isEmpty: Bool { get }
func copy() -> T[]
}
当复制和粘贴相同的源代码并尝试任何变体时,例如:
extension T[] : ArrayType {
在Objective类别中,可以通过在类中包含类别的标题来引入类别方法引入的扩展功能。
似乎所有Swift扩展都是在没有导入的情况下自动引入的。你如何在Swift中实现同样的目标?
例如:
extension UIView {
// only want certain UIView to have this, not all
// similar to Objective-C, where imported category header
// will grant the capability to the class
func extraCapability() {
我喜欢Swift允许您在协议中定义方法,然后通过该协议的扩展实现该方法。但是,特别是对于在相同作用域和相同访问级别中定义协议和扩展的情况,是否需要首先在协议中定义方法?
考虑下面的代码:
public protocol MyProtocol {
func addThese(a:Int, b:Int) -> Int
}
public extension MyProtocol {
func addThese(a:Int, b:Int) -> Int{
return a + b
}
}
这和这个有什么不同呢?
public protocol My
使用Swift 4.1 (Xcode 9.3),我尝试创建一组Int,但是我得到:
类型'Int‘不符合协议'Hashable’
但据我所知,说,只要数组的值符合哈斯可,它现在就符合哈斯可。作为一个解决办法,我有:
extension Array: Hashable where Element == Int {
public var hashValue: Int {
return debugDescription.hashValue
}
}
但我还是想知道为什么我不能免费得到哈斯。
在Swift中定义类时,可以具有var属性,这些属性与其他OOP语言中的正常字段类似,但也可以具有let属性,这些属性既是只读的,也是不可变的(如C++中的T const * const )。
然而,是否存在与C++的T * const等价的Swift?(也就是说,字段本身是不可变的,但它指向的对象是可变的)?
下面是我的场景的一个表示:
class Foo {
let bar: Bar
init(bar: Bar) {
self.bar = bar
}
}
protocol Bar {
var fleem: Int? { get set }
我正在尝试定义一个扩展协议A的协议B(后者包含相关的类型C),同时按照编译器的建议使用带有相同类型约束的where子句。然而,当这样做的时候,代码将不再编译。这是一个Swift bug (在本例中是Swift 4)吗?
更具体地说,下面的代码编译时没有出现错误:
类型“E”不符合协议“A”
class D {
}
protocol A: class {
associatedtype C: AnyObject
}
protocol B: A where C == D {
}
class E: B {
}
如下所示更改协议B的定义将进行编译,但将显示此警告:
Typealias
根据注释中引用的错误消息,以下代码失败。它已经被归结为最低限度,所以下面的代码没有明显的实用价值。我只是试图处理真正奇怪的(在我看来)错误消息。我之所以将数组声明为[P]而不是[S],是因为数组内容的常见运行时多态性。
protocol P {
func sp()
}
struct S: P {
func sp() {}
}
extension Array where Element: P {
func am() {}
}
func t() {
let goodA = [S]()
goodA.am() // No problem
let
我想重构一些懒惰的功能敏捷。为了解释这种情况,我首先解释一下类似的急切情况:
let numbers = 1...10
do {
print("==================== EAGER INLINE =============================")
/// We start with a series of transformation on an array:
let result
= numbers
.filter { $0 >= 5 } /// Drop numbers less than 5
协议和类绑定协议有什么区别,我们应该在Swift中使用哪一种协议?
protocol A : class { ... }
protocol A { ... }
当未将协议定义为weak时,尝试添加: class委托时会出现错误
protocol A { ... }
weak var delegate: A
给出错误:
“弱”不能应用于非类类型。
或
不能将“弱”应用于非类绑定的“A”;考虑添加具有类约束的协议一致性。
Swift的数组类型以==和!=运算符重载的方式实现了等效协议:
extension Array : Equatable where Element : Equatable {
/// - Parameters:
/// - lhs: An array to compare.
/// - rhs: Another array to compare.
public static func == (lhs: [[Element].Element], rhs: [[Element].Element]) -> Bool
public stati
我正在使用模板项目来尝试构建一个新的应用程序。当我运行模板项目时,我得到一个错误消息:'T没有名为'indexOf‘的成员。
Array+RemoveObject.swift文档中的现有代码是:
import Foundation
public func removeObject<T: Equatable>(object: T, inout fromArray array: [T])
{ let index = array.indexOf(object)
if let index = index {
array.removeAtIndex(i
为什么这个Swift代码不能编译?
protocol P { }
struct S: P { }
let arr:[P] = [ S() ]
extension Array where Element : P {
func test<T>() -> [T] {
return []
}
}
let result : [S] = arr.test()
编译器会说:“类型P不符合协议P”(或者,在以后的Swift版本中,“不支持使用'P‘作为符合协议'P’的具体类型。”)。
为什么不行?不知何故,这感觉就像是语言中的一个漏洞。我
Swift 1.2
我试图在一个函数的开关大小写中进行模式匹配,该函数的参数为任何类型,以便将其分派给私有的、更专门化的init。
这里是游乐场推断:
import Foundation
struct myStruct {
}
func switchOnAny(any: Any) -> String {
println("Dynamic Type == \(any.dynamicType)")
switch any {
case let array as [Any]:
return "Array"
cas