I tend to only put the necessities (stored properties, initializers) into my class definitions and move everything else into their own extension, kind of like an extension per logical block that I would group with // MARK: as well.
For a UIView…
I searched some posts, I think I cannot write an extension under Swift, and call it from Objective-C code, right?
@objc like attributes only support methods, class, protocols ?
You can create a String extension like so:
extension String {
func someFunc() -> Bool { return true }
}
but what if you want it to apply to optional string?
var optionalString: String? = ""
optionalString!.someFunc() /* String? does not have a…
I want to create category of my existing swift class, but there is no option in IDE to do so.
Any idea if category exists in swift project? Or how to achieve similar functionality in swift project?
I'd like to use the Accelerate framework to extend [Float] and [Double] but each of these requires a different implementation.
I tried the obvious:
extension Array {
}
and get this error:
"Constrained extension must be declared on the…
protocol Car {
var wheels : Int { get set}
init(wheels: Int)
}
extension Car {
init(wheels: Int) {
self.wheels = wheels
}
}
on self.wheels = wheels i get the error
Error: variable 'self' passed by reference before being…
Set is an unordered collection of unique elements. Almost similar to array.
I want to add/insert multiple elements in a Set of String. But there is only single method provided that can insert only one element (accepts single Set element as a…
Not sure what's going on here, this seems like it should be pretty straight forward. I have a protocol that mutable var, an extension with a mutating function. Things are crapping out in the testClass.testFunc, when I try and use mtkAnimQueAppend…
I'm trying to write an extension to Array which will allow an array of optional T's to be transformed into an array of non-optional T's.
e.g. this could be written as a free function like this:
func removeAllNils(array: [T?]) -> [T] {
return…
I saw so many examples with below format
extension Protocolname where Self: UIViewController
What is where Self in protocol extension. I couldn't find the documentation on this.
I have an extension:
extension UILabel {
func animateHidden(flag: Bool) {
self.hidden = flag
}
}
I need to make the same one for UIImageView but I don't want to copy that whole code. Is it possible to make an extension for multiple…
As of Swift 2.0 it seems we can get closer to extensions of generic types applicable to predicated situations.
Although we still can't do this:
protocol Idable {
var id : String { get }
}
extension Array where T : Idable {
...
}
...we can…
I'm trying to make this extension:
extension UIViewController
{
class func initialize(storyboardName: String, storyboardId: String) -> Self
{
let storyboad = UIStoryboard(name: storyboardName, bundle: nil)
let controller =…
Assume we have the following example code:
protocol MyProtocol {
func someFunction()
}
public class MyClass {
}
public extension MyClass: MyProtocol {
func someFunction() {
print("hello")
}
}
Compiling the code above gives…