I added Swiftlint to a project and I'm having trouble understanding what the warning is for extension_access_modifier. I see it mainly on a class that is declared as public, but there are extensions littered throughout the codebase that adds…
I have this function:
func flatten(dict: Dictionary>) -> Dictionary {
var result = [Key: Value]()
for (key, value) in dict {
guard let value = value else { continue }
…
I hoped that Swift gives me the ability to create an extension for type with specified conditions in where block. I imagined that I can extend the same generic type with different extensions dependent on concrete generic type value (T). But not.…
I have an API that returns a JSON array of objects. I've setup the structure to look as follows:
typealias MyModels = [MyModel]
struct MyModel: Codable {
let field1: String
let field2: String
let mySubModel: SubModel?
enum…
I'm trying to create a default implementation of an MKMapViewDelegate by using the conditional extension as follows:
extension MKMapViewDelegate where Self: NSObject {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) ->…
I want to extend Dictionary with String keys (JSON dictionaries) to allow subscripting with any enum that has a RawValue type of String. The end goal would be multiple enums that can be used to subscript JSON dictionaries.
enum JSONKey: String {
…
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…
I'm trying to create fabric method to create UIViewController with correct nib name (to fix iOS8 default initialiser issue). To do it I have added extension:
extension UIViewController {
class func create() -> Self {
if…
I'm having trouble accessing my swift extension from objective-c.
I have the following code in a .swift file:
extension NSDictionary {
func dictionaryAsJsonString() -> NSString {
var err: NSError?
var data =…
After converting from Swift 2.2 to 3.0 my Array extension does not compile anymore, because it contains a call to global standard library function min(T,T) and shows compiler error extra argument in call.
Here's a simple way to reproduce the…
I want to try to write a default init in a protocol extension, so I did this:
protocol P {
var data: [AnyHashable: Any] { get }
init(_ s: String)
}
extension P {
init(_ s: String) {
self.data = ["s": s]
}
}
But I'm getting…
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…
I am programming a media player app and created my own framework for managing all the player functionality. In this framework I have a public protocol called PlayerControllerType and an internal protocol _PlayerControllerType. In…
I want to write Optional extension for any types.
My code for integer:
extension Optional where Wrapped == Int {
func ifNil(default: T) -> T {
if self != nil {
return self as! T
}
return default
…