Swift extension clause where this or that - ios

I want an extension for two classes UITextField and UITextView and the code is identical, but I have trouble coming up with an extension that will work for them both.
I am using ReactiveCocoa and I currently have this
import UIKit
import ReactiveCocoa
import enum Result.NoError
typealias NoError = Result.NoError
// How to DRY up this code?
extension UITextField {
func textSignalProducer() -> SignalProducer<String, NoError> {
return self.rac_textSignal()
.toSignalProducer()
.map { $0 as! String }
.flatMapError { error in SignalProducer<String, NoError>(value: "") }
}
}
extension UITextView {
func textSignalProducer() -> SignalProducer<String, NoError> {
return self.rac_textSignal()
.toSignalProducer()
.map { $0 as! String }
.flatMapError { error in SignalProducer<String, NoError>(value: "") }
}
}
How would I write an extension that would work for both? I was trying to do something like
protocol TextSignalProducer {}
extension TextSignalProducer where Self: ???? {
// Same code as is duplicated in both current extensions...
}
but I have no idea how to specify Self as either UITextField or UITextView. Something like where Self == UITextField || Self == UITextView would probably make this possible.
Is there a nice way to accomplish what I want to try? Is this really necessary (I don't know the naming conventions for protocols/extensions)
import UIKit
import ReactiveCocoa
import enum Result.NoError
typealias NoError = Result.NoError
protocol TextSignal {
func rac_textSignal() -> RACSignal!
}
extension UITextField: TextSignal, TextSignalProducer {}
extension UITextView: TextSignal, TextSignalProducer {}
protocol TextSignalProducer {}
extension TextSignalProducer where Self: TextSignal {
func textSignalProducer() -> SignalProducer<String, NoError> {
return self.rac_textSignal()
.toSignalProducer()
.map { $0 as! String }
.flatMapError { error in SignalProducer<String, NoError>(value: "") }
}
}
I am using Swift 2.1, Xcode 7.2 and ReactiveCocoa 4.0.1

You can cut down your proposed solution to a single dummy protocol:
protocol TextSignalProducer {
func rac_textSignal() -> RACSignal!
}
extension TextSignalProducer {
func textSignalProducer() -> SignalProducer<String, NoError> {
return self.rac_textSignal()
.toSignalProducer()
.map { $0 as! String }
.flatMapError { error in SignalProducer<String, NoError>(value: "") }
}
}
extension UITextField: TextSignalProducer {}
extension UITextView: TextSignalProducer {}
I don't think there's a more concise way than that, though. UITextField and UITextView's rac_textSignal() implementations have nothing in common.

UITextView and UITextField conform to UITextInput protocol. If rac_textSignal base on this protocol (I'm not sure because I don't have any project with RactiveCocoa at hand :) ) you can do this:
protocol Cos {
func textSignalProducer() -> String
}
extension UITextView: Cos {
}
extension UITextField: Cos {
}
extension Cos where Self: UITextInput {
func textSignalProducer() -> String {
return "dsfsdf"
}
}
let cos = UITextView()
cos.textSignalProducer()
let cos2 = UITextField()
cos2.textSignalProducer()

Related

Implement protocol variable conforming to single protocol as variable conforming to several protocols

I'm trying to do a simple thing, but the code gives an error: Type 'MyClass' does not conform to protocol 'MyProtocol3'.
protocol MyProtocol1 {
}
protocol MyProtocol2 {
}
protocol MyProtocol3 {
var output: MyProtocol2 { get }
}
class MyClass: MyProtocol3 {
var output: MyProtocol2 & MyProtocol1
init(output: MyProtocol1 & MyProtocol2) {
self.output = output
}
}
Conforming MyProtocol2 to MyProtocol1 also doesn't do the trick. Is it possible to use variable conforming to multiple protocol as another protocol var?
You say, "Conforming MyProtocol2 to MyProtocol1 also doesn't do the trick." Unless you have some trick in mind that you haven't mentioned, it looks to me like the answer.
The following works in a playground:
protocol MyProtocol1 {
func test1()
}
protocol MyProtocol2: MyProtocol1 {
func test2()
}
class OutputClass: MyProtocol2 {
func test1() {
print("test1")
}
func test2() {
print("test2")
}
}
protocol MyProtocol3 {
var output: MyProtocol2 { get }
}
class MyClass: MyProtocol3 {
var output: MyProtocol2
init(output: MyProtocol2) {
self.output = output
}
}
let o = OutputClass()
o.test1()
o.test2()
let m = MyClass(output: o)

