Re: Value of type '[AnyObject]!' has no member 'Generator'
Re: Value of type '[AnyObject]!' has no member 'Generator'
- Subject: Re: Value of type '[AnyObject]!' has no member 'Generator'
- From: Roland King <email@hidden>
- Date: Sun, 04 Oct 2015 20:10:08 +0800
> On 4 Oct 2015, at 18:56, Rick Mann <email@hidden> wrote:
>
>
>> On Oct 3, 2015, at 23:03 , Roland King <email@hidden> wrote:
>>
>> Apply the usual swift technique and break it down into more than one line then go look at the types of each variable.
>>
>> let ff = device.formats
>> let pp = ff!
>>
>> option-click on each of those variables and you’ll quickly see the ‘!!’ on the first one and hence why you have to explicitly unwrap it to get it into a [AnyObject] which has a generator.
>
> A) Ah, so it's Swift continuing to suck.
No, not at all, Swift is being consistently consistent. The variable is a ‘!!’ which means it’s implicitly unwrapped optional of an implicitly unwrapped optional of an array, so when you use it it gets unwrapped for you, once, thus leaving an implicitly unwrapped optional and optionals don’t have generators, you have to re-unwrap it again.
> B) Why is device.formats double-bang?
I believe (every time I say that Quincey sharpens his keyboard) it’s because you’re calling formats() on an AnyObject. Swift knows formats() returns an Array of AnyObject, well an optional one, but it doesn’t know if the AnyObject you’re calling it on actually supports the method/property ‘formats’. So the first optional is ‘was I able to even call formats on this AnyObject’ and the second optional is the result. So you can disambiguate between the object not having a formats property and it having a formats property which doesn’t have any formats.
If you explicitly format devices as [ AVCaptureDevice ], either when you declare it or when you use it in the for loop
for device in devices as [ AVCaptureDevice ]
then device becomes an AVCaptureDevice and the second optional goes away.
> C) Why can't I option-click on formats in device.formats to get an answer (because A)?
More likely because of B. Again if you cast the devices so each device is an AVCaptureDevice then option-click works.
> D) Why does command-clicking on formats take me to an Obj-C file (because A)?
No clue - I did it in a playground for speed and Command-clicking doesn’t do much in playgrounds at the best of times. Does it do better if you’ve strongly typed device?
>
> Thanks for pointing out you can get the type of a variable by option-clicking it like that. Not sure why it doesn't consistently work (e.g. why can't I get the type of formats), but that'll be very helpful.
If it wasn’t for option-click I think I would have thrown Swift down a deep hole by now. My two techniques when faced with an error such as the above are either split into multiple lines and use Option-Click or, knowing it’s usually a mismatch between what I think a variable is and what Swift thinks a variable is, explicitly type things until either the error goes away, or Swift tells me why what I think the variable is, is wrong.
Generally if you find yourself doing something with [AnyObject] (and there’s less of that with the new typed collections in ObjC) you should smell the whiff wafting up through the code and do a cast to something stronger-typed, then Swift can do a better job of helping you help yourself, and assert brutally if the type isn’t what you thought it was going to be.
_______________________________________________
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