>>
>> ok this simple test works for me .. I also don’t understand the extra parens
>> etc in your example. So where does your code differ from the below? Note I
>> set it both with a public function and a closure, just to see if it works.
>
> A detail I should have included. The @property is declared in the optional
> section of a protocol. I know the object in question has implemented the
> property. But that explains the extra requirements for unwrapping.
>
> I've put together a gist where I've attempted to play with this.
>
> https://gist.github.com/SheffieldKevin/a06907e163885f249548
> <https://gist.github.com/SheffieldKevin/a06907e163885f249548>
>
> I got the assigning to a property working when that property was declared as
> part of the class, but not when it has been declared in the optional section
> of a protocol. So I was able to duplicate what you did Roland. But no matter
> what I try, documentation I read I can't make it work when the property is
> declared in the protocol.
>
> I got myself distracted because the project I setup to try out stuff was an
> objective-c command line tool and I also had trouble calling swift code from
> Objective-c. I could not get a swift function that wasn't a class or instance
> method to be callable from Objective-c.
>
> Kevin
>
Right the optionality in the protocol is somewhat important and yes that does
explain why there’s the extra parens and ? around it. You’re certainly out in
some deeper water here with optional properties in a protocol which return
blocks in objective C being bridged over to Swift.
I can’t get your error message however. The stuff you posted on github might be
helpful if it were a full project, with xcode project file, which exhibits that
error message when built, ie it doesn’t actually build. I just can’t make it
fail. What version of Xcode are you using by the way, Swift is so flux-y at the
moment it probably makes a difference.
I made my own protocol with an optional (and a non-optional) property and used
it in Swift and I can assign to either of them with the method posted earlier
in the thread. I can actually defeat the compiler in a few ways, most notably
by casting to the protocol itself in which case it won’t let me assign the
property at all.
I thought of this …
x.createImage? = function
but that doesn’t work either. If x does implement createImage() but returns nil
because it’s not set yet then it doesn’t set it, if x doesn’t implement the
getter then it just crashes.
The one version I do have working is this piece of ugliness. Fruit is the
protocol with the optional property, Banana is a fruit which may or may not
implement it, I personally don’t like bananas at all.
var x = Banana()
var y = x as Fruit
if y.createImage != nil
{
x.createImage = theImplementationOfCreateImage
let r = x.createImageOptional( nil )
}
The test on y appears to tell you whether or not the protocol is implemented on
x. Then you can set the property on x inside the block, except you probably
can’t because this is the place you’ve been having issues setting the property
with an error message I can’t make happen. That code works fine for me, and
calls the method (which then crashes because I sent it nil but that’s fine)
There’s probably a deeper issue here. createImage is a property name which
itself implies two methods, one with the same name as the property, the other
with ‘set’ stuffed on the front. Then the implementation of that property is
itself optional. Then you pull all that over to Swift and have to disambiguate
the case where you’re checking for implementation of the property and the case
you’re getting it and checking if it’s set to nil or not and the case you want
to set it to something, possibly back to nil. This may be one of those things
which just doesn’t translate very well.
_______________________________________________
Cocoa-dev mailing list ([email protected])
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]