swift-extensions

Swift: Extension on [<SomeType<T>?] to produce [<T>?] possible?

荒凉一梦 提交于 2019-12-01 03:51:28
问题 In Swift, I have a custom struct with this basic premise: A wrapper struct that can contain any type that conforms to BinaryInteger such as Int, UInt8, Int16, etc. protocol SomeTypeProtocol { associatedtype NumberType var value: NumberType { get set } } struct SomeType<T: BinaryInteger>: SomeTypeProtocol { typealias NumberType = T var value: NumberType } And an extension on Collection: extension Collection where Element: SomeTypeProtocol { var values: [Element.NumberType] { return self.map {

Swift “where” Array Extensions

孤街醉人 提交于 2019-11-30 17:01:15
As of Swift 2.0 it seems we can get closer to extensions of generic types applicable to predicated situations. Although we still can't do this: protocol Idable { var id : String { get } } extension Array where T : Idable { ... } ...we can now do this: extension Array { func filterWithId<T where T : Idable>(id : String) -> [T] { ... } } ...and Swift grammatically accepts it. However, for the life of me I cannot figure out how to make the compiler happy when I fill in the contents of the example function. Suppose I were to be as explicit as possible: extension Array { func filterWithId<T where T

Swift 2 Error using mutating function in Protocol extension \"Cannot use mutating member on immutable value: 'self' is immutable

China☆狼群 提交于 2019-11-30 05:35:31
Not sure what's going on here, this seems like it should be pretty straight forward. I have a protocol that mutable var, an extension with a mutating function. Things are crapping out in the testClass.testFunc , when I try and use mtkAnimQueAppend declared in the extension, I get this error: "Cannot use mutating member on immutable value: 'self' is immutable. protocol MTKAnimateValueDelegate { var mtkAnimQue:[MTKAnimateValue]? {get set} } extension MTKAnimateValueDelegate { ///Adds element to que mutating func mtkAnimQueAppend(element:MTKAnimateValue) { if mtkAnimQue != nil { mtkAnimQue?

Swift “where” Array Extensions

删除回忆录丶 提交于 2019-11-30 00:16:33
问题 As of Swift 2.0 it seems we can get closer to extensions of generic types applicable to predicated situations. Although we still can't do this: protocol Idable { var id : String { get } } extension Array where T : Idable { ... } ...we can now do this: extension Array { func filterWithId<T where T : Idable>(id : String) -> [T] { ... } } ...and Swift grammatically accepts it. However, for the life of me I cannot figure out how to make the compiler happy when I fill in the contents of the

Swift 2 Error using mutating function in Protocol extension "Cannot use mutating member on immutable value: 'self' is immutable

老子叫甜甜 提交于 2019-11-29 10:34:40
问题 Not sure what's going on here, this seems like it should be pretty straight forward. I have a protocol that mutable var, an extension with a mutating function. Things are crapping out in the testClass.testFunc , when I try and use mtkAnimQueAppend declared in the extension, I get this error: "Cannot use mutating member on immutable value: 'self' is immutable. protocol MTKAnimateValueDelegate { var mtkAnimQue:[MTKAnimateValue]? {get set} } extension MTKAnimateValueDelegate { ///Adds element to

Add constraints to generic parameters in extension

时光怂恿深爱的人放手 提交于 2019-11-29 09:28:44
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 to add constraints to the generic parameters of the dictionary. This is what I tried: extension

How to add an optional string extension?

最后都变了- 提交于 2019-11-28 20:01:30
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 */ 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 } } You can do it like this: protocol OptionalType { typealias A; var opt: A? { get } } extension Optional: OptionalType { var opt: A? { return self } } protocol StringType { var get:

Can objective-c code call swift extension on Class?

百般思念 提交于 2019-11-28 18:04:44
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 ? 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) call the methods from the Swift extension I found out that in Swift 4.0 I had to add @objc in front of my

How to create several cached UIColor

百般思念 提交于 2019-11-28 13:55:43
I have custom colors within my code. I use them several times and I would like to have them allocated only once. The situation / problem If we get a look at UIColor headers we can see the following : [...] // Some convenience methods to create colors. These colors will be as calibrated as possible. // These colors are cached. open class var black: UIColor { get } // 0.0 white open class var darkGray: UIColor { get } // 0.333 white [...] I've created an extension of UIColor, like so : import UIKit extension UIColor { class func colorWithHexString(_ hex: String) -> UIColor { print("\(#function):

Implementing a function with a default parameter defined in a protocol

♀尐吖头ヾ 提交于 2019-11-28 12:10:35
Swift protocols can provide default implementations for functions and computed properties by adding extensions to them. I've done that plenty of times. It is my understanding that the default implementation is only used as a "fallback" : It's executed when a type conforms to the protocol but doesn't provide its own implementation. At least that's how I read The Swift Programming Language guide: If a conforming type provides its own implementation of a required method or property, that implementation will be used instead of the one provided by the extension. Now I ran into a situation where my