Re: [swift-users] Class vs Structures

2017-06-30 Thread Taylor Swift via swift-users
The Ray Wenderlich style guide contains *some* useful insights, but you should not take it as a “swift best practices” guide, or even a good code style guide for your own projects. At the risk of sounding blunt, the RW style guide is optimized for blog posts and cheap tutorials, not a cohesive

Re: [swift-users] Passing Data to a f(void *) function

2017-06-30 Thread Taylor Swift via swift-users
I believe UnsafeRawPointer is a subtype of any UnsafePointer type, so UnsafePointer’s are always implicitly convertible to UnsafeRawPointers. So even the following compiles: import Foundation func f(_ a:UnsafeRawPointer) { } let data = Data(bytes: [1, 2, 3, 4]) data.withUnsafeBytes{

Re: [swift-users] Swift alternative for heterogeneous collection of objects with "generic" contents.

2017-06-30 Thread Joe Groff via swift-users
> On Jun 29, 2017, at 10:59 PM, Mikhail Seriukov via swift-users > wrote: > > Hello everyone, > In objc there is quite common pattern when we use a base class with a type > property and concrete subclasses where type is uniquely identifying the > subclass. > We can

Re: [swift-users] Passing Data to a f(void *) function

2017-06-30 Thread Daniel Dunbar via swift-users
> On Jun 30, 2017, at 7:40 AM, Martin R via swift-users > wrote: > > I have a C function > >void myfunc(const void *ptr); > > which is imported to Swift as > >func myfunc(_ ptr: UnsafeRawPointer!) > > This compiles and runs without problems: > >let data

Re: [swift-users] Is '_ = x' guaranteed to hold a reference to x?

2017-06-30 Thread Mike Ferenduros via swift-users
With an empty body, you mean? > On 30 Jun 2017, at 22:00, Joe Groff wrote: > > >> On Jun 30, 2017, at 11:47 AM, Mike Ferenduros via swift-users >> wrote: >> >> I'm doing a RAII sort of thing with an object, and would like to keep it >> alive until

Re: [swift-users] Is '_ = x' guaranteed to hold a reference to x?

2017-06-30 Thread Taylor Swift via swift-users
You should probably use `withExtendedLifetime(_:_:) ` for this. On Fri, Jun 30, 2017 at 2:54 PM, Charles Srstka via swift-users < swift-users@swift.org> wrote: > > On Jun 30, 2017, at 1:47 PM, Mike Ferenduros via

[swift-users] the pain of strings

2017-06-30 Thread David Baraff via swift-users
I know, I’ve read tons about about this. I sympathize. Unicode, it’s all very complex. But. BUT. Python: shortID = longerDeviceID[-2:] # give me the last two characters Swift: let shortID = String(longerDeviceID.characters.dropFirst(longerDeviceID.characters.count - 2)) I

Re: [swift-users] Is '_ = x' guaranteed to hold a reference to x?

2017-06-30 Thread Joe Groff via swift-users
> On Jun 30, 2017, at 12:25 PM, Mike Ferenduros > wrote: > > Ah, I think I was unclear - I want to extend the lifetime into an escaping > closure, not just within a scope, and I was wondering what the minimal way is > to do that. I see. Using

Re: [swift-users] Is '_ = x' guaranteed to hold a reference to x?

2017-06-30 Thread Joe Groff via swift-users
> On Jun 30, 2017, at 12:13 PM, Mike Ferenduros > wrote: > > With an empty body, you mean? I'd say it's probably more readable to nest the code that's dependent on the lifetime of the object in the block body, though you can just put `withExtendedLifetime(x) { }`

Re: [swift-users] Is '_ = x' guaranteed to hold a reference to x?

2017-06-30 Thread Mike Ferenduros via swift-users
Ah, I think I was unclear - I want to extend the lifetime into an escaping closure, not just within a scope, and I was wondering what the minimal way is to do that. > On 30 Jun 2017, at 22:15, Joe Groff wrote: > > >> On Jun 30, 2017, at 12:13 PM, Mike Ferenduros

[swift-users] Is '_ = x' guaranteed to hold a reference to x?

2017-06-30 Thread Mike Ferenduros via swift-users
I'm doing a RAII sort of thing with an object, and would like to keep it alive until an completion-block is called (asynchronously). Is it sufficient to say '_ = x' in the completion-block to keep a live reference to the object? I was told that the optimiser is free to discard this line, and

Re: [swift-users] Is '_ = x' guaranteed to hold a reference to x?

2017-06-30 Thread Charles Srstka via swift-users
> On Jun 30, 2017, at 1:47 PM, Mike Ferenduros via swift-users > wrote: > > I'm doing a RAII sort of thing with an object, and would like to keep it > alive until an completion-block is called (asynchronously). > > Is it sufficient to say '_ = x' in the completion-block

Re: [swift-users] the pain of strings

2017-06-30 Thread Adrian Zubarev via swift-users
let longString = "1234567890" print(longString.suffix(2)) // prints "90" --  Adrian Zubarev Sent with Airmail Am 30. Juni 2017 um 23:45:01, David Baraff via swift-users (swift-users@swift.org) schrieb: I know, I’ve read tons about about this. I sympathize. Unicode, it’s all very complex.

Re: [swift-users] the pain of strings

2017-06-30 Thread Adrian Zubarev via swift-users
Well you’ve mentioned Swift 4 in your original post, therefore I provided a solution using Swift 4. It’s returning a view called `Substring`. --  Adrian Zubarev Sent with Airmail Am 1. Juli 2017 um 00:38:42, David Baraff (davidbar...@gmail.com) schrieb: I’m sorry, but I don’t see suffix() as a

Re: [swift-users] Class vs Structures

2017-06-30 Thread Brent Royal-Gordon via swift-users
> On Jun 29, 2017, at 10:40 AM, Vitor Navarro via swift-users > wrote: > > Do you guys have any guideline regarding usage here? Here's my suggestion: Use a class when the object represents a specific *thing* that can't be duplicated without consequence; use a struct

Re: [swift-users] the pain of strings

2017-06-30 Thread Brent Royal-Gordon via swift-users
> On Jun 30, 2017, at 2:44 PM, David Baraff via swift-users > wrote: > > Python: > shortID = longerDeviceID[-2:]# give me the last two > characters > > Swift: > let shortID = > String(longerDeviceID.characters.dropFirst(longerDeviceID.characters.count

Re: [swift-users] the pain of strings

2017-06-30 Thread David Baraff via swift-users
I’m sorry, but I don’t see suffix() as a member function in any documentation, nor does it complete in Xcode. Is this perhaps only in Swift 4? If so, that’s a definite improvement! Begin forwarded message: > From: Adrian Zubarev > Subject: Re: [swift-users]

Re: [swift-users] the pain of strings

2017-06-30 Thread Adrian Zubarev via swift-users
The best docs you can get without Xcode I know about is this one here:  https://developer.apple.com/documentation/swift/string In Swift 4 a String will yet again become a collection type. --  Adrian Zubarev Sent with Airmail Am 1. Juli 2017 um 02:57:05, David Baraff (davidbar...@gmail.com)

Re: [swift-users] the pain of strings

2017-06-30 Thread David Baraff via swift-users
I only know a little bit of what I’ve read online in blog posts and such. I don’t have access to the Swift 4 API documentation, since i’m not running the xcode beta yet. Is there someplace I can see the actual new API’s for String, in swift 4? i googled but haven’t found it yet. > On Jun

Re: [swift-users] the pain of strings

2017-06-30 Thread Adrian Zubarev via swift-users
Plus if you want to play with Swift 4 without running a toolchain or the new Xcode you can do it in your browser: https://swift.sandbox.bluemix.net/#/repl Just change that repl to Swift 4. --  Adrian Zubarev Sent with Airmail Am 1. Juli 2017 um 03:02:38, Adrian Zubarev

Re: [swift-users] the pain of strings

2017-06-30 Thread Kyle Murray via swift-users
Hi David, You can see the new APIs for Swift 4's String here: https://developer.apple.com/documentation/swift/string?changes=latest_minor The page indicates changes and additions since Xcode 8.3's Swift 3.1,

Re: [swift-users] the pain of strings

2017-06-30 Thread David Baraff via swift-users
> On Jun 30, 2017, at 6:06 PM, Kyle Murray wrote: > > Hi David, > > You can see the new APIs for Swift 4's String here: > https://developer.apple.com/documentation/swift/string?changes=latest_minor >

Re: [swift-users] Class vs Structures

2017-06-30 Thread Taylor Swift via swift-users
An addendum: if something represents a specific “thing” that can’t be duplicated without consequence, often that means it should be stored in one specific place in your code, not made into a class. On Fri, Jun 30, 2017 at 10:10 PM, Brent Royal-Gordon via swift-users < swift-users@swift.org>

Re: [swift-users] the pain of strings

2017-06-30 Thread Charles Srstka via swift-users
> On Jun 30, 2017, at 4:44 PM, David Baraff via swift-users > wrote: > > I know, I’ve read tons about about this. I sympathize. Unicode, it’s all > very complex. > > But. > > BUT. > > Python: > shortID = longerDeviceID[-2:]# give me the last two >

Re: [swift-users] the pain of strings

2017-06-30 Thread Taylor Swift via swift-users
> Swift's strings were very deliberately designed this way. It's tougher to > get off the ground, sure, but it's better in the long run. > > It probably is, but the correct idiom is not very well known, and sadly most tutorials and unofficial guides are still teaching dumb ways of subscripting

[swift-users] Passing Data to a f(void *) function

2017-06-30 Thread Martin R via swift-users
I have a C function void myfunc(const void *ptr); which is imported to Swift as func myfunc(_ ptr: UnsafeRawPointer!) This compiles and runs without problems: let data = Data(bytes: [1, 2, 3, 4]) data.withUnsafeBytes { (ptr) in myfunc(ptr) } // (A) and the type of