Type 'MyWeights' does not conform to protocol 'NSCopying'

I am using Metal Performance Shader to set up a neural network, and encountered the issue when writing the weights initialization class: Type 'MyWeights' does not conform to protocol 'NSCopying'. What caused the error, and how to fix this?
PS. I tried to fix it by adding the copy() function, however I do not know what to return or what it means.
import Foundation
import MetalPerformanceShaders
class MyWeights: NSObject, MPSCNNConvolutionDataSource {
//Error: Type 'MyWeights' does not conform to protocol 'NSCopying'
/*
func copy(with zone: NSZone? = nil) -> Any {
return self
}
*/
let name: String
let kernelWidth: Int
let kernelHeight: Int
let inputFeatureChannels: Int
let outputFeatureChannels: Int
var data: Data?
init(name: String, kernelWidth: Int, kernelHeight: Int,
inputFeatureChannels: Int, outputFeatureChannels: Int,
useLeaky: Bool = true) {
self.name = name
self.kernelWidth = kernelWidth
self.kernelHeight = kernelHeight
self.inputFeatureChannels = inputFeatureChannels
self.outputFeatureChannels = outputFeatureChannels
}
func dataType() -> MPSDataType {
return .float32
}
func descriptor() -> MPSCNNConvolutionDescriptor {
let desc = MPSCNNConvolutionDescriptor(kernelWidth: kernelWidth,
kernelHeight: kernelHeight,
inputFeatureChannels: inputFeatureChannels,
outputFeatureChannels: outputFeatureChannels)
return desc
}
func weights() -> UnsafeMutableRawPointer {
return UnsafeMutableRawPointer(mutating: (data! as NSData).bytes)
}
func biasTerms() -> UnsafeMutablePointer<Float>? {
return nil
}
func load() -> Bool {
if let url = Bundle.main.url(forResource: name, withExtension: "dat") {
do {
data = try Data(contentsOf: url)
return true
} catch {
print("Error: could not load \(url): \(error)")
}
}
return false
}
func purge() {
data = nil
}
func label() -> String? {
return name
}
}
It's telling you exactly what to do.
You need to declare that your class conforms to the NSCopying protocol, and then you need to implement the only function in that protocol, copy(with:)
class MyWeights: NSObject, MPSCNNConvolutionDataSource, NSCopying {
func copy(with zone: NSZone? = nil) -> Any {
return MyWeights(
name: self.name,
kernelWidth: self.kernelWidth,
kernelHeight: self.kernelHeight,
inputFeatureChannels: self.inputFeatureChannels,
outputFeatureChannels: self.outputFeatureChannels,
useLeaky: self.useLeaky)
}
//The rest of your class
}
You have to implement the entire NSCopying protocol
class MyWeights: NSObject, MPSCNNConvolutionDataSource, NSCopying {
init(/* ... */) {
// your init...
super.init() // NSObject init
}
// add this method
func copy(with zone: NSZone? = nil) -> Any {
return super.copy() // NSObject copy
}
//The rest of your class
}
The convolution data source protocol has changed over the years and recently added the NSCopying protocol so that the MPSCNNConvolution can itself conform to NSCopying. It is possible that some sample code didn’t move forward. MPS should be checking available selectors so that this isn’t a binary compatibility issue, but moving forward, your protocol adopters should conform to NSCopying so that the framework can behave as advertised. The answers above demonstrate how.

Generic `getText()` function for UI elements in Swift

