6

I have the following protocol in Swift:

protocol FooConvertible{
    typealias FooType

    init(foo: FooType)
}

I can make Swift classes conform to it in the class definition:

class Bar: FooConvertible {
    var baz: String = ""
    required init(foo: String){
        baz = foo
    }
}

So far so good. However, the problem arises when I try to make a class conform to it in an extension (With Cocoa classes, it's my only option, as I don't have the source):

class Baz {
    var baz = ""
}

extension Baz: FooConvertible{

    required convenience init(foo: String) { // Insists that this should be in the class definition
        baz = foo
    }
}

extension NSURL: FooConvertible{

    required convenience init(foo: String) { // this also fails for the same reason

    }
}

This used to be possible, in previous versions of the language

What's the reason it was removed?

That would mean that all the XXXLiteralConvertible Protocols are banned from Cocoa classes!

cfischer
  • 24,452
  • 37
  • 131
  • 214
  • That fact is spelled out quite clearly by [the documentation](https://developer.apple.com/library/mac/documentation/Swift/Conceptual/Swift_Programming_Language/Extensions.html#//apple_ref/doc/uid/TP40014097-CH24-XID_235). – CodaFi Nov 12 '14 at 00:23
  • Where does it say that you may add convenience inits to Swift classes but not to Cocoa classes? – cfischer Nov 12 '14 at 00:39
  • You are not allowed to do either. Your example does not compile. – CodaFi Nov 12 '14 at 00:42
  • Crap, I wasn't getting any compiler errors in the playground until now. you're right and I'll modify the question. – cfischer Nov 12 '14 at 00:49
  • FYI, there's a discussion on the devforums about the `XXXLiteralConvertible` aspect of this: [StringLiteralConvertible in Swift 1.1 for class](https://devforums.apple.com/message/1057171) – Mike S Nov 12 '14 at 02:04

1 Answers1

1

Any chance you are trying to create something like this:

protocol FooConvertible : class {
    typealias FooType

    var baz : String { get set } // protocol extensions inits may need to know this

    init(foo: FooType) // this is your designated initializer
}

extension FooConvertible {

    // init(foo: String) {
    //     self.init(foo: foo)
    //     baz = foo
    // }
    // you can't do this because it could call it self recursively 

    init(num: Int) { // this init will call your designated init and can instantiate correctly 
        self.init(foo: "\(num)")
    }
}

class Baz {
    var baz = ""
}

class Bar: FooConvertible {
    var baz: String = ""

    required init(foo: String) { // designated initializer
        baz = foo
    }
}

Baz will now know about all inits of FooConvertible. If so, I'm glad I could help. :)

DevAndArtist
  • 4,971
  • 1
  • 23
  • 48