• 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: Noob Swift frustration with dicts and optionals
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Noob Swift frustration with dicts and optionals


  • Subject: Re: Noob Swift frustration with dicts and optionals
  • From: Roland King <email@hidden>
  • Date: Thu, 18 Sep 2014 06:56:41 +0800

> On 18 Sep 2014, at 6:37 am, Marco S Hyman <email@hidden> wrote:
>
>
>>
>> The message still seems wrong, though: the operand of the "?" is the dict lookup, which _is_ optional.
>
> Many Swift error messages leave me scratching my head. My latest
> head scratcher was this:
>
> func someFunc() -> String {
>    return "foo"
> }
>
> let key = someFunc()
>
> switch key {
> case "foo", "bar", "baz":
>    println(key)
> default:
>    println("Key not found")
> }
>
> Works fine.  But when someFunc (in another file) changes to return
> a "String?" instead of a "String" the compiler tells me that "foo",
> "bar", and "baz" does not conform to protocol 'IntervalType'.
>
> Marc
> _______________

I’ve said before, I pretend lots of Swift error messages just say ‘Syntax Error’ and don’t worry about what the error message wording is. Usually the error messages are correct, however they are often unrelated to the fix you need to make. Type inference, powerful as it is, when there is an error often seems not to be entirely sure which possibility you wanted, picks the wrong one and then reports an error which represents where the compiler, heading down one possible rabbit hole, got stuck. So you have to guess where the compiler got to, then try to figure out why it might have tried going there, then go backwards from that, it’s often not worth the time.

In this case it’s not so bad, although again not the most helpful; the type of the cases is inferred directly from the type of the switch variable, so your cases are now optional Strings and indeed an optional String doesn’t conform to the IntervalType which is what switch cases need to be. Dealing with optionals in switches is a bit of a pain, you can use .Some( something ) and .None( Something) but it makes for unreadable code.

Another option for that piece of code is

switch key ?? “Key which cannot possibly exist”
{
	...
}

which makes the switch back into a String switch
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden


References: 
 >Noob Swift frustration with dicts and optionals (From: Jens Alfke <email@hidden>)
 >Re: Noob Swift frustration with dicts and optionals (From: Marco S Hyman <email@hidden>)
 >Re: Noob Swift frustration with dicts and optionals (From: Jens Alfke <email@hidden>)
 >Re: Noob Swift frustration with dicts and optionals (From: Marco S Hyman <email@hidden>)

  • Prev by Date: Re: Noob Swift frustration with dicts and optionals
  • Next by Date: Re: Docs?
  • Previous by thread: Re: Noob Swift frustration with dicts and optionals
  • Next by thread: Need help to get my Enterprise app running on iOS8?
  • Index(es):
    • Date
    • Thread