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

Swift: Stored Properties in Extensions unrecognized selector exception

I have an extension of NSURLSessionDownloadTask, inside of this Extension, I created a stored property called 'id' using Objective C Associated Objects. When I try to set the property, I get 'an unrecognized selector sent to instance'. Here's my…
krypton36
  • 197
  • 2
  • 16
2
votes
1 answer

How to override instance method from protocol extension in Swift?

I'm trying to override an instance method from a protocol extension, and I'm having some trouble. For context, I'm making an iOS app with a lot of different UICollectionViews. These views get data from different databases (requiring different…
Colin McDonnell
  • 897
  • 1
  • 11
  • 17
2
votes
1 answer

Is it possible to add associated values to an existing Swift enum via extensions?

In Swift 3, say there is a system defined enum (i.e. I don't control the source code) like this: enum currentState: Int { case enabled case disabled case unknown } Is it possible to add an associated value to the existing members specifically…
Matt Weinecke
  • 602
  • 6
  • 17
2
votes
1 answer

How do I implement delegate methods through Swift extension

My project is a mix of Obj-C and Swift, and I'm trying to extend my AppDelegate class with Swift. So right now I have AppDelegate.m, AppDelegate.h, and AppDelegate.swift. Most of the methods are in the Obj-C file, but I'm trying to implement just…
Enrico Susatyo
  • 19,372
  • 18
  • 95
  • 156
2
votes
1 answer

Array extension, Ambiguous reference to member 'subscript'

I'm trying to write an extension to Array in Xcode's playground. I want to write a function that will modify the array so that it will be filled with 0s when the function is called. The code I'm attempting to use is this: import…
OhNo
  • 1,720
  • 1
  • 11
  • 7
2
votes
1 answer

Swift: Viewing full class API including extensions

How can you view the full API for a class including any defined extensions? If you open the class in Xcode and view the method list in the quick jump bar it doesn't include any methods defined in any existing extensions for that class. Ie: You…
Marcus Leon
  • 55,199
  • 118
  • 297
  • 429
2
votes
1 answer

How to create extension for all types that have ".contains"?

I'd like to create an extension off all types that have the .contains API. For example, I did this for strings, but would like to expand it for all types: func within(values: [String]) -> Bool { return values.contains(self) } With this, instead…
TruMan1
  • 33,665
  • 59
  • 184
  • 335
2
votes
1 answer

How to constrain function parameter's protocol's associated types

For fun, I am attempting to extend the Dictionary class to replicate Python's Counter class. I am trying to implement init, taking a CollectionType as the sole argument. However, Swift does not allow this because of CollectionType's associated…
BallpointBen
  • 9,406
  • 1
  • 32
  • 62
2
votes
1 answer

Swift generics in protocol: operator '===' cannot be applied to operands of type '_' and 'Self.T'

I'm trying to build a simple observer mixin with Swift 2. Here comes just the relevant part. protocol Observable{ typealias T var observers:[T] { get set } mutating func removeObserver(observer:T) } To create the mixin I use an…
hendra
  • 2,531
  • 5
  • 22
  • 34
2
votes
2 answers

Swift: cannot convert value of type 'Self' to expected argument type 'UnsafePointer'

I do a bunch of work with [Uint8] arrays. But I often have to recast these as NSData objects to interface with iOS frameworks such as CoreBluetooth. So I have lots of code that might look something like: var input:[UInt8] = [0x60, 0x0D, 0xF0,…
Travis Griggs
  • 21,522
  • 19
  • 91
  • 167
2
votes
1 answer

Swift why doesn't my generator protocol extension work?

I think my cognition for Swift types/protocols/generics has overflowed. I've been using the pattern of extending "an input stream bytes" by doing something like: extension GeneratorType where Element == UInt8 { func foobar() { ... …
Travis Griggs
  • 21,522
  • 19
  • 91
  • 167
2
votes
1 answer

Custom keyboard in iphone

Does any one know how to Create custom keyboard in iphone? Pls give me any samples for custom keyboard
Bala
  • 39
  • 1
  • 8
2
votes
1 answer

How does one subclass an extension locked inside a Pod?

I have Cocoapod library that offers its functionality via an extension like this: extension ExposedLibraryClass { class func setup () { ... } } I have some boilerplate code that goes into setup() for every app I do, but each app also needs some…
Joseph Beuys' Mum
  • 2,395
  • 2
  • 24
  • 50
2
votes
1 answer

How to access static variables in methods using Swift's 'abstract class'-like protocol extensions

I've been trying to make abstract superclass-like behaviour in Swift using the protocols and extensions suggested here: Abstract classes in Swift Language But I can't figure out how to write methods that make use of static (class) variables. For…
2
votes
2 answers

Why does my custom class not recognize swift extensions? (i.e. String)

I have a string extension in which I define a function that calculates the DamerauLevenshtein distance between two different strings. When adding the following code: var result = "Hello" result.damerauLevenshteinTo("Hellow") in any other class…
cyril
  • 3,020
  • 6
  • 36
  • 61