swift-extensions

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

五迷三道 提交于 2019-11-27 13:34:52
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 UIView subtype? Ideally, can the extension infer the type , even? As well as taking a type? So,

How to add an optional string extension?

前提是你 提交于 2019-11-27 12:38:04
问题 You can create a String extension like so: extension String { func someFunc -> Bool { ... } } but what if you want it to apply to optional string? var optionalString :String? = "" optionalString!.someFunc() /* String? does not have a member someFunc */ 回答1: In Swift 3.1 you can add an extension to optional values as well: extension Optional where Wrapped == String { var isBlank: Bool { return self?.isBlank ?? true } } 回答2: You can do it like this: protocol OptionalType { typealias A; var opt:

Can objective-c code call swift extension on Class?

让人想犯罪 __ 提交于 2019-11-27 11:02:33
问题 I searched some posts, I think I cannot write an extension under swift, and call it from Objective-C code, right ? @objc like attributes only support methods, class, protocols ? 回答1: You can write a Swift extension and use it in ObjectiveC code. Tested with XCode 6.1.1. All you need to do is: create your extension in Swift (no @objc annotation) import "ProjectTarget-Swift.h" in your ObjectiveC class (where "ProjectTarget" represents the XCode target the Swift extension is associated with)

Extension may not contain stored property but why is static allowed

给你一囗甜甜゛ 提交于 2019-11-27 09:03:26
Extension cannot contain stored property, but why then can static stored property be defined within extension? I also didn't find any documentation mentioning that static property is allowed in extension. extension String { static let test = "Test" static var test2 = "Test2" } Extensions cannot contain stored instance properties. Why? Because adding an instance property would change the size of instances of that type. What happens if one module adds an extension such that an Int is now 2 words long? What should then happen when it, for example, gets an Int from another module where they are

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

只愿长相守 提交于 2019-11-27 07:58:23
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 though I have not used the functionality yet, the compiler has worked just fine compiling this code: //

How to use generic types to get object with same type

谁都会走 提交于 2019-11-27 04:43:01
问题 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

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

試著忘記壹切 提交于 2019-11-27 03:17:29
问题 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) -

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

喜你入骨 提交于 2019-11-26 22:10:23
问题 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

Extend array types using where clause in Swift

主宰稳场 提交于 2019-11-26 22:07:22
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 framework. extension _ArrayType where Generator.Element == Float { func quickSum() -> Float { var result: Float

How to properly use class extensions in Swift?

六月ゝ 毕业季﹏ 提交于 2019-11-26 20:27:59
问题 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