• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Stuck in Swift
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: Stuck in Swift
      • From: "Gerriet M. Denkmann" <email@hidden>
References: 
 >Stuck in Swift (From: "Gerriet M. Denkmann" <email@hidden>)

  • Prev by Date: Re: diff tool on iOS?
  • Next by Date: Re: Translating to Swift
  • Previous by thread: Stuck in Swift
  • Next by thread: Re: Stuck in Swift
  • Index(es):
    • Date
    • Thread