swift-extensions

Extending Collection with a recursive property/method that depends on the element type

有些话、适合烂在心里 提交于 2019-11-28 11:49:37
In the context of this question , I though about how one could implement a property or method that counts across all nesting levels in collections. Intuitively, something this should work: extension Collection { var flatCount: Int { if self.count == 0 { return 0 } else if self.first is Collection { // .Iterator.Element: Collection return self.reduce(0) { (res, elem) -> Int in res + (elem as! Collection).flatCount // ERROR } } else { return self.reduce(0) { (res,_) in res + 1 } } } } However, we are not allowed to cast values to protocol types that have associated types. So I thought to make

Why should not directly extend UIView or UIViewController?

时间秒杀一切 提交于 2019-11-28 11:40:50
I saw this question , with this code: protocol Flashable {} extension Flashable where Self: UIView { func flash() { UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseIn, animations: { self.alpha = 1.0 //Object fades in }) { (animationComplete) in if animationComplete == true { UIView.animate(withDuration: 0.3, delay: 2.0, options: .curveEaseOut, animations: { self.alpha = 0.0 //Object fades out }, completion: nil) } } } } And I wonder why do we you not just directly just extend UIView ? Or in similar cases extend UIViewController why twist it around with a where Self: Is it so

Swift map(_:) extension for Set() ?

纵饮孤独 提交于 2019-11-28 09:55:19
let numberSet = Set(1...11) let divideSet = numberSet.map({ $0 / 10 }) //Error: Set does not have a member named map :( Swift 1.2 supports Set() for unordered collections, but map(_:) doesn't seem to work on Sets, so i decide to get smart on my playground and tried: let stringSet = Set(map(numberSet, { String($0)})) println(stringSet) stringSet = ["2", "11", "1", "8", "6", "4", "3", "9", "7", "10", "5] This seemed to work. So I tried extending Set: extension Set { func map<U>(transform: (T) -> U) -> Set<U> { return Set(Swift.map(self, transform)) } } Error: "couldn't find initialiser for Set(T

Swift Extension: same extension function in two Modules

与世无争的帅哥 提交于 2019-11-28 09:00:48
Say I have a Framework called SwiftKit, which has a UIView's extension class method named someClassMethod and a property named someProperty within it: // SwiftKit public extension UIView { class func someClassMethod() { print("someClassMethod from Swift Kit") } var someProperty: Double { print("someProperty from Swift Kit") return 0 } } And I also have a Framework called SwiftFoundation, which also has a UIView's extension class method named someClassMethod and a property named someProperty within it: // SwiftFoundation public extension UIView { class func someClassMethod() { print(

Why does Swift not allow stored properties in extensions?

人走茶凉 提交于 2019-11-28 08:56:31
问题 I've been trying to find the best way to implement a stored property in an extension, and came across this question: Swift extension stored properties alternative. However, I have not found a reason why in the discussion or anywhere else. Is there a reason why stored properties are not allowed in Swift? And if so, what is the reason? 回答1: Extensions are for extending the functionality of an existing class without changing the memory structure. It's more or less syntactic sugar. Imagine you

How to properly use class extensions in Swift?

纵饮孤独 提交于 2019-11-28 07:34:45
In Swift, I have historically used extensions to extend closed types and provide handy, logic-less functionality, like animations, math extensions etc. However, since extensions are hard dependencies sprinkled all over your code-base, I always think three times before implementing something as an extension. Lately, though, I have seen that Apple suggests using extensions to an even greater extent, e.g. implementing protocols as separate extensions. That is, if you have a class A that implement protocol B, you end up with this design: class A { // Initializers, stored properties etc. }

Add constraints to generic parameters in extension

余生长醉 提交于 2019-11-28 02:52:35
问题 I have this function: func flatten<Key: Hashable, Value>(dict: Dictionary<Key, Optional<Value>>) -> Dictionary<Key, Value> { var result = [Key: Value]() for (key, value) in dict { guard let value = value else { continue } result[key] = value } return result } As you can see, it transforms a [Key: Value?] dictionary into a [Key: Value] one (without the optional). I wanted to extend the Dictionary class with a new method only for classes which value is an Optional of any type, but I am unable

Extension for UIColor with custom colors it is real? [duplicate]

随声附和 提交于 2019-11-28 02:14:20
This question already has an answer here: How to access extension of UIColor in SWIFT? 8 answers What is the best way to introduce a custom UIColor to a Swift project? [closed] 2 answers I have some custom colors for my application and now its saves like a dictionary, but I think this is no really good idea and I want to do extension for UIColor with a custom color. That may look like this var newColor = UIColor.MyColor // like UIColor.white Maybe I should add to extension an enumeration with my colors? Create class property in UIColor extension extension UIColor { class var themeColor:UIColor

How to use generic types to get object with same type

老子叫甜甜 提交于 2019-11-28 00:13:49
I have extension for NSManagedObject that should help me to transfer objects between contexts: extension NSManagedObject { func transferTo(#context: NSManagedObjectContext) -> NSManagedObject? { return context.objectWithID(objectID) } } for now it return object of NSManagedObject and i should cast it to class what i want, like this: let someEntity: MyEntity = // ...create someEntity let entity: MyEntity = someEntity.transferTo(context: newContext) as? MyEntity Is there a way in Swift to avoid that useless casting and if i call transferTo(context: ...) from object of class MyEntity make it

How to create swift class for category?

拜拜、爱过 提交于 2019-11-27 17:11:50
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? 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 to an existing type, the new functionality will be available on all existing instances of that type, even if