swift-extensions

How to define initializers in a protocol extension?

喜欢而已 提交于 2019-12-02 16:59:15
protocol Car { var wheels : Int { get set} init(wheels: Int) } extension Car { init(wheels: Int) { self.wheels = wheels } } on self.wheels = wheels i get the error Error: variable 'self' passed by reference before being initialized How can I define the initializer in the protocol extension? As you can see this doesn't work under these circumstances because when compiling, one has to make sure that all properties are initialized before using the struct/enum/class. You can make another initializer a requirement so the compiler knows that all properties are initialized: protocol Car { var wheels

Protocol extension in Swift 3 [duplicate]

我们两清 提交于 2019-12-02 13:02:18
This question already has an answer here: EXC_BAD_ACCESS Using IBInspectable 1 answer I want to have a default property of UIImageView , which would be isFlipped . I am able to do it by subclassing UIImageView and adding one property isFlipped . But I want to user protocol and extensions for this , but it is crashing after sometime. Below is my code. How can I use it in right way? Thanks import Foundation import UIKit protocol FlipImage { var isFlipped: Bool { get set } } extension UIImageView:FlipImage{ var isFlipped: Bool { get { return self.isFlipped } set { self.isFlipped = newValue } } }

Declaring conformance to @objc protocol in empty extension breaks with EXC_BAD_INSTRUCTION

孤街醉人 提交于 2019-12-02 11:26:21
问题 Been having lots and lots of trouble with Swift protocols in combination with arrays, but I couldn't even reproduce my whole problem before things started to break in playground. Here's a minimal example. I have two protocols and a class Bus which declares conformance to one of the protocols. In addition, an empty extension of Bus declares conformance to the other protocol: import Foundation @objc protocol Displayable { var name: String {get} } @objc protocol Utterable { var utterance: String

Declaring conformance to @objc protocol in empty extension breaks with EXC_BAD_INSTRUCTION

。_饼干妹妹 提交于 2019-12-02 05:13:30
Been having lots and lots of trouble with Swift protocols in combination with arrays, but I couldn't even reproduce my whole problem before things started to break in playground. Here's a minimal example. I have two protocols and a class Bus which declares conformance to one of the protocols. In addition, an empty extension of Bus declares conformance to the other protocol: import Foundation @objc protocol Displayable { var name: String {get} } @objc protocol Utterable { var utterance: String {get} } class Bus : Displayable { var name = "a bus"; var utterance = "this is a bus"} extension Bus :

Same class extension in two different modules

╄→гoц情女王★ 提交于 2019-12-01 17:10:51
I have a framework in my project that implements an extension on NSDate. The extension looks like this. extension NSDate { func isGreaterThanDate(otherDate: NSDate) -> Bool { //function implementation here } } I've imported this framework into an app project. Now if I copy and paste that same extension into a swift file in the app, the new copy in my app's code appears to override the copy in the framework's code. When I'm calling this function in my app, is there a way I can use namespacing to specify which implementation I want? The only answer I've found so far "No you can't use namespacing

Same class extension in two different modules

别等时光非礼了梦想. 提交于 2019-12-01 16:33:36
问题 I have a framework in my project that implements an extension on NSDate. The extension looks like this. extension NSDate { func isGreaterThanDate(otherDate: NSDate) -> Bool { //function implementation here } } I've imported this framework into an app project. Now if I copy and paste that same extension into a swift file in the app, the new copy in my app's code appears to override the copy in the framework's code. When I'm calling this function in my app, is there a way I can use namespacing

Swift is there a method that gives the index of a substring inside another string

 ̄綄美尐妖づ 提交于 2019-12-01 14:40:27
Is there any existing function that looks for the index of a substring inside another string? A method like .indexOfSubstring thank does this: let word: String = "Hey there, how are you?" let indexOf: Int = word.indexOfSubstring("ere, how are") println("index = " + \(indexOf)) and prints: index = 6 You can use the rangeOfString method: import Foundation let word: String = "Hey there, how are you?" if let range = word.rangeOfString("ere, how are") { let index = distance(word.startIndex, range.startIndex) println("index = \(index)") } It returns a range, i.e. both sides of the searched string -

Swift Extension of Array with Equatable Elements Cannot Call Index(of:)

吃可爱长大的小学妹 提交于 2019-12-01 12:34:45
I am attempting to add an extension to the Array type in Swift limited to Arrays whose elements conform to the equatable protocol. I am attempting to define a function in the following manner: import Foundation extension Array where Iterator.Element: Equatable { func deletedIndicies<T: Equatable>(newArray: [T]) -> [Int] { var indicies = [Int]() for element in self { if newArray.index(of: element) == nil { indicies.append(self.index(of: element)!) } } return indicies } } } The purpose of the function is to return the indices of any items in the original array that do not appear in the newArray

Implement protocol through extension [duplicate]

白昼怎懂夜的黑 提交于 2019-12-01 08:17:32
问题 This question already has answers here : Swift: Using protocol extension results in “unrecognized selector sent to instance” (2 answers) Closed 3 years ago . I'm trying to create a protocol that wraps the process of using the UIImagePickerController to make it more stream-lined in my apps. I essentially have something like this: public protocol MediaAccessor : UIImagePickerControllerDelegate, UINavigationControllerDelegate { func mediaCaptured(title: String, fileData: NSData, fileType: String

Extending typed array by conforming to a protocol in Swift 2

穿精又带淫゛_ 提交于 2019-12-01 04:32:21
问题 I want to extend a typed array Array<SomeType> so that it conforms to a protocol SomeProtocol . Now I know you can extend a typed array like below: extension Array where Element: SomeType { ... } And you can also extend an object to conform to a protocol like so: extension Array: SomeProtocol { ... } But I can't figure out what's the right syntax to have the typed array conform to a protocol, something like: extension (Array where Element: SomeType): SomeProtocol { ... } Any Swift 2 experts