swift-extensions

How to create swift class for category?

别来无恙 提交于 2019-11-26 18:56:08
问题 I want to create category of my existing swift class, but there is no option in IDE to do so. Any idea if category exists in swift project? Or how to achieve similar functionality in swift project? 回答1: In Swift, you can use Extensions to add new functionality to existing classes, structs and enumeration types. They differ from Objective-C categories in a few ways, mainly: They aren't named You don't need to import an Extension explicitly. If you define an extension to add new functionality

How to define optional methods in Swift protocol?

走远了吗. 提交于 2019-11-26 18:06:57
Is it possible in Swift? If not then is there a workaround to do it? 1. Using default implementations (preferred). protocol MyProtocol { func doSomething() } extension MyProtocol { func doSomething() { /* return a default value or just leave empty */ } } struct MyStruct: MyProtocol { /* no compile error */ } Advantages No Objective-C runtime is involved (well, no explicitly at least). This means you can conform structs, enums and non- NSObject classes to it. Also, this means you can take advantage of powerful generics system. You can always be sure that all requirements are met when

Swift 3.0: compiler error when calling global func min<T>(T,T) in Array or Dictionary extension

北战南征 提交于 2019-11-26 16:47:30
After converting from Swift 2.2 to 3.0 my Array extension does not compile anymore, because it contains a call to global standard library function min<T>(T,T) and shows compiler error extra argument in call . Here's a simple way to reproduce the error: extension Array { func smallestInt(first: Int, second: Int) -> Int { return min(first, second) // compiler error: "Extra argument in call" } } I get the same error when adding the same function to an extension of Dictionary , while the exact same code compiles just fine in an extension of other types (e.g. String or AudioBuffer ): Looking at the

Pass in a type to a generic Swift extension, or ideally infer it

痴心易碎 提交于 2019-11-26 16:24:41
问题 Say you have class Fancy:UIView you want to find all sibling Fancy views. No problem... for v:UIView in superview!.subviews { if let f = v as? Fancy { f.hungry = false } } So, try an extension, public extension UIView { internal func fancySiblings()->([Fancy]) { return (self.superview! .subviews .filter { $0 != self } .flatMap { $0 as? Fancy } ) } } Awesome, you can now for f:Fancy in self.fancySiblings() { f.hungry = false } Fantastic. But, How to generalize that extension to work with any

Can you override between extensions in Swift or not? (Compiler seems confused!)

柔情痞子 提交于 2019-11-26 13:56:33
问题 I've been working on an iOS application in Swift (much of it being moved from Objective-C). I'm using Core Data and trying to use extensions to add functionality to classes auto-generated from my model. One thing I readily did in Objective-C was to add a method in a category on class A and override that method in a category on class B (which derived from A), and I was hoping to do the same in Swift. For a while now I've had the following code in my project (and this is just one example), and

Creating an extension to filter nils from an Array in Swift

佐手、 提交于 2019-11-26 09:35:06
问题 I\'m trying to write an extension to Array which will allow an array of optional T\'s to be transformed into an array of non-optional T\'s. e.g. this could be written as a free function like this: func removeAllNils(array: [T?]) -> [T] { return array .filter({ $0 != nil }) // remove nils, still a [T?] .map({ $0! }) // convert each element from a T? to a T } But, I can\'t get this to work as an extension. I\'m trying to tell the compiler that the extension only applies to Arrays of optional

Extend array types using where clause in Swift

混江龙づ霸主 提交于 2019-11-26 08:09:58
问题 I\'d like to use the Accelerate framework to extend [Float] and [Double] but each of these requires a different implementation. I tried the obvious: extension Array<Float> { } and get this error: \"Constrained extension must be declared on the unspecialised generic type \'Array\' with constraints specified by a \'where\' clause\" Is it posible to extend generic types in Swift 2 in this way? I\'ve got the code working as expected now. Here\'s an example showing a summation using the Accelerate

How to define optional methods in Swift protocol?

梦想与她 提交于 2019-11-26 06:13:08
问题 Is it possible in Swift? If not then is there a workaround to do it? 回答1: 1. Using default implementations (preferred). protocol MyProtocol { func doSomething() } extension MyProtocol { func doSomething() { /* return a default value or just leave empty */ } } struct MyStruct: MyProtocol { /* no compile error */ } Advantages No Objective-C runtime is involved (well, no explicitly at least). This means you can conform structs, enums and non- NSObject classes to it. Also, this means you can take

Swift 3.0: compiler error when calling global func min<T>(T,T) in Array or Dictionary extension

独自空忆成欢 提交于 2019-11-26 04:56:21
问题 After converting from Swift 2.2 to 3.0 my Array extension does not compile anymore, because it contains a call to global standard library function min<T>(T,T) and shows compiler error extra argument in call . Here\'s a simple way to reproduce the error: extension Array { func smallestInt(first: Int, second: Int) -> Int { return min(first, second) // compiler error: \"Extra argument in call\" } } I get the same error when adding the same function to an extension of Dictionary , while the exact

Return instancetype in Swift

孤街浪徒 提交于 2019-11-26 01:30:03
I'm trying to make this extension: extension UIViewController { class func initialize(storyboardName: String, storyboardId: String) -> Self { let storyboad = UIStoryboard(name: storyboardName, bundle: nil) let controller = storyboad.instantiateViewControllerWithIdentifier(storyboardId) as! Self return controller } } But I get compile error: error: cannot convert return expression of type 'UIViewController' to return type 'Self' Is it possible? Also I want to make it as init(storyboardName: String, storyboardId: String) Similar as in Using 'self' in class extension functions in Swift , you can