Hey, I'm learning Swift at the moment and I'm reading a ton of code to try and improve. I made this post because there are two patterns I see a lot in large swift projects which I am having understanding:
- Declaration of classes as if they were protocols. Meaning the class only has the function signatures but not the implementations in it.
- New types get declared and immediately get an extension, instead of just defining what the extension is adding in the initial type declaration in the first place, which is what would seem more reasonable to me at least.
The UTTraitCollection definition has an example of both cases:
import Foundation
import UIKit
import _SwiftUIKitOverlayShims
//
// UITraitCollection.h
// UIKit
//
// Copyright (c) 2013-2018 Apple Inc. All rights reserved.
//
/** A trait collection encapsulates the system traits of an interface's environment. */
@available(iOS 8.0, *)
open class UITraitCollection : NSObject, NSCopying, NSSecureCoding {
public init()
public init?(coder: NSCoder)
open func containsTraits(in trait: UITraitCollection?) -> Bool
/** Returns a trait collection by merging the traits in `traitCollections`. The last trait along any given
axis (e.g. interface usage) will supersede any others. */
public /*not inherited*/ init(traitsFrom traitCollections: [UITraitCollection])
public /*not inherited*/ init(userInterfaceIdiom idiom: UIUserInterfaceIdiom)
open var userInterfaceIdiom: UIUserInterfaceIdiom { get } // unspecified: UIUserInterfaceIdiomUnspecified
@available(iOS 12.0, *)
public /*not inherited*/ init(userInterfaceStyle: UIUserInterfaceStyle)
@available(iOS 12.0, *)
open var userInterfaceStyle: UIUserInterfaceStyle { get } // unspecified: UIUserInterfaceStyleUnspecified
@available(iOS 10.0, *)
public /*not inherited*/ init(layoutDirection: UITraitEnvironmentLayoutDirection)
@available(iOS 10.0, *)
open var layoutDirection: UITraitEnvironmentLayoutDirection { get } // unspecified: UITraitEnvironmentLayoutDirectionUnspecified
public /*not inherited*/ init(displayScale scale: CGFloat)
open var displayScale: CGFloat { get } // unspecified: 0.0
public /*not inherited*/ init(horizontalSizeClass: UIUserInterfaceSizeClass)
open var horizontalSizeClass: UIUserInterfaceSizeClass { get } // unspecified: UIUserInterfaceSizeClassUnspecified
public /*not inherited*/ init(verticalSizeClass: UIUserInterfaceSizeClass)
open var verticalSizeClass: UIUserInterfaceSizeClass { get } // unspecified: UIUserInterfaceSizeClassUnspecified
@available(iOS 9.0, *)
public /*not inherited*/ init(forceTouchCapability capability: UIForceTouchCapability)
@available(iOS 9.0, *)
open var forceTouchCapability: UIForceTouchCapability { get } // unspecified: UIForceTouchCapabilityUnknown
@available(iOS 10.0, *)
public /*not inherited*/ init(preferredContentSizeCategory: UIContentSizeCategory)
@available(iOS 10.0, *)
open var preferredContentSizeCategory: UIContentSizeCategory { get } // unspecified: UIContentSizeCategoryUnspecified
@available(iOS 10.0, *)
public /*not inherited*/ init(displayGamut: UIDisplayGamut)
@available(iOS 10.0, *)
open var displayGamut: UIDisplayGamut { get } // unspecified: UIDisplayGamutUnspecified
@available(iOS 13.0, *)
public /*not inherited*/ init(accessibilityContrast: UIAccessibilityContrast)
@available(iOS 13.0, *)
open var accessibilityContrast: UIAccessibilityContrast { get } // unspecified: UIAccessibilityContrastUnspecified
@available(iOS 13.0, *)
public /*not inherited*/ init(userInterfaceLevel: UIUserInterfaceLevel)
@available(iOS 13.0, *)
open var userInterfaceLevel: UIUserInterfaceLevel { get } // unspecified: UIUserInterfaceLevelUnspecified
@available(iOS 13.0, *)
public /*not inherited*/ init(legibilityWeight: UILegibilityWeight)
@available(iOS 13.0, *)
open var legibilityWeight: UILegibilityWeight { get } // unspecified: UILegibilityWeightUnspecified
}
/** Trait environments expose a trait collection that describes their environment. */
public protocol UITraitEnvironment : NSObjectProtocol {
@available(iOS 8.0, *)
var traitCollection: UITraitCollection { get }
/** To be overridden as needed to provide custom behavior when the environment's traits change. */
@available(iOS 8.0, *)
func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?)
}
extension UITraitCollection {
/* The current trait collection, used when resolving the appearance of dynamic UIColors and similar objects.
* This is a thread-local property, so it may be changed on non-main threads without affecting the main thread.
*/
@available(iOS 13.0, *)
open class var current: UITraitCollection
/* Sets `UITraitCollection.currentTraitCollection` to this trait collection, performs the given actions,
* then restores `UITraitCollection.currentTraitCollection` to its original value.
* Just like `currentTraitCollection`, this only affects the current thread, and may be used on non-main threads
* without affecting the main thread.
*/
@available(iOS 13.0, *)
open func performAsCurrent(_ actions: () -> Void)
}
extension UITraitCollection {
/* Return whether this trait collection, compared to a different trait collection, could show a different appearance
* for dynamic colors that are provided by UIKit or are in an asset catalog.
* If you need to be aware of when dynamic colors might change, override `traitCollectionDidChange` in your view or view controller,
* and use this method to compare `self.traitCollection` with `previousTraitCollection`.
*
* Currently, a change in any of these traits could affect dynamic colors:
* userInterfaceIdiom, userInterfaceStyle, displayGamut, accessibilityContrast, userInterfaceLevel
* and more could be added in the future.
*/
@available(iOS 13.0, *)
open func hasDifferentColorAppearance(comparedTo traitCollection: UITraitCollection?) -> Bool
}
extension UITraitCollection {
/*
* Returns an image configuration compatible with this trait collection.
*/
@available(iOS 13.0, *)
open var imageConfiguration: UIImage.Configuration { get }
}
UITraitCollection gets defined and immediately extended, and it is also a class that has no implementations for its methods.
If anybody can tell me what is it that I am missing I'd be really thankful!