Re: [swift-users] FileManager.copyItem not throwing?

2017-11-13 Thread Marco S Hyman via swift-users
On 11 Nov 2017, at 01:51, Marco S Hyman via swift-users <swift-users@swift.org> 
wrote:
> 
>> My report was closed as a duplicate of 30350792.
> 
> Are you still seeing this on 10.13?

I don’t know, let me test...

Very good... the copy fails into the catch block as it should using Xcode 9.1 
on macOS 10.13.  I’m going to keep my workaround in my code for a while, anyway 
:)

Marc
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] FileManager.copyItem not throwing?

2017-11-11 Thread Marco S Hyman via swift-users


> On Nov 11, 2017, at 2:07 PM, John Brownie  wrote:
> 
> And that's the point. The throw does not happen. An error is logged, but 
> execution continues as though there was no error.

Also...

My workaround for this bug assumes you tested for the destination file presence 
BEFORE doing the copy and removing/moving one away if found.  Otherwise you 
won’t know if you are looking at an older version of the file when testing 
after the copy.

In my code I’m making a versioned backup and loop over file names (file-1, 
file-2, ... file-n) until I find a name that is not in use and then attempt the 
copy.

Marc
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] FileManager.copyItem not throwing?

2017-11-10 Thread Marco S Hyman via swift-users

> Running on macOS 10.12.6, Xcode 8.3.2, I get to the copyItem call, which 
> shows an error on the console:
> 
> 2017-11-11 11:18:25.931446+1000 MyApp[32662:2408351] open on /path/to/file: 
> Permission denied
> 
> But it doesn't go to either of my catch blocks, but goes to the following 
> line, where the completion handler is to be run. What is going on?

Known (by Apple) bug.  My report was closed as a duplicate of 30350792.

My workaround is to do this in my do block after attempting to copy a file to 
saveFileUrl:

try fileManager.copyItem(at: url, to: saveFileUrl)
/// DANGER WILL ROBINSON -- the above call can fail to return an
/// error when the file is not copied.  radar filed and
/// closed as a DUPLICATE OF 30350792 which is still open.
/// As a result I must verify that the copied file exists
if !fileManager.fileExists(atPath: (saveFileUrl.path)) {
unexpected(error: nil,
   "Cannot copy \(url.path) to \(saveFileUrl.path)")
return false
}

which duplicates what the catch block would do.

Marc
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Should Array's `append(_)` function cause `didSet`?

2017-07-07 Thread Marco S Hyman via swift-users

> On Jul 7, 2017, at 9:48 PM, Zhao Xin  wrote:
> 
> Thank you very much Marco. But What is  “outside of an initializer” really 
> bothers me. **Both** `func bar(keysAndValues:Dictionary)` 
> works now. **Are they really outside ?**

Uhhh, that is certainly not the results I’d have expected.  Perhaps one of the  
swift language lawyers can explain.


___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Should Array's `append(_)` function cause `didSet`?

2017-07-07 Thread Marco S Hyman via swift-users

> init(keysAndValues:Dictionary) {
> self.keys.append(contentsOf: keysAndValues.keys)
> }

> 
> Above code doesn't call `didSet` in playground. My .swift file is similar and 
> didn't call `didSet` either. However, if without a struct, `didSet` is called.

“If you don’t need to compute the property but still need to provide code that 
is run before and after setting a new value, use willSet and didSet. The code 
you provide is run any time the value changes outside of an initializer.”

Excerpt From: Apple Inc. “The Swift Programming Language (Swift 3.1).” iBooks. 
https://itun.es/us/jEUH0.l

Note “outside of an initializer”.  didSet and willSet are not called in 
initializers.



___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Swift Documentation question

2017-07-01 Thread Marco S Hyman via swift-users
>  but what I am looking for is something that lists the various protocols and 
> types that make up the standard library with links to the reference pages for 
> the types and protocols. Does documentation like this exist? I thought I have 
> seen this documentation before but for the life of me I have not been able to 
> find it.

http://swiftdoc.org


___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] cascaded do/catch bug?

2017-06-29 Thread Marco S Hyman via swift-users

> On Jun 29, 2017, at 4:20 PM, Jordan Rose  wrote:
> 
> That does sound like a bug to me. Can you make a self-contained test case and 
> file at https://bugs.swift.org/ ?

Done. SR-5339

Marc
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[swift-users] cascaded do/catch bug?

2017-06-29 Thread Marco S Hyman via swift-users
This macOS code in Xcode 8.3.3:

