Re: Determining underlying type of array in swift?
Re: Determining underlying type of array in swift?
- Subject: Re: Determining underlying type of array in swift?
- From: Quincey Morris <email@hidden>
- Date: Wed, 09 Dec 2015 14:57:17 -0800
- Feedback-id: 167118m:167118agrif8a:167118sg5PfbLlfZ:SMTPCORP
On Dec 9, 2015, at 14:35 , Rick Mann <email@hidden> wrote:
>
> If I have var foo : [Int], how can I get that type, Int? I need to get the size of each element.
I imagine there’s a direct way, but I can’t find it in a program. You can do something like this:
strideofValue (foo [0]) // but this requires foo.count != 0, or:
strideof (foo [0].dynamicType) // which seems to work in a playground even for empty arrays
Note that I’m not sure there’s an API guarantee that the Array<> elements are stored contiguously in memory. If you want that, you should probably use ContiguousArray<> instead. (That also gives you API to directly/unsafely access the memory too, IIRTGHF** correctly.)
Do be careful, though. If you’re accessing Ints raw-ly, and (say) saving them or transmitting them to another application or platform, you might also have to worry about element size and endianness differences, theoretically.
** "If I Read The Generated Header File"
_______________________________________________
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