• 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: Determining underlying type of array in swift?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Determining underlying type of array in swift?


  • Subject: Re: Determining underlying type of array in swift?
  • From: Rick Mann <email@hidden>
  • Date: Wed, 09 Dec 2015 15:08:00 -0800

> On Dec 9, 2015, at 14:57 , Quincey Morris <email@hidden> wrote:
>
> 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.

Actually, looking further, what I have is an UnsafePointer<Int32>. This is actually coming from Objective-C++ code (it's how I bridge my Swift code to our extensive C++ codebase). The class has a property:

@property (nonatomic, readonly, nullable)    const int*    faceIndexes;

Which shows up in swift as UnsafePointer<Int32>, and I can treat it as an array:

	myObjCObj.faceIndexes[0]

So, really, I need to get at the UnsafePointer's type. Is that:

	var up: UnsafePointer<Int32>
	sizeof(up.memory.dynamicType)

That seems to work. Wordy, but gets the job done.

>
> ** "If I Read The Generated Header File"


--
Rick Mann
email@hidden



_______________________________________________

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: 
 >Determining underlying type of array in swift? (From: Rick Mann <email@hidden>)
 >Re: Determining underlying type of array in swift? (From: Quincey Morris <email@hidden>)

  • Prev by Date: Re: NSViewController -identifier unrecognized selecto problem
  • Next by Date: NSView -identifier - is there more to it?
  • Previous by thread: Re: Determining underlying type of array in swift?
  • Next by thread: CG Error with popovers - known issue?
  • Index(es):
    • Date
    • Thread