swift-extensions

Swift extension for selected class instance

自作多情 提交于 2021-02-16 06:24:23
问题 In Objective-C category, you can bring in the extended capability introduced by the category methods by including the header of the category in your class. It seems like all Swift extensions are automatically introduced without import. How do you achieve the same thing in Swift? For example: extension UIView { // only want certain UIView to have this, not all // similar to Objective-C, where imported category header // will grant the capability to the class func extraCapability() { } } 回答1:

Single extension for UITextView and UITextField in Swift

☆樱花仙子☆ 提交于 2020-06-23 01:44:12
问题 I want to create a single extension for both UITextField and UITextView and add a below method to it: func addDoneButtonOnKeyboardWith(selector : Selector) { let keyBoardToolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 320, height: 30)) let barButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: selector) let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil) keyBoardToolBar.items = [flexibleSpace, barButtonItem]

Single extension for UITextView and UITextField in Swift

馋奶兔 提交于 2020-06-23 01:43:23
问题 I want to create a single extension for both UITextField and UITextView and add a below method to it: func addDoneButtonOnKeyboardWith(selector : Selector) { let keyBoardToolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 320, height: 30)) let barButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: selector) let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil) keyBoardToolBar.items = [flexibleSpace, barButtonItem]

Convert Integer to Roman Numeral String in Swift

匆匆过客 提交于 2020-02-26 04:04:29
问题 I am looking to take an Integer in Swift and convert it to a Roman Numeral String . Any ideas? 回答1: One could write an extension on Int , similar to the one seen below. Please note: this code will return "" for numbers less than one. While this is probably okay in terms of Roman Numeral numbers (zero does not exist), you may want to handle this differently in your own implementation. extension Int { var romanNumeral: String { var integerValue = self var numeralString = "" let mappingList: [

Swift Decodable, Endpoint returns completely different types

人盡茶涼 提交于 2020-02-06 08:03:56
问题 With API I'm working with, I have a case where 1 API Endpoint can return completely different responses, based on if the call was successful or not. In case of success , API Endpoint returns an Array of requested objects, in the root, something like this: [ { "key1": "value1", "key2": "value2", "key3": "value3" }, { "key1": "value1", "key2": "value2", "key3": "value3" }, ... ] which I'm normally decoding with try JSONDecoder().decode([Object].self, from: data) In case of an error , API

Swift Decodable, Endpoint returns completely different types

不想你离开。 提交于 2020-02-06 08:03:25
问题 With API I'm working with, I have a case where 1 API Endpoint can return completely different responses, based on if the call was successful or not. In case of success , API Endpoint returns an Array of requested objects, in the root, something like this: [ { "key1": "value1", "key2": "value2", "key3": "value3" }, { "key1": "value1", "key2": "value2", "key3": "value3" }, ... ] which I'm normally decoding with try JSONDecoder().decode([Object].self, from: data) In case of an error , API

Implementing a function with a default parameter defined in a protocol

拜拜、爱过 提交于 2020-01-09 11:11:06
问题 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

Implementing a function with a default parameter defined in a protocol

一曲冷凌霜 提交于 2020-01-09 11:09:29
问题 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