swift-extensions

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

陌路散爱 提交于 2019-12-04 02:25:53
问题 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 回答1: 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,

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

六月ゝ 毕业季﹏ 提交于 2019-12-04 02:13:46
问题 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

Name collisions for extension methods from different frameworks

Deadly 提交于 2019-12-03 11:55:30
问题 As a test, I created two frameworks. Both frameworks contain this extension: public extension UIDevice { var extraInfo: UIDeviceExtraInfo { return UIDeviceExtraInfo() } } public class UIDeviceExtraInfo { public var prop: String = "Device1" //"Device2" is used in another framework } I then imported the two frameworks and attempt to print UIDevice.currentDevice().extraInfo.prop . Swift compiler gives the error: Ambiguous use of extraInfo" . How does one go about resolving name clashing like

what is 'where self' in protocol extension

耗尽温柔 提交于 2019-12-03 11:06:51
问题 I saw so many examples with below format extension Protocolname where Self: UIViewController What is where Self in protocol extension. I couldn't find the documentation on this. 回答1: That syntax is: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-ID521 Consider: protocol Meh { func doSomething(); } //Extend protocol Meh, where `Self` is of type `UIViewController` //func blah() will only

How to make extension for multiple classes Swift

给你一囗甜甜゛ 提交于 2019-12-03 08:22:12
问题 I have an extension: extension UILabel { func animateHidden(flag: Bool) { self.hidden = flag } } I need to make the same one for UIImageView but I don't want to copy that whole code. Is it possible to make an extension for multiple classes? Thanks. 回答1: You could make a protocol and extend it. Something like: protocol Animations { func animateHidden(flag: Bool) } extension Animations { func animateHidden(flag: Bool) { // some code } } extension UILabel: Animations {} extension UIImageView:

Swift 'open' keyword & overridable method/properties in extension?

ε祈祈猫儿з 提交于 2019-12-03 06:14:51
With introduction of open keyword in Swift 3.0 ( What is the 'open' keyword in Swift? ). Note: Limited to extensions on NSObject derived classes or @objc attributed method/properties. Code wich declared and used public ( class ) methods/properties in extension across modules/frameworks broke, as public is no longer means 'overridable' outside of defining module. Example: public extension UIManagedDocument { public class func primaryDocumentName() -> String { return "Document" } public class func primaryStoreURL() -> URL { let documentsURL = FileManager.default.userDocumentsURL return URL

How to define initializers in a protocol extension?

与世无争的帅哥 提交于 2019-12-03 04:30:39
问题 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? 回答1: 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

Self, protocol extension and non-final class

陌路散爱 提交于 2019-12-03 04:30:29
问题 I tried write a static method for UIView which instantiates view of that class from the nib. Method should be generic and work on every UIView subclasses. Also I want to save the type information – so, for example, in this code let myView = MyView.loadFromNib() compiler infers that myView has MyView class. After few trials I decided to use protocol extensions, because otherwise I wouldn't have access to Self inside method body. Looks like this should work: protocol NibLoadable { static func

what is 'where self' in protocol extension

半腔热情 提交于 2019-12-03 01:31:53
I saw so many examples with below format extension Protocolname where Self: UIViewController What is where Self in protocol extension. I couldn't find the documentation on this. That syntax is: https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html#//apple_ref/doc/uid/TP40014097-CH25-ID521 Consider: protocol Meh { func doSomething(); } //Extend protocol Meh, where `Self` is of type `UIViewController` //func blah() will only exist for classes that inherit `UIViewController`. //In fact, this entire extension only exists for

Self, protocol extension and non-final class

倾然丶 夕夏残阳落幕 提交于 2019-12-02 17:42:39
I tried write a static method for UIView which instantiates view of that class from the nib. Method should be generic and work on every UIView subclasses. Also I want to save the type information – so, for example, in this code let myView = MyView.loadFromNib() compiler infers that myView has MyView class. After few trials I decided to use protocol extensions, because otherwise I wouldn't have access to Self inside method body. Looks like this should work: protocol NibLoadable { static func loadFromNib(name: String?) -> Self } extension NibLoadable where Self: UIView { static func loadFromNib