I'm NOT asking if these extensions are a good idea or not, it's just a thought experiment, I'm trying to learn from the practice.
Agreeing with Christian Lattner, that methods are generally preferable, I thought I'd play with being able to…
I'm trying to extend an array to return only objects at certain indexes. The map function seemed to be the best choice for me here.
extension Array {
func objectsAtIndexes(indexes: [Int]) -> [Element?]? {
let elements: [Element?] =…
Let's say for academic purposes, I would prefer an expression like
someInt.asDouble
rather than the stock
Double(someInt)
Since all of the various Swift integer types conform to the IntegerType protocol AND because there seems to be a Double()…
I have a Swift framework which is managed via Cocoapods and contains an extension like so:
public extension UIImage {
public static func maskedImageWithColor( color: UIColor, forImageNamed image: UIImage) {
// Implementation
…
How can a swift array be extended to access members of a particular type?
This is relevant if an array contains instances of multiple classes which inherit from the same superclass. Ideally it would enforce type checking appropriately.
Some…
I want to extend dictionary and constrain it to specific key type of NSDate with values of type Array
MyClass is a swift class with no subclass.
extension Dictionary where Key: NSDate, Value: Array{
func myFunction() -> Int{
…
I want to create an extension for the String class in Swift that allows you to get a substring via the subscript operator like in Python. This can be accomplished with the Range class in the following way
extension String {
subscript (range:…
I am creating an extension of UIFont in Swift that will return a custom font.
import UIKit
extension UIFont {
func museoSansRounded900(size: CGFloat) -> UIFont {
return UIFont(name: "MuseoSansRounded900", size: size)!
}
}
However, when I…
If I want to make an Array extension, but only do an extension for an Array of type [UIView], is that possible in Swift?
Currently I'm just doing a normal extension Array with a precondition that the Array only holds elements of type UIView, but it…
To an already extensive Objective-C app I wanted to add Core Data functionality. Recent additions are in Swift, and this worked well, up until now.
Because my Objective-C AppDelegate already contains quite some stuff, I decided to write a Swift…
I'm trying to create a simple Swift extension containing a calculated property. I don't understand why I'm getting this compile error (“declaration only valid at file scope”). The error is at the beginning of the "private extension OpStack" line.…
Structs in Swift can contain functions and can have extensions. Cool! Looks like a neat way to make this function available to more classes and to reduce argument passing. Here's the way the function originally looked in a map view controller:
…
As an exercise, I'm trying to extend Array in Swift to add a sum() member function. This should be type safe in a way that I want a call to sum() to compile only if the array holds elements that can be added up.
I tried a few variants of something…
I'll keep this as short and simple as I can.
I have an NSDate extension that I use for a number of things throughout my app, but I have run into a problem with (strangely) a very simple function.
The relevant part of the extension is:
import…
I'm learning Swift language now a days.
In Apple's documentation I saw a example for extension like:
extension Int: ExampleProtocol {
var simpleDescription: String {
return "The number \(self)"
}
mutating func adjust() {
…