Questions tagged [swift-extensions]

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

Apple Pre-release Docs

394 questions
1
vote
1 answer

How to override debugDescription for NSDate objects in Swift

I'm trying to override the debugDescription property when displaying the value of NSDate objects in the Xcode debugger. My extension code: import Foundation extension NSDate { public override var debugDescription: String { return…
GusP
  • 2,454
  • 2
  • 23
  • 32
1
vote
1 answer

Swift Array extension with optionals

I've created simple extension for Array which will append only unique elements. It works fine until I want to work with an array of optionals. Since that I'm keep getting error Type 'Event?' does not conform to protocol 'Equatable' Event…
Błażej
  • 3,617
  • 7
  • 35
  • 62
1
vote
2 answers

Extension of UIView

My code is written in Swift 2.0 and with a deployment target of iOS 8.0 or later. I am trying to extend UIView so that instead of using UIView.animateWithDuration(), I can use my custom function. I want to take a UIView that's a circle and keep it…
Cody Weaver
  • 4,756
  • 11
  • 33
  • 51
1
vote
1 answer

Failing to implement Protocol Extension for conforming type

(feel free to retitle question as appropriate) I'm working with a lot of BLE data, and for debugging purposes, I've found it easy to extend UInt8 with a HEX computed variable: extension UInt8 { var HEX:String { return String(format:…
Travis Griggs
  • 21,522
  • 19
  • 91
  • 167
1
vote
1 answer

Difficulty understanding `Associated Types` in Swift protocol extensions

I'm struggling to understand protocols and protocol extensions in swift. I'm wanting to define a series of protocols that can be applied to a class, along with a set of protocol extensions to provide default implementations. Example code: // MARK:…
So Over It
  • 3,668
  • 3
  • 35
  • 46
1
vote
1 answer

Extension of array with Elements conforming a protocol in swift 2.0

i'm trying to create an extension for the Array struct in order to add methods just if the contained objects conforms to a particular protocol, but i'm having strange behaviors when i try to access to a method in the extension from a class. here is…
Giuseppe Lanza
  • 3,519
  • 1
  • 18
  • 40
1
vote
2 answers

Swift Array extension with where clause doesn't work on sub-protocols

I've written some code that I think ought to work in Swift 2 (Xcode 7b4), but it's not compiling. I'm hoping to get some thoughts on whether what I'm trying to do should be valid. Consider this example Array extension: extension Array where…
par
  • 17,361
  • 4
  • 65
  • 80
1
vote
2 answers

Swift 1.2 Extensions

I upgraded XCode to 6.3 / Swift 1.2 and I'm seeing errors with extensions. Below is my extension to UINavigationItem for hiding the backButton text. extension UINavigationItem { func backBarButtonItem() -> UIBarButtonItem { return…
Arasan Rajendren
  • 265
  • 1
  • 2
  • 7
1
vote
1 answer

Declaring conformance to @objc protocol in empty extension breaks with EXC_BAD_INSTRUCTION

Been having lots and lots of trouble with Swift protocols in combination with arrays, but I couldn't even reproduce my whole problem before things started to break in playground. Here's a minimal example. I have two protocols and a class Bus which…
Andreas
  • 2,665
  • 2
  • 29
  • 38
1
vote
1 answer

When to use the generic parameter clause

I'm new to generics in swift, and while reading some books, I came across something I don't understand. In generic functions, when is it appropriate to use the type parameter (the right after the function name)? and when is it inappropriate? Here…
1
vote
2 answers

Swift is there a method that gives the index of a substring inside another string

Is there any existing function that looks for the index of a substring inside another string? A method like .indexOfSubstring thank does this: let word: String = "Hey there, how are you?" let indexOf: Int = word.indexOfSubstring("ere, how…
Matt
  • 773
  • 2
  • 15
  • 32
1
vote
2 answers

I made a Swift extension to use frames like I would on iOS but Can't compile while using it

I made this extension to NSView to make it easier to work with views. import Cocoa public extension NSView { var invertedFrame: NSRect { get { return NSRect(x: frame.origin.x, y: superview.bounds.height -…
o.uinn
  • 838
  • 1
  • 8
  • 13
1
vote
2 answers

A bug ? Property in an extension of UIView

I created an extension for the UIView: import UIKit extension UIView { var frameHeight : CGFloat { get { return self.frame.size.height } set(newHeight) { self.frame =…
Bagusflyer
  • 12,675
  • 21
  • 96
  • 179
0
votes
1 answer

How to assign updated value to Text UI in extension function SwiftUI

How to assign updated value to Text value in Extension SwiftUIenter image description here extension Text { public func translate(text: String) -> Text{ var translated = "" let conditions =…
Akshay
  • 1
  • 1
0
votes
1 answer

Sendability of function types in property does not match requirement in protocol

I am trying to add a protocol which has all the attributes and functions of a Process but I see a warning with terminationHandler. I was hoping not to see this warning since @Sendable is already added to terminationHandler. protocol ProcessProtocol…