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

Swift Generated Header Missing Objective-C Property

I have a core data generated class called Message. Message has a message property that for some reason is inaccessible in Swift. You can clearly see the message property here in the header file In a Swift extension of Message I'm unable to access…
0
votes
2 answers

Create Image Array with Images in Cache (Swift)

I am relatively new to iOS and would appreciate any help figuring out how to create an array of images when using an extension to help manage downloads and caching. I've been going around in circles with this for a couple hours so thought it was…
Ben
  • 3,346
  • 6
  • 32
  • 51
0
votes
2 answers

Swift Array extension with determined Element type

extension Array where Element: StringLiteralConvertible{ func spliteByPrefix() -> [Element]{ for item in self{ } return [] } } I want to write an extension of Array whose Element is always a String. And in my…
jhd
  • 1,243
  • 9
  • 21
0
votes
1 answer

Refreshing UITableViewCell layout from its imageView after fetching image

I have UIImageView extension that is downloading image from given url in background. I'm using it to fetch images for UITableViewCell. After successful download I'm calling self.setNeedsLayout() but it does not refresh layout of cell since this…
Prettygeek
  • 2,461
  • 3
  • 22
  • 44
0
votes
2 answers

Changing return type to a decimal?

I am trying to convert the current values that I am dealing with to hundredths. Here is my code: extension String { var value: Int { if let found = Array("abc".characters).indexOf(Character(lowercaseString)) { return found +…
Bigfoot11
  • 911
  • 2
  • 11
  • 25
0
votes
1 answer

How to create function variables with parameter in extensions?

When working with Swift - Extensions As you know, we can create variables for function. i want to create a variable which can hold reference to a function which has parameters. i have managed to create variable for a function which didn't have any…
Pawan Joshi
  • 1,581
  • 3
  • 20
  • 40
0
votes
2 answers

Understanding Swift extension usage in your API design

The Alamofire API has extensions such as extension Request in ResponseSerialization.swift. When designing a Swift API why would you take this approach as opposed to just adding these methods to the Request class (Request.swift)? I understand the…
Marcus Leon
  • 55,199
  • 118
  • 297
  • 429
0
votes
1 answer

Switch Generic Numeric Function with Protocol

This is working great for Doubles and Floats. But getting other numerals like Ints involved is proving really difficult. public protocol TemperatureConvertable: FloatLiteralConvertible, CustomStringConvertible { func +(lhs: Self, rhs: Self) ->…
Ryan Poolos
  • 18,421
  • 4
  • 65
  • 98
0
votes
2 answers

Struggling to understand the code - which tries to return the decimal digit 'n' places in from the right of the number

I'm new to Swift and is trying to learn the concept of extension. I saw this code in "the swift programming language", which tries to return the decimal digit 'n' places in from the right of the number. The code work fine, but I am struggling to…
Thor
  • 9,638
  • 15
  • 62
  • 137
0
votes
1 answer

Accessing swift class extension methods from within original class

The below code does not compile for me: import Foundation @objc public protocol MyProtocol { func protocolMethod(parameter: (String)) -> String func anotherProtocolMethod() -> Int } class MyClass: NSObject { var myValue =…
0
votes
1 answer

How to break down a class conforming to multiple interfaces with shared instance variables?

class X: Interface1, Delegate1, Delegate2 { var sharedVariable = true .... very long code comes here, that implements the delegates ... } How can I now break down this long class? I was thinking of using extensions, something like extension…
Novellizator
  • 13,633
  • 9
  • 43
  • 65
0
votes
1 answer

How to rewrite description property in NSDictionary by extension in swift?

I want to rewrite the description property in NSDictionary by textension in swift, but can't figure how. This is very easy and common in Objective-C by using category, I can just do as this: @implementation NSDictionary (Log) - (NSString…
0
votes
2 answers

How to make extension both outlet (swift 2)

I created extension for UITextField. And I need the same extension for UITextView. How to make this extension available for all other views? My extension code: extension UITextField { func addTopBorderWithColor(color: UIColor, height: CGFloat)…
Clever
  • 401
  • 1
  • 6
  • 17
0
votes
2 answers

Swift: Generic Protocols

I have some swift structs for which protocol compliance is generated with individual extensions with equal methods names which just differ in their return types which are struct dependent. On top of That I want to use them in a generic function…
hakkurishian
  • 266
  • 1
  • 3
  • 13
0
votes
1 answer

UICollectionview in a keyboard extension

I have tried to get a UICollectionview into my keyboard extension, which i made in swift. But i can´t get it to work. The keyboard just crashes on launch. Anyone know how to fix this? Or is it even possible to use a UICollectionview in a keyboard…