if !fileManager.fileExists(atPath: (saveFileURL.path)) {
do {
1 ==>   try fileManager.linkItem(atPath: url.path, toPath: saveFileURL.path)
2 ==>   } catch {
// couldn't create hard link, copy file instead
do {
3 ==>   try fileManager.copyItem(at: url, to: saveFileURL)
} catch let error as NSError {
unexpected(error: error,
   "Cannot copy \(url.path) to 
\(saveFileURL.path)\n\nReason: ")
return false
}
}
}
return true


lldb tells me the code at 1 fails.  That was expected.  The app is sandboxed 
and the security bookmark code that gives access to the destination is in flux. 
 lldb says control transfers to 2 then falls into the do block containing 3.

The code at 3 fails for the same reason.  This is when the unexpected occurred. 
 lldb says that control transferred to 2 then to the end of the if statement 
where the function returns true.  The catch following the do containing the 
function “unexpected” is not entered.

Is that a bug?

As for why 1 uses path instead of URL an older version of the code had this 
note.

// linkItemAtURL fails trying to link foo.jpg_original to somedir/foo.jpg.


___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] unowned self in closure in a playground

2017-02-21 Thread Marco S Hyman via swift-users
>> 
> The problem with this solution is that this changes the semantics.  I want a 
> value for every Demo instance.

My error... I read too fast and missed the () instantiation of Demo in the 
failing call.  I was reading it as Demo.increment().

Never mind.
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] unowned self in closure in a playground

2017-02-21 Thread Marco S Hyman via swift-users
> The following code crashes:
> 
> class Demo {
>   var value = 0
>   lazy var increment: (Int) -> Void = { [unowned self] by in
> self.value += by
> print(self.value)
>   }
>  }
> 
> Demo().increment(3)
> error: Playground execution aborted: error: Execution was interrupted, 
> reason: EXC_BREAKPOINT (code=EXC_I386_BPT, subcode=0x0).

value is not a static/class variable.   Without an instance of Demo it does not 
exist.  This seems to work.

class Demo {
  static var value = 0
  lazy var increment: (Int) -> Void = { [unowned self] by in
value += by
print(value)
  }
}

___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] for with optional collection?

2017-02-09 Thread Marco S Hyman via swift-users

> On Feb 9, 2017, at 1:26 PM, Rick Mann via swift-users  
> wrote:
> 
> Is there any concise way to write the following?
> 
> if let collection = someOptionalCollection
> {
>for item in collection
>{
>}
> }

