[swift-users] Sampling collections

Jens Alfke jens at mooseyard.com
Sun Apr 10 17:12:02 CDT 2016

> On Apr 10, 2016, at 1:00 PM, Milos Rankovic <milos at milos-and-slavica.net> wrote:
> The ObjC–Swift bridge is relevant here only in the sense that I would also like sampling methods added to `NSArray`, `NSSet` or `NSDictionary`…

Any library or program can add methods to any class using extensions. You can easily implement your own `sample` property. There may be a Swift library somewhere that provides one; all you’d have to do is import it.

If you want to implement it yourself, you can call the C functions `random` or `arc4random` directly from Swift. (You may need to add an #include to your bridging header in Xcode.)

> What I would personally like to see, however (and what I was wondering the community feels about), is that one of these frameworks extends Standard Library data types and protocols with this functionality

So far Apple hasn’t added Swift-specific functionality to system frameworks; the frameworks are in Objective-C or C, and the Swift compiler and runtime bridge to that. What you’re suggesting would go the other direction, with a framework offering custom API wrappers. Maybe that will happen in the near future.


More information about the swift-users mailing list