Re: [swift-users] Restricting associated values

2017-06-18 Thread Howard Lovatt via swift-users
To me Angle is a unit with two common representations: radians and degrees. It's not an enum because it doesn't have two values, it has one value that you can view in two ways. Therefore I would make an Angle struct, something like: //: Angle struct instead of angle enum import Foundation str

[swift-users] TWISt-shout Newsletter 2017-06-19

2017-06-18 Thread Kenny Leung via swift-users
Hi All. Here is your TWISt-shout Newsletter for the week of 2017-06-12 to 2017-06-18 https://github.com/pepperdog/TWISt-shout/blob/master/2017/TWISt-shout-2017-06-19.md Enjoy! -Kenny ___

Re: [swift-users] Restricting associated values

2017-06-18 Thread David Sweeris via swift-users
> On Jun 18, 2017, at 19:30, Nevin Brackett-Rozinsky via swift-users > wrote: > > Is there a way to restrict the associated values of an enum? For example, > suppose I have this type: > > enum Angle { > case radians(Double) > case degrees(Double) > } > > I want to ensure that the rad

Re: [swift-users] Intended behavior or bug in Dictionary's new subscript(_:default:)?

2017-06-18 Thread David Sweeris via swift-users
> On Jun 17, 2017, at 21:12, Ben Cohen via swift-users > wrote: > > In order for this defaulting subscript technique to work as intended, the > subscript { get } needs to be called, then the mutation happens, then the > subscript { set } writes the mutated value back, including adding it for

[swift-users] Restricting associated values

2017-06-18 Thread Nevin Brackett-Rozinsky via swift-users
Is there a way to restrict the associated values of an enum? For example, suppose I have this type: enum Angle { case radians(Double) case degrees(Double) } I want to ensure that the radians values is always in [0, 2π) and the degrees values is always in [0, 360). Ideally I would like to

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-06-18 Thread Jon Shier via swift-users
Given that, per his description, “metadata” can be anything, creating a struct doesn’t really help. Jon Shier > On Jun 18, 2017, at 9:00 PM, somu subscribe wrote: > > Create a struct for Metadata and conform to Coding > > Code: > > let string = """ > { > "id": "4yq6txdpfadhbaqnwp3", >

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-06-18 Thread somu subscribe via swift-users
Create a struct for Metadata and conform to Coding Code: let string = """ { "id": "4yq6txdpfadhbaqnwp3", "email": "john@example.com", "name":"John Doe", "metadata": { "link_id": "linked-id", "buy_count": 4 } } """ let data = string.data(using: .utf8)! //Force unwrapping jus

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-06-18 Thread Jon Shier via swift-users
The more I use Codable, the less suited to networking it becomes. In reading a variety of blog posts about implementing custom Decodable support from JSON, I kept running across the same pattern. Basically, users had started implementing their own decoding protocols which wrap Decodable

Re: [swift-users] Can't use unsafe pointer from second thread?

2017-06-18 Thread Michel Fortin via swift-users
Oh, right, my mistake. I think the problem is that UnsafeMutableRawPointer(&mSelf) creates a pointer to the local variable mSelf on the stack. When mSelf goes out of scope, your raw pointer to an object reference pointer on the stack becomes invalid. (And it could become invalid even earlier wi

Re: [swift-users] Intended behavior or bug in Dictionary's new subscript(_:default:)?

2017-06-18 Thread Jens Persson via swift-users
https://bugs.swift.org/browse/SR-5250 On Sun, Jun 18, 2017 at 6:12 AM, Ben Cohen wrote: > In order for this defaulting subscript technique to work as intended, the > subscript { get } needs to be called, then the mutation happens, then the > subscript { set } writes the mutated value back, inclu

Re: [swift-users] Can't use unsafe pointer from second thread?

2017-06-18 Thread Robert Nikander via swift-users
The main thread isn’t deallocating the PointerTest object, because it’s saved in the AppDelegate. Rob > On Jun 18, 2017, at 10:45 AM, Michel Fortin wrote: > > The problem is that the main thread is deallocating the object before the > other thread has a chance to see it. Ideally you'd pass

Re: [swift-users] Can't use unsafe pointer from second thread?

2017-06-18 Thread Michel Fortin via swift-users
The problem is that the main thread is deallocating the object before the other thread has a chance to see it. Ideally you'd pass a managed object reference to the block that runs in the other thread so the reference count does not fall to zero. If you absolutely need to pass it as an unsafe poi

[swift-users] Can't use unsafe pointer from second thread?

2017-06-18 Thread Robert Nikander via swift-users
Hi, I’m porting some C to Swift and I need to pass a Swift instance through a `void *` (ie, UnsafeMutableRawPointer). It works, in one thread, but when a second thread accesses the exact same pointer, it fails with memory errors like EXC_BAD_ACCESS The code below could be pasted into an AppDel

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-06-18 Thread Jon Shier via swift-users
Your issue here is Any, which will likely never be Decodable. You’ll need an actual type to contain the raw JSON. Hilariously, I have to wonder if Argo’s JSON enum could be made Decodable, as it can represent every valid JSON type typically contained in the Any returned by JSONSerializat

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-06-18 Thread Rien via swift-users
Dang, hit send too soon. Sorry. This does not address your question, so please ignore… (foot in mouth)! Regards, Rien Site: http://balancingrock.nl Blog: http://swiftrien.blogspot.com Github: http://github.com/Balancingrock Project: http://swiftfire.nl - An HTTP(S) web server framework in Swift

Re: [swift-users] Decode a JSON object of unknown format into a Dictionary with Decodable in Swift 4

2017-06-18 Thread Rien via swift-users
Are you looking for a general purpose JSON interpreter / API ? There are many of them around, and in fact I do have my own: https://github.com/Balancingrock/VJson With VJson I would write: let json = VJson.parse(… your json object…) and then access the metadata as: let buyCount = (json | ”met