I've been working on an iOS application in Swift (much of it being moved from Objective-C). I'm using Core Data and trying to use extensions to add functionality to classes auto-generated from my model. One thing I readily did in Objective-C was…
Say I have a Framework called SwiftKit, which has a UIView's extension class method named someClassMethod and a property named someProperty within it:
// SwiftKit
public extension UIView {
class func someClassMethod() {
…
Extension cannot contain stored property, but why then can static stored property be defined within extension?
I also didn't find any documentation mentioning that static property is allowed in extension.
extension String {
static let test =…
Hi there =) I was just faced with a design problem where I need to (essentially) do the following:
I want to inject a bit of code on viewWillAppear: of any UIViewController subclass that conforms to a protocol MyProtocol. Explained in code:
protocol…
Say you have
class Fancy:UIView
you want to find all sibling Fancy views. No problem...
for v:UIView in superview!.subviews
{
if let f = v as? Fancy
{ f.hungry = false }
}
So, try an extension,
public…
I started writing Swift extensions on my view controllers. So I have three files right now:
My header file, ViewController.h:
@interface MyViewController : UIViewController
@end
My Obj-C implementation file, ViewController.m:
@interface…
I'd like to write an extension for tuples of (e.g.) two value in Swift. For instance, I'd like to write this swap method:
let t = (1, "one")
let s = t.swap
such that s would be of type (String, Int) with value ("one", 1). (I know I can very easily…
In Swift, I have historically used extensions to extend closed types and provide handy, logic-less functionality, like animations, math extensions etc. However, since extensions are hard dependencies sprinkled all over your code-base, I always think…
To be very frank, I am totally new to learn Extension creation and usage.
I wanted to create a category (Extension in swift 3.0) which can be used throughout an application to perform repeated operations for Array.
Sample Link 1
This is what I have…
Swift Programming Language has this to say about access control for extension:
You can extend a class, structure, or enumeration in any access
context in which the class, structure, or enumeration is available.
Any type members added in an…
I have an extension on UIView implementing a protocol
protocol SomeProtocol {
var property : Int
}
extension UIView : SomeProtocol {
var property : Int {
get {
return 0
}
set {
// do nothing
…
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…
let numberSet = Set(1...11)
let divideSet = numberSet.map({ $0 / 10 })
//Error: Set does not have a member named map :(
Swift 1.2 supports Set() for unordered collections, but map(_:) doesn't seem to work on Sets, so i decide to get smart…
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 which declared and used public (class) methods/properties in…
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 =…