Questions tagged [swift-extensions]

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

Apple Pre-release Docs

394 questions
-1
votes
1 answer

How do I get or set a value inside/outside an extension

I am new to Swift and I am trying to understand how could a class get a value inside an extension. I have two problems but they are similar to each other, but first I will explain my project. I am using 2 controllers (MainViewController and…
virtu
  • 39
  • 5
-1
votes
1 answer

Is it safe to extend an Objective-C class in Swift with non-prefixed methods?

In Objective-C, one should always prefix category methods, e.g. if extending UIView with the method descendants, you'd add zzz_ and make it zzz_descendants to avoid naming conflicts. Is that necessary for a function in extension UIView { ... } in…
meisel
  • 2,151
  • 2
  • 21
  • 38
-1
votes
1 answer

Does extension return new instance each time

Consider this case, a variable declared in an extension, like this. extension UIViewController { var apiClient: APIClient { return APIClientImplementation() } } Does this extension return a new instance each time i call it from a…
Mohmmad S
  • 5,001
  • 4
  • 18
  • 50
-1
votes
1 answer

Extension in Swift

I was reading open source project and I found extension for a specific view controller like : import UIKit class FirstViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() …
mazen
  • 438
  • 1
  • 4
  • 13
-1
votes
1 answer

Protocol extension in Swift 3

I want to have a default property of UIImageView, which would be isFlipped. I am able to do it by subclassing UIImageView and adding one property isFlipped. But I want to user protocol and extensions for this , but it is crashing after sometime.…
Sishu
  • 1,510
  • 1
  • 21
  • 48
-1
votes
1 answer

Create generic extension for Int, Double, CGFloat

I have one extension which helps me to range bound a value with min and max. There are different scenarios where I need to use a different type like Int, Float, Double, CGFloat. So for that, I have created multiple extensions like below. extension…
Parth Adroja
  • 13,198
  • 5
  • 37
  • 71
-1
votes
1 answer

iOS: UIToolbar or UIView for keyboard in swift

I am creating a UIView or a UIToolbar for my keyboard when I select a textfield. For example something like that in my viewDidLoad: let customView = UIView(frame: CGRectMake(0, 0, self.view.frame.size.width, 40)) customView.backgroundColor =…
cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241
-1
votes
1 answer

why Swift type system try to convert a type into wrong expected parameter

Using Xcode 8 beta, swift 3 the second extension can not compiled. I don't understand if this is a swift bug or a known limitation. extension Array { func scanl(initial: T, combine:(Iterator.Element, T) -> T) -> [T] { guard let first…
netanel
  • 31
  • 1
  • 4
-1
votes
2 answers

How to unwrap Dictionary in extension as Dictionary

I'm trying to unwrap dictionary in extension as Dictionary. But I'm having some problems and can't find answer anywhere. I have a function which gets a dictionary, where inside I want to get a json string from JSON dictionary, of course if it is a…
Jonauz
  • 4,133
  • 1
  • 28
  • 22
-1
votes
1 answer

SlideMenuControllerSwift implementation in Objective-C

I try to add SlideMenuControllerSwift in Objective-c. I can access [slideMenuController openLeft] in my AppDelegate.m - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UIStoryboard *sb…
-2
votes
1 answer

Override object method using Swift protocols and extensions

I would like to override a method that is declared in a framework and implemented in an extension. Here's what I've tried : import UIKit // Code from a dependency class Object { // UIViewController func printSomething() { …
Xys
  • 8,486
  • 2
  • 38
  • 56
-2
votes
1 answer

Get count of UIView instances shown on the UI

Is there a way to count the number of UIView instances created on UI without changing too much code? I need to improve performance, I feel some of the UIView instances are untracked. Is there a way to create an extension of UIView and always return…
zs2020
  • 53,766
  • 29
  • 154
  • 219
-2
votes
1 answer

How is swift allowing same method in protocol and protocol extension?

Here is the map method which seems to be duplicate in Sequence protocol. How is apple doing this? what is the intention behind this? public protocol Sequence { public func map(_ transform: (Self.Iterator.Element) throws -> T) rethrows ->…
user3869369
  • 137
  • 1
  • 11
-2
votes
2 answers

Parameter types not shown correctly from Swift extension

I am replacing some Obj-C in an app where there is a category on NSDictionary and I need the same functionality in a Swift extension. I have written the extension and have a clean build, however when trying to use the extension my parameter type is…
Pheepster
  • 6,045
  • 6
  • 41
  • 75
-2
votes
2 answers

Is it possible use non read-only computed property in extension?

Is it possible for a computed property in extension to have both getter and setter? Apple's guide does not mention it and the only example I have seen only shows read-only computed property in extension.
Boon
  • 40,656
  • 60
  • 209
  • 315
1 2 3
26
27