Re: Stuck in Swift
Re: Stuck in Swift
- Subject: Re: Stuck in Swift
- From: Roland King <email@hidden>
- Date: Thu, 14 Aug 2014 20:24:29 +0800
>
> class AppDelegate: NSObject
> {
> dynamic var statusString : String? // bound to some TextField
>
> let someThing = SomeClass( myStatusHandler ) <---- this creates strange error messages
>
> func myStatusHandler( s: String )
> {
> statusString = s
> }
> }
>
> class SomeClass
> {
> private let statusHandler: (String) -> Void
>
> init( statusHandler: (String) -> Void )
> {
> self.statusHandler = statusHandler
> }
> }
>
> Gerriet.
>
What you’re doing there doesn’t make much sense to me. myStatusHandler is an instance method (or whatever it’s called in Swift). You’re trying to assign it outside the context of an actual instance, there’s no ‘self’ at that point. I don’t really know what the type of myStatusHander is at the point you’re trying to assign it, probably something which takes an AppDelegate and returns a (String)->Void function, or something like that, ie (AppDelegate)->(String)->Void. Whatever it is it’s not a (String)->Void function.
if you write an init() method and put it in there there’s context for it, self exists
class AppDelegate: NSObject
{
dynamic var statusString : String? // bound to some TextField
let someThing : SomeClass?
func myStatusHandler( s: String )
{
statusString = s
}
override init()
{
super.init()
someThing = SomeClass( self.myStatusHandler )
}
}
class SomeClass
{
private let statusHandler: (String) -> Void
init( statusHandler: (String) -> Void )
{
self.statusHandler = statusHandler
}
}
let a = AppDelegate();
a.someThing
and yes you have to do the little let someThing: SomeClass? dance so you can use super.init() before using myStatusHandler.
I’m almost entirely sure there’s a far better way to do what you’re trying to do which doesn’t involve quite this much ugly. Not loving this syntax yet, really, just not getting into it and I’m not even trying anything hard.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden
References: | |
| >Stuck in Swift (From: "Gerriet M. Denkmann" <email@hidden>) |