Questions tagged [swift-extensions]

Extensions add new functionality to an existing class, structure, or enumeration type.

Apple Pre-release Docs

394 questions
2
votes
1 answer

Idiomatic way to test Swift Optionals

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…
Travis Griggs
  • 21,522
  • 19
  • 91
  • 167
2
votes
1 answer

Swift 2.1 Array Extension objectsAtIndexes

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?] =…
Ron
  • 1,610
  • 15
  • 23
2
votes
1 answer

Protocol Extension type Self does not match initializer types

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()…
Travis Griggs
  • 21,522
  • 19
  • 91
  • 167
2
votes
1 answer

Swift extension in Cocoapods Framework causing Unrecognised Selector sent to Class

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 …
2
votes
4 answers

Extend Swift Array to Filter Elements by Type

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…
Anthony Mattox
  • 7,048
  • 6
  • 43
  • 59
2
votes
1 answer

Extending Dictionary with key and value constraints

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{ …
the Reverend
  • 12,305
  • 10
  • 66
  • 121
2
votes
1 answer

Complicated Swift Extension

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:…
bjornorri
  • 369
  • 1
  • 3
  • 11
2
votes
1 answer

Extension function asking for return type instead of parameter

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…
SleepNot
  • 2,982
  • 10
  • 43
  • 72
2
votes
0 answers

Swift Array extension only for certain type of Array

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…
Adam Johns
  • 35,397
  • 25
  • 123
  • 176
2
votes
0 answers

Adding optional method of objective-C class to swift extension

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…
2
votes
1 answer

Swift extension declaration error: "declaration only valid at file scope"

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.…
2
votes
1 answer

Swift struct extension function - "Cannot assign to 'origin' in 'self'"

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: …
Adam Eberbach
  • 12,309
  • 6
  • 62
  • 114
2
votes
1 answer

Generalized type constraints with Swift

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…
Jean-Philippe Pellet
  • 59,296
  • 21
  • 173
  • 234
2
votes
1 answer

Swift Extension, Missing Argument For Parameter #1 In Call

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…
2
votes
1 answer

Immutable value of type `Int` only has mutating members named adjust

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() { …
Midhun MP
  • 103,496
  • 31
  • 153
  • 200