someOptionalCollection?.map {
/* do something with $0 /*
}


___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Bool to Int

2016-11-21 Thread Marco S Hyman via swift-users
> Except it does, because if I write
> 
>   let a = 2

> a is of type Int (at least, according to Xcode's code completion).

and if you write

let b = 2 + 0.5

2 is treated as a double. The type of the literal “2” varies with context. Do 
you also find that inconsistent and confusing? 

> But this gives inconsistent results:
> 
>   let t = true
> 
>   let a = Int(true)
>   let b = Int(t)  //  Error
> 
> I find this to be very inconsistent and confusing.

t is a Bool and there is no automatic conversion from Bool to Int.

true is not a Bool.  It may be treated as a Bool depending upon context.  In 
the line `let t = true` it is treated as a Bool. In `let a = Int(true)` it is 
treated as an NSNumber (assuming you import foundation).

Marc
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Bool to Int

2016-11-20 Thread Marco S Hyman via swift-users
> Is there a way that avoids branching?

Don’t know.  Have you profiled

let r = a > b ? 1 : 0

to know it is an issue?
> 
> So, according to Xcode, "true" and "a > b" both have type "Bool". I don't 
> know why the compiler allows one and not the other, except that it's literal, 
> and I guess there's a "BoolLiteralConvertible" (or equivalent) for the types.

You are including foundation which gets you the bridging code.

let r = Int(true)

is an error without foundation.  With foundation you get this:

extension NSNumber : ExpressibleByFloatLiteral, ExpressibleByIntegerLiteral, 
ExpressibleByBooleanLiteral {

// [snip]

/// Create an instance initialized to `value`.
required public convenience init(booleanLiteral value: Bool)
}

and this

extension Int {

public init(_ number: NSNumber)
}


which when combined make `let r = Int(true)` work.  I haven’t profiled the code 
but would suspect that `let r = a > b ? 1 : 0` might be more efficient.


___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Performance critical code in Swift

2016-10-02 Thread Marco S Hyman via swift-users
> 
> On a broader note, I have yet to see a true modern replacement for SQLite on 
> the embedded side. There are any number of lightweight document stores, but 
> they either have performance worse than SQLite, or are not really suitable 
> for embedded use.

Realm ???

It’s faster than sqlite and easy to use.  At least I found it easy in a simple 
swift test app I wrote a while back.   Haven’t played with it since the switch 
to swift 3.0.
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] private vs. fileprivate on global declaration in Swift3?

2016-09-28 Thread Marco S Hyman via swift-users

> On Sep 28, 2016, at 8:04 AM, Jens Alfke via swift-users 
>  wrote:
> 
> 
>> On Sep 28, 2016, at 12:16 AM, Cao Jiannan via swift-users 
>>  wrote:
>> 
>> I think the Swift team should tell us which is better.
> 
> If one were better than the other, they wouldn’t both exist...
> 
> It depends on whether you need to use those entities in other source files in 
> the same module.
> If you don’t, use fileprivate. If you do, use private.

private is *more* restrictive than fileprivate.  If you need to use the 
entities in other source files in the same module you want internal, not 
private.
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Can anyone please explain this behavior?

2016-09-21 Thread Marco S Hyman via swift-users

> On Sep 21, 2016, at 2:22 PM, Jens Persson via swift-users 
>  wrote:
> 
> // This little Swift program compiles (and runs) fine:
> 
> func foo() -> Int { return a }
> let a = 1
> let b = 2
> print(foo())
> 
> But if `foo()` returns `b` instead of `a`, I get this compile time error:
> "Use of unresolved identifier `b`”

Interesting.  Seems to have something to do with ordering as if you code it 
this way:

let a = 1
let b = 2
func foo() -> Int { return b }
print(foo())

it works fine (Xcode 8 playground).
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Migrating to Swift 3 using Xcode fails

2016-09-08 Thread Marco S Hyman via swift-users

> 6. It’s still using the old compiler and my code fails to build.

Select your target.  Click on the Build Settings Tab.  Scroll down to the Swift 
Compiler - Version group and change Use Legacy Swift Language Version from Yes 
to No.

I haven’t a clue if that will actually work.   It’s worth a try, though.

Marc
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] See documentation comment for discussion

2016-06-19 Thread Marco S Hyman via swift-users

> On Jun 19, 2016, at 6:35 PM, Maury Markowitz via swift-users 
>  wrote:
> 
> All true, except, of course, when one is working with strings that really do 
> work perfectly weel qwith 1...2, like the strings I'm processing, which came 
> from 80-column punch cards originally used with LLNL's CDC 6600 (serial #2 
> IIRC). I had totally forgotten I had written this:

I’d claim those are not strings, they are arrays of Uint8.

As for your subscript... in the code "mystring[0...1]” [0...1] is not a 
Range, it is a CountableClosedRange.  The error message told you 
that.  If you change your subscript to use CountableClosedRange instead of 
Range it works -- but it’s not something I think I’d do.

Marc
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] #selector with Swift 3?

2016-06-17 Thread Marco S Hyman via swift-users
> On Fri, Jun 17, 2016 at 9:33 PM, Maury Markowitz via swift-users 
>  wrote:
> I was asked to try out the latest betas of Cocoa to check if a SceneKit bug I 
> reported has been fixed. As part of this I decided to try an update to Swift 
> 3. I've run into a number of minor issues, but one has me frustrated. In my 
> menu validate method, I have:
> 
> switch menuItem.action {
> case #selector!(showRescale) :
> 
> This worked fine in 2.2, but now it complains that it expects a ( after 
> #selector. Removing the ! fixes that problem, but now returns an error that 
> there's a missing !, which it inserts to cause the original error again. I 
> also tried placing it after the ()'s, and now it complains that it's 
> expecting the :
> 
> Does anyone know the proper Swift 3 syntax for this?

This code snippet works for me in a menu validation function...

guard let action = anItem?.action else { return false }
switch action {
case #selector(showOpenPanel(_:)):
return true
case #selector(save(_:)):
return modified
case #selector(openPreferencesWindow(_:)):
return true
default:
print("default for item \(anItem)")
}
return false

I used the guard let... to get around the fact that action selectors are now 
optionals.
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Swift Binary Size vs. Obj-C

2016-06-15 Thread Marco S Hyman via swift-users
On Jun 15, 2016, at 5:37 PM, Seth Friedman via swift-users 
 wrote:
> 
> Does anyone know if the runtime libs definitively increased in size in Swift 
> 3? I could have sworn I heard 4.5 MB for Swift 2.

The total of the Frameworks dir in a Swift 2.2 app I have is 5.8M.  The 
breakdown for this app is:

 64KlibswiftAppKit.dylib
4.8MlibswiftCore.dylib
 44KlibswiftCoreData.dylib
112KlibswiftCoreGraphics.dylib
 32KlibswiftCoreImage.dylib
 84KlibswiftDarwin.dylib
 40KlibswiftDispatch.dylib
516KlibswiftFoundation.dylib
 64KlibswiftObjectiveC.dylib
 48KlibswiftWebKit.dylib

So how big might partially depend upon what frameworks the app uses.  The same 
app in Swift 3 is 6.6M -- a little bit bigger.  It’s breakdown is:

 76KlibswiftAppKit.dylib
4.5MlibswiftCore.dylib
 44KlibswiftCoreData.dylib
116KlibswiftCoreGraphics.dylib
 36KlibswiftCoreImage.dylib
 68KlibswiftDarwin.dylib
 40KlibswiftDispatch.dylib
552KlibswiftFoundation.dylib
 36KlibswiftIOKit.dylib
 64KlibswiftObjectiveC.dylib
1.0MlibswiftSwiftOnoneSupport.dylib
 48KlibswiftWebKit.dylib
 36KlibswiftXPC.dylib

The Swift 3 version is a debug build, the swift 2.2 version isn’t.
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[swift-users] Amusing function return warning in swift 3

2016-06-12 Thread Marco S Hyman via swift-users
My first try with 5/31 swift 3 snapshot gave me a warning regarding an unused 
return value -- in main.swift.   Wouldn’t you know, NS ApplicationMain is 
declared as

func NSApplicationMain(_ argc: Int32,
_ argv: UnsafeMutablePointer) -> Int32

And what does the second line of function help say?

"This method never returns a result code."

OK ;)
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Dictionary with optional values

2016-05-18 Thread Marco S Hyman via swift-users

> On May 18, 2016, at 11:03 AM, Artyom Goncharov via swift-users 
>  wrote:
> 
> Yes, of course I can use API method but this kind of behaviour for subscript 
> operator seems inconsistent(or even magical) to me because it is possible to 
> initialise a dictionary with nil without casting it. Though nil is a special 
> case it is still a value in the set of all values of a T? type, am I wrong?

There are a few ways to assign nil to a dictionary entry.  First of all

dictionary[“key”] = nil

always removes “key" from dictionary.  To assign a nil value use one of:

dictionary[“key”] = Optional(nil)
dictionary[“key”] = .Some(nil)
dictionary[“key”]? = nil

That last only works if “key” already exists in the dictionary, i.e. you are 
replacing the existing value.  The first two will add a dictionary entry if 
necessary.

I suspect now that you’ve been bitten by this you will remember it forever :)

Marc
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Wrapping function declarations in #if swift()

2016-05-13 Thread Marco S Hyman via swift-users
On May 12, 2016, at 10:38 PM, Tyler Fleming Cloutier via swift-users 
 wrote:
> 
> Hello everyone,
> 
> It does seem like it is currently possible to wrap just the function 
> declaration in an #if swift() directive like so:
> 
> #if swift(>=3.0)
> public func add(filter filterName: String, path: String) {
> #else // ERROR Expected ‘}’ at end of brace statement
> public func addFilter(filterName: String, path: String) {
> #endif

Swift conditional compilation is not line based.   SE-0020 said:

"Like other build configurations, #if swift isn't line-based - it encloses 
whole statements or declarations. However, unlike the others, the compiler 
won't parse inactive branches guarded by #if swift or emit lex diagnostics, so 
syntactic differences for other Swift versions can be in the same file.”

https://github.com/apple/swift-evolution/blob/master/proposals/0020-if-swift-version.md

> Is it possible I’m missing how to do this? This is particularly painful in 
> Swift 3 given the change to move have labels on the first function parameter 
> by default. As far as I can see it means that I am required to wrap the 
> entire function body even if nothing else is incompatible with Swift 3.

My solution: a git branch for 2.x and a git branch for 3.x.   Eventually the 
2.x branch will wither away.

Marc
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


Re: [swift-users] Swift 3.0

2016-04-17 Thread Marco S Hyman via swift-users
Hey Marc,
> 
> You didn’t explicitly state the problem you’re running into but I assume you 
> can’t build the app.

The app builds fine.   Running the app gives me errors such as "Must implement 
numberOfRowsInTableView: and tableView:objectValueForTableColumn:row:” when the 
swiftified version "numberOfRows(in tableView: NSTableView) -> Int” and the 
required methods to support a view based table exist -- unless I got them 
wrong.  

At this point I’m not sure if the issues are mine or the development system.  
My next step is to build some miniature test apps to check such things out.

> As others have noticed 
> (http://comments.gmane.org/gmane.comp.lang.swift.user/1689) the latest 
> development snapshot is broken.

That particular issue didn’t bite me.  I am used to seeing the SourceKitService 
die every 3 or 4 minutes.

Marc
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[swift-users] Swift 3.0

2016-04-17 Thread Marco S Hyman via swift-users
Is this the appropriate list to ask questions regarding the 3.0 snapshots?

I’ve hand converted a small OS X app such that it compiles using the 2016-04-12 
snapshot to get a feel for the changes.  Now I’m trying to get it to run.

Marc
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users