For a lot of actions, I need to check if the content of label.text, textView.text, textField.text etc. is nil.
So I created some extensions:
extension UILabel {
func getText() -> String {
return self.text ?? ""
}
}
extension UITextField {
func getText() -> String {
return self.text ?? ""
}
}
extension UITextView {
func getText() -> String {
return self.text ?? ""
}
}
The extensions are very redundant. A similar case is when I need to cast an Int, Double, Float etc. to another number format. What I want is a simple toInt() with a return of -1 or 0 when something went wrong.
So how can I create a generic function for the toString() or toInt()? I read about extensions and generics in the Apple documentation, but I didn't see a solution for my problem.
Finally, I tried to expand UIView with the same extension, because it's a superclass of UILabel etc., but I can't call getText().
So what is a good method to create generic extensions?
There are a couple of dubious things here: I wouldn't want any toInt() function to just return -1 or 0 when things went wrong. Swift has a good optionality system for a reason, and there are several obvious pitfalls introduced by returning -1 or 0. I also don't know what you plan to do for implementing getText() on a UIView. Lots of views don't have text. I don't know what this implementation would mean or do.
So I'll ignore those two details and focus on the primary question, which seems to be the redundancy of your extensions. There is a cleaner way, via protocol extensions, to cut down on duplication.
protocol TextProviding {
var text: String? { get }
}
extension TextProviding {
// Following your example here. I would prefer calling
// this a `var textOrEmpty: String` but same idea.
func getText() -> String {
return text ?? ""
}
}
// To conform additional types, you just need 1 line
extension UILabel: TextProviding { }
extension UITextField: TextProviding { }
EDIT: as some have pointed out, using the above code with extension UITextView: TextProviding { } will not work, because UITextView's text is a String!, not a String?. Unfortunately, this means if you want this to work for UITextView as well, you should rename the var text requirement to something else (and this means you will need a couple extra lines to manually conform UILabel and UITextField).
protocol TextProviding {
var string: String? { get }
}
extension TextProviding {
var stringOrEmpty: String {
return string ?? ""
}
}
extension UILabel: TextProviding {
var string: String? { return text }
}
extension UITextField: TextProviding {
var string: String? { return text }
}
extension UITextView: TextProviding {
var string: String? { return text }
}
For the first problem, you should only extend UIView and check whether its a label, a text field or a text view with if let.
extension UIView {
func getText() -> String {
if let label = self as? UILabel {
return label.text ?? ""
} else if let textField = self as? UITextField {
return textField.text ?? ""
} else if let textView = self as? UITextView {
return textView.text ?? ""
}
return ""
}
}
The advantage of this method over creating a protocol is that you can easily extend it, even if the text/title is retrievable with another method, for example, with a UIButton.
[...]
else if let button = self as? UIButton {
return button.title(for: .normal) ?? ""
}
This one-liner reduces redundant code (what you wanted) than all other answers posted here. No need for crazy condition-checks or multiple extensions.
extension UIView {
func getText() -> String? {
return self.responds(to: #selector(getter: UILabel.text)) ?
self.perform(#selector(getter: UILabel.text))?.takeUnretainedValue() as? String : nil
}
}
Create a protocol with an extension and then extend every class to adopt the protocol :)
protocol GetTextProtocol {
var text: String? {get}
func getText() -> String
}
extension GetTextProtocol {
func getText() -> String {
return self.text ?? ""
}
}
extension UILabel: GetTextProtocol {
var text: String? {
return self.text
}
}
extension UITextView: GetTextProtocol {
var text: String? {
return self.text
}
}

Swift switch between generics type and protocol conformance

I want to reach this goal:
func parse<T>(element: Any?) -> [T] {
// if T is kind of MyProtocol, return get result
// else
let array = [T]()
//do some stuff
return array
}
func get<T: MyProtocol>(obj: Any?) -> [T] {
return //some other stuffs
}
Is it possible in Swift language?
EDIT:
I have a class, let's say Parser, with some properties. I want a unique function signature, but the executed code must vary in base of property type.
class Parser: ParserProtocol {
let property1 : [MyClass1] = parse(element: elem1)
let property2 : [MyClass2] = parse(element: elem2)
}
protocol ParserProtocol {
func parse<T>(element: Any?) -> [T]
}
is this something you could use?
protocol GenericsTypeProtocol {
func callParseLogic() -> Void
}
protocol MyProtocol : GenericsTypeProtocol {}
extension GenericsTypeProtocol {
func callParseLogic() -> Void {
print("Generic logic called")
}
}
extension GenericsTypeProtocol where Self : MyProtocol {
func callParseLogic() -> Void {
print("MyProtocol logic called")
}
}
class GenericClass : GenericsTypeProtocol { }
class MyClass : MyProtocol { }
class MixedClass : GenericsTypeProtocol, MyProtocol {}
let a = GenericClass()
a.callParseLogic() //prints: Generic logic called
let b = MyClass()
b.callParseLogic() //prints: MyProtocol logic called
let c = MixedClass()
c.callParseLogic() //prints: MyProtocol logic called

Subclassing a Typhoon assembly to override methods not working

I am subclassing a Typhoon assembly such that stubbed implementations are returned, for unit testing purposes.
My assembly looks something like this:
class RealAssembly : TyphoonAssembly {
public dynamic func instanceToStubOut() -> AnyObject {
return TyphoonDefinition.withClass(SomeRealWorldClass.self)
}
public dynamic func instanceToTest() -> AnyObject {
return TyphoonDefinition.withClass(ClassToTest.self, configuration: { (definition : TyphoonDefinition!) -> Void in
definition.useInitializer("initWithObjectToStub:", parameters: { (initializer : TyphoonMethod!) -> Void in
initializer.injectParameterWith(self.instancetoStubOut())
})
})
}
}
My test class is solely for testing the instance of type ClassToTest, and I want to test it with the initializer-injected dependency on the object of type SomeRealWorldClass to be stubbed out. So I subclass RealAssembly so that instanceToStubOut() is overridden to return my stub object.
class MyTestClass : XCTestCase {
var assembly : TestAssembly!
class TestAssembly : RealAssembly {
override dynamic func instanceToStubOut() -> AnyObject {
return TyphoonDefinition.withClass(TestClass.self)
}
}
#objc
class TestClass : NSObject, ClassToStubOut {
func methodToStubOut() { /* do nothing */ }
}
override func setUp() {
super.setUp()
self.assembly = TestAssembly().activate()
}
override func tearDown() {
self.assembly = nil
super.tearDown()
}
func testStuff() {
let testingInstance = self.assembly.instanceToTest()
XCTAssertTrue(testingInstance.doStuff(), "doStuff returns true")
}
}
I expected this to work, but it doesn't. Typhoon seems to inject an uninitialized object instead of ever calling TestAssembly.instanceToStubOut()
Am I doing something wrong? Should I take a different approach?
EDIT: Here is some code you can paste into a Swift Playground that demonstrates the problem. The last line shows c.otherInstance returning nil :
import Typhoon
#objc
class BaseClass : NSObject {
var otherInstance : OtherProtocol!
func doIt() -> String {
return self.otherInstance.doStuff()
}
}
#objc
protocol OtherProtocol {
func doStuff() -> String
}
#objc
class OtherImpl : NSObject, OtherProtocol {
func doStuff() -> String {
return "OtherClass"
}
}
#objc
class StubClass : NSObject, OtherProtocol {
func doStuff() -> String {
return "Stubbed out"
}
}
class BaseAssembly : TyphoonAssembly {
dynamic func baseObject() -> AnyObject {
return TyphoonDefinition.withClass(BaseClass.self,
configuration: { (def : TyphoonDefinition!) -> Void in
def.injectProperty("otherInstance", with: self.otherObject())
})
}
dynamic func otherObject() -> AnyObject {
return TyphoonDefinition.withClass(OtherImpl.self)
}
}
var assembly = BaseAssembly()
assembly.activate()
var b = assembly.baseObject() as! BaseClass
b.doIt()
#objc
class TestAssembly : BaseAssembly {
override func otherObject() -> AnyObject {
return TyphoonDefinition.withClass(StubClass.self)
}
}
var testAssembly = TestAssembly()
testAssembly.activate()
var c = testAssembly.baseObject() as! BaseClass
c.otherInstance // this shouldn't be nil
Edit:
While patching is an option, as outlined in #Herman's answer below, what was attempted in the question is a supported feature, however there was a regression bug preventing it from working correctly.
The regression bug has been fixed in Typhoon 3.2.2 and so now both patching and overriding an assembly are again options for configuring Typhoon for a particular use-case.
Patching
There is a patching feature for this purpose in Typhoon. Look here.
For example:
class StubClass : NSObject, OtherProtocol {
#objc func doStuff() -> String {
return "Stubbed out"
}
}
let assembly = BaseAssembly()
assembly.activate()
let b = assembly.baseObject() as! BaseClass
print(b.doIt())
let testAssembly = BaseAssembly().activate()
let patcher = TyphoonPatcher()
patcher.patchDefinitionWithSelector("otherObject") { () -> AnyObject! in
return StubClass()
}
testAssembly.attachPostProcessor(patcher)
let c = testAssembly.baseObject() as! BaseClass
print(c.doIt())

Resources