Questions tagged [swift-extensions]

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

Apple Pre-release Docs

394 questions
0
votes
1 answer

Extending a Generic object with a Generic Object whose generic is a Protocol

I want to create an extension in swift to an Array where the Element if of ObjectWithGenerics whose Generic conforms to a protocol so that no matter what object I create and have in an array as long as it conforms to my protocol and is of type…
0
votes
1 answer

Cannot use Array initializer in an extension function for an array type in swift

Using Swift 5.7.2 and Xcode 14.2 I am attempting to write an extension function to an array of a certain type i.e. [MyClass]. Inside the function I would like to be able to use the Array() initializer to convert a set to an array, but I am not able…
0
votes
1 answer

Generating unique ids for each implementor of a protocol

I have the following I want to achieve in Swift: I have a protocol protocol IdProtocol { static var id: Int { get } } Now I want each implementor of this potocol to have a different value for id. For example: struct One: IdProtocol {} struct Two:…
Jomy
  • 514
  • 6
  • 22
0
votes
1 answer

Is it possible to Extend functions in swift

I would like to ask if it's possible to extend functions or have some way to use them like this. Because I don't want to open task closure for every async function. I couldnt find any Nominal Type for functions in swift to extend. func…
0
votes
0 answers

Extend ObjC class with Swift extension Causes crashes when released to Appstore

I have an objective project and when I'm extending the project to Swift, It works fine when locally testing it and also when testing it over testflight. But after submitting the app to the appstore and when users download it and use, I see crash…
0
votes
1 answer

does js/ts has something like impl in Rust or extension in Swift?

does js/ts has something like impl in Rust or extension in Swift? this is a good pratice to implement method for object with a specific shape. and we can replace userSetAge(user, x => x + 1) to user.setAge(x => x + 1). Not only that, it can still…
eczn
  • 1,459
  • 2
  • 10
  • 15
0
votes
1 answer

how to extend a swift protocol with an extension so that classes get an implemented variable defined by the protocol

Circumstances The following class: class User { var name = "Max" var surname = "Mustermann" } and the following protocol: protocol Sharable { func share(name: String) var isSharable: Bool { get set } } Intention Extend the protocol…
WalterBeiter
  • 2,021
  • 3
  • 23
  • 48
0
votes
0 answers

UIViewController + NSNotification extension protocol based @objc issue

I use this example to avoid keyboard frame when it appears. Now I want to make it protocol based to avoid extending all view controllers with this functionality. Now I am getting two errors which looks like conflict each other (one is want to add…
Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277
0
votes
1 answer

How to invalidate timer in extension in Swift

I was trying to add a timer property in an extension. The code is as follow @objc public extension RCTTouchHandler { static let kSessionTimer = "sessionTimer" var sessionTimer: Timer { get { return objc_getAssociatedObject(self,…
RY_ Zheng
  • 3,041
  • 29
  • 36
0
votes
1 answer

Extension for UITABLEVIEWCELL not working in swift

I had created extension : public extension UITableViewCell { /// Generated cell identifier derived from class name static func cellIdentifier() -> String { return String(describing: self) } var…
0
votes
1 answer

How do I use extensions to extend an object's tableview's functionality from the calling class?

Overview I'm trying to better understand how extensions work. In my app I have a ViewController. There I put a view of another class. In this custom class I put a bunch of buttons and a table view. I want them to display some text inside of my…
crost
  • 165
  • 1
  • 13
0
votes
3 answers

New stored property always returns nil | iOS Swift

A new stored property created for exisiting class using extension. While getting value its always returning nil value. The below code is what i tried to add new stored property. var IdentifiableIdKey = "kIdentifiableIdKey" extension EKEvent { …
Code cracker
  • 3,105
  • 6
  • 37
  • 67
0
votes
2 answers

Data.subdata(in:) results in EXC_BAD_INSTRUCTION

While trying to retrieve subdata of a Data object, the application crashes issuing the following error: Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) Below you can see the code. It's a Data extension. Hope someone can explain…
Bram
  • 2,718
  • 1
  • 22
  • 43
0
votes
0 answers

Case insensitive search of elements of NSDictionary in Swift

It is possible to do a caseinsensitive search of keys in a Swift Dictionary in Swift using: extension Dictionary where Key == String { func valueForKeyInsensitive(key: Key) -> T? { let foundKey = self.keys.first { $0.compare(key,…
zztop
  • 701
  • 1
  • 7
  • 20
0
votes
1 answer

Extended capability for a more restrictive generic

I have a generic binary search tree based on Comparable: public class BSTree { public func insert(_ val: T, _ n: Int) { // ... } @discardableResult public func delete(_ val: T, _ n: Int) -> Int { //…
Rick Clark
  • 33
  • 1
  • 6