Tag: generics

Protocol只能用作通用约束,因为它具有Self或associatedType的要求

您好我有一个协议的RequestType,它有associatedType模型如下。 public protocol RequestType: class { associatedtype Model var path: String { get set } } public extension RequestType { public func executeRequest(completionHandler: Result<Model, NSError> -> Void) { request.response(rootKeyPath: rootKeyPath) { [weak self] (response: Response<Model, NSError>) -> Void in completionHandler(response.result) guard let weakSelf = self else { return } if weakSelf.logging { debugPrint(response) } } […]

Xcode 7 Swift 2无法实例化通用UITableViewController的UIViewController子类

我有一个generics类: class PaginatedTableViewController <GenericElement, Source: PaginationDataSource where Source.PaginatedGenericElement == GenericElement>: UITableViewController 另一个我试图从故事板实例化: class CandidatesTableViewController: PaginatedTableViewController<Match, MatchPaginationDataSource> 我无法在故事板自定义类下拉菜单中findCandidatesTableViewController 。 如果我强制它,然后在代码中投我的控制器,应用程序崩溃在运行时抱怨我的控制器(这应该是一个CandidatesTableViewController实例)实际上是一个UITableViewController实例。 未知类_TtC21MyProjectName29CandidatesTableViewController在Interface Builder文件中。 无法将types'UITableViewController'(0x1040917f8)的值转换为“MyProjectName.CandidatesTableViewController”(0x1013a9890)。 在我的项目中,这个控制器被embedded到另一个中, tableViewController = (segue.destinationViewController as! CandidatesTableViewController) 有谁知道如何解决这个问题?

如何将符合相关types的协议的不同types添加到集合中?

作为学习练习,我正在用Swift重写我的validation库 。 我有一个ValidationRule协议,它定义了什么样的个人规则: protocol ValidationRule { typealias InputType func validateInput(input: InputType) -> Bool //… } 关联的typesInputType定义了要validation的input的types(例如String)。 它可以是明确的或通用的。 这里有两条规则: struct ValidationRuleLength: ValidationRule { typealias InputType = String //… } struct ValidationRuleCondition<T>: ValidationRule { typealias InputType = T // … } 在其他地方,我有一个函数用一个ValidationRule集合来ValidationRuleinput: static func validate<R: ValidationRule>(input i: R.InputType, rules rs: [R]) -> ValidationResult { let errors = […]

在Interface Builder中使用generics类作为自定义视图

我有一个UIButton的自定义子类: import UIKit @IBDesignable class MyButton: UIButton { var array : [String]? } 这是IBDesignable,我已经将它设置为我的故事板中button之一的自定义类。 我想使它通用,使数组不必是一个string对象。 所以,我试过这个: import UIKit @IBDesignable class MyButton<T>: UIButton { var array : [T]? } 但是,我不确定如何将此设置为IB中的类。 我试着把MyButton<String>或MyButton<Int> ,但接口生成器只是删除尖括号部分,并得到以下编译错误: Command /Applications/Xcode6-Beta4.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift failed with exit code 254 有没有办法使用通用的自定义类,或者不支持?

如何使用generics协议作为variablestypes

假设我有一个协议: public protocol Printable { typealias T func Print(val:T) } 这是实施 class Printer<T> : Printable { func Print(val: T) { println(val) } } 我的期望是,我必须能够使用Printablevariables来打印这样的值: let p:Printable = Printer<Int>() p.Print(67) 编译器抱怨这个错误: “协议”Printable“只能用作通用约束,因为它具有自我或相关types的要求” 难道我做错了什么 ? 有任何解决这个问题的方法吗 ? **EDIT :** Adding similar code that works in C# public interface IPrintable<T> { void Print(T val); } public class Printer<T> […]

按索引移动数组中的元素

给定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 } […]

iOS:通常从NSObject类序列化/反序列化复杂的JSON

任何人都知道如何序列化基于NSObject类的嵌套JSON? 有一个讨论序列化简单的JSON 在这里 ,但它不足以满足复杂的嵌套JSON。 想象一下,这是JSON的结果: { "accounting" : [{ "firstName" : "John", "lastName" : "Doe", "age" : 23 }, { "firstName" : "Mary", "lastName" : "Smith", "age" : 32 } ], "sales" : [{ "firstName" : "Sally", "lastName" : "Green", "age" : 27 }, { "firstName" : "Jim", "lastName" : "Galley", "age" : 41 } […]