Re: [swift-users] Dictionary with optional values

2016-05-18 Thread Nathan Day via swift-users
In objective-c I have come across something like this a lot where a 
NSDictionary has been created from JSON an a NSNull is used to represent an 
actual null in the source JSON versus the absence of the key, most of the time 
I have had to just convert the NSNull to a nil, but I did have a situation 
where I had to treat the two differently with the absence of key falling back 
to a default value but NSNull meaning explicitly null, in swift you could have 
an actual nil in the dictionary.

Sent from my iPhone

> On 19 May 2016, at 6:16 AM, Jens Alfke via swift-users 
>  wrote:
> 
> Thinking about it, I can’t see much use for a dictionary of optionals. What’s 
> the difference between “x has no value” and “x has a value of nil”? I guess 
> it’s that when you iterate the keys you see x. This seems like a tricky use 
> that could easily confuse someone reading the code (who could be you, a year 
> later!) Personally I’d prefer a different, clearer solution, unless this was 
> something performance-critical that led to faster code. In which case I’d add 
> lots of comments!
___
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 Jordan Rose via swift-users

> On May 18, 2016, at 09:38, Ray Fix via swift-users  
> wrote:
> 
> 
>> On May 18, 2016, at 3:56 AM, Artyom Goncharov via swift-users 
>>  wrote:
>> 
>> var noOptDict = ["one": 1, "two": 2, "three": 3 ]
>> noOptDict["one"] = nil
> 
> Wow, interesting.  To me this was surprising behavior too.
> 
> The comment for Dictionary subscript  says:
> 
>/// Access the value associated with the given key.
>///
>/// Reading a key that is not present in `self` yields `nil`.
>/// Writing `nil` as the value for a given key erases that key from
>/// `self`.
> 
> Which is exactly what it is doing.  As the Zhaoxin said, you can use 
> updateValue (and removeValueForKey) to get better results when dealing with 
> optional dictionary values.

There’s a bit more discussion of this on the Apple Swift blog: 
https://developer.apple.com/swift/blog/?id=12 


Jordan

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


[swift-users] file io in Swift 2.2 for Linux (would like to be pointed in the right direction)

2016-05-18 Thread John Myers via swift-users
I've been having trouble figuring out how to read and write data to a
textfile, and not finding much out there for Swift on Linux.  I found code
for reading data from a file on StackOverflow that works for me, but
cludged together my own code for writing to a file.  I'm not sure exactly
what I'm doing, but am making some progress.  Can anyone point me toward a
good source of help for reading and writing data in Swift on Linux.  Thanks!

This is the code that I found that works for reading data:

//  Try to read from a file

//  5/16/2016

import Glibc  //import a Linux library

let path = "Resources/sampleIn.txt"

let BUFSIZE = 1024

print("attempting to open a file for input")

let fp = fopen(path, "r")

if fp == nil {print("error reading file")}

if fp != nil {

 print("reading...")

 var buf = [CChar](count:BUFSIZE, repeatedValue:CChar(0))

 while fgets(, Int32(BUFSIZE), fp) != nil {

   print(String.fromCString(buf)!, terminator:"")

 }

}


This is the code I pieced together for writing data:

import Glibc  //import a Linux library
let path = "Resources/sampleOut.txt"let BUFSIZE =
1024print("attempting to open a file for Output")let fp = fopen(path,
"w+")if fp == nil {print("error writing file")}if fp != nil {
  print("Type a few words to be saved to a file")
  var fewWords=readLine()!
  fputs(fewWords,fp)
  print("writing...")
  }
fclose(fp)


Any help would be appreciated.
Thanks!
John

-- 
John Myers
Mathematics and Computer Science Teacher

--
___
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] Dictionary with optional values

2016-05-18 Thread Jens Alfke 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?

It’s an unfortunate ambiguity, one that comes up in any dictionary API that 
potentially allows nil to be stored as a value. Does a RHS of nil mean to 
remove the key/value pair, or to store a nil as the value?

In particular, the C++ STL goes through horrible contortions to get around 
this, which is part of what makes it so awful and verbose to use. :-P

The intuitive (and most commonly useful) interpretation of “dict[x] = nil” is 
to remove the key, which is what Swift does. If you’ve created a dictionary of 
optionals and need to store a null, you have to add a bit of verbosity to make 
your intention clear. I think that’s fine: it goes along with Alan Kay’s maxim 
that “simple things should be simple, and complex things should be doable.”

(Thinking about it, I can’t see much use for a dictionary of optionals. What’s 
the difference between “x has no value” and “x has a value of nil”? I guess 
it’s that when you iterate the keys you see x. This seems like a tricky use 
that could easily confuse someone reading the code (who could be you, a year 
later!) Personally I’d prefer a different, clearer solution, unless this was 
something performance-critical that led to faster code. In which case I’d add 
lots of comments!)

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


[swift-users] Dictionary with optional values

2016-05-18 Thread Artyom Goncharov via swift-users
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?

> On 18 May 2016, at 18:38, Ray Fix via swift-users  
> wrote:
> 
> 
>> On May 18, 2016, at 3:56 AM, Artyom Goncharov via swift-users 
>>  wrote:
>> 
>> var noOptDict = ["one": 1, "two": 2, "three": 3 ]
>> noOptDict["one"] = nil
> 
> Wow, interesting.  To me this was surprising behavior too.
> 
> The comment for Dictionary subscript  says:
> 
>   /// Access the value associated with the given key.
>   ///
>   /// Reading a key that is not present in `self` yields `nil`.
>   /// Writing `nil` as the value for a given key erases that key from
>   /// `self`.
> 
> Which is exactly what it is doing.  As the Zhaoxin said, you can use 
> updateValue (and removeValueForKey) to get better results when dealing with 
> optional dictionary values.
> 
> Ray
> 
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users


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


Re: [swift-users] Member wise initializer doesn't work with default-initialized const properties

2016-05-18 Thread Jens Alfke via swift-users

> On May 17, 2016, at 7:48 AM, Neil Faiman via swift-users 
>  wrote:
> 
> “You can provide a default value for a stored property as part of its 
> definition, as described in Default Property Values. You can also set and 
> modify the initial value for a stored property during initialization. This is 
> true even for constant stored properties, as described in Assigning Constant 
> Properties During Initialization.” (Classes and Structures / Stored 
> Properties)

You can assign the value of an as-yet-unassigned ‘let’ property, once. If that 
happens in the default initialization phase (before any initializer methods 
run), then you’ve used up that chance. The initializer would be setting a 
second value, which isn’t allowed.

Read up on the phases of initialization in the Swift book.

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


Re: [swift-users] Member wise initializer doesn't work with default-initialized const properties

2016-05-18 Thread Neil Faiman via swift-users
> On May 17, 2016, at 7:03 AM, Jeremy Pereira  
> wrote:
> 
> 
>> On 16 May 2016, at 22:37, Neil Faiman via swift-users 
>>  wrote:
>> 
>> Using the default Swift with Xcode 7.3.1.
>> 
>> It appears that you cannot use the implicit memberwise initializer with a 
>> struct that has “let” properties with default values.
> 
> It’s not a default value, it is *the* value
> 
>> 
>> I don’t believe that the Apple _The Swift Programming Language_ mentions 
>> this restriction.
> 
> Chapter “The Basics”, section “Constants and Variables”
> 
> “The value of a constant cannot be changed once it is set”.

But:

“You can provide a default value for a stored property as part of its 
definition, as described in Default Property Values. You can also set and 
modify the initial value for a stored property during initialization. This is 
true even for constant stored properties, as described in Assigning Constant 
Properties During Initialization.” (Classes and Structures / Stored Properties)

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


[swift-users] Measuring time accurately on Linux

2016-05-18 Thread Michal Kalinowski via swift-users

It's my first time posting to users mailing list so hello everyone.

I'm currently optimizing code of my server written in Swift. To do that 
reliably I want to be able to measure how long it takes to execute specific 
methods.

On OSX I'm using mach_absolute_time() that I've wrapped in a struct for easier 
interaction and it works fine. Now I'd like to do the same tests on a copy of 
my actual platform that runs Ubuntu 15.10.

Mach0 library that I use is not available on Linux, instead I could use 
gettimeofday or clock_gettime. For that I would have to expose time.h to my 
swift code, however I'm unsure how to do it exactly.
I have started putting my timing struct in a swift package as a first step. Now 
I'm stuck and here are my questions:
    * How exactly do I use clang's modulemap with package manager?
    * How do I make modulemap include time.h only on Linux?
    * Is it necessary to include full path to time.h header or I can omit it in 
case of system headers?

Here is a link to my package for future reference (no linux code there for now).
https://github.com/michalkalinowski-/Timer

Also, if anyone is aware of a package that uses C library in place of OSX C 
method to work cross-platform please let me know. It would be a great guidance 
for me as I have a very vague idea of how package manager and C libs import 
work.

Regards,
M. Kalinowski___
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 David Sweeris via swift-users
I'm not in front of Xcode, so I can't confirm this, but I suspect that 
`optDict["one"] = nil as! Int?` will set "one" to nil, rather than removing 
"one".

Whatever the rules for inferring the type of `nil` when an 
Optional is involved are, it seems like it always 
infers the one I don't want.

Hope that helps.

- Dave Sweeris

> On May 18, 2016, at 05:56, Artyom Goncharov via swift-users 
>  wrote:
> 
> Hi, here is the playground snippet:
> 
> var noOptDict = ["one": 1, "two": 2, "three": 3 ]
> noOptDict["one"] = nil
> noOptDict // “one” is gone
> 
> var optDict: [String: Int?] = ["one": 1, "two": 2, "three": nil]
> optDict["one"] = nil
> optDict // “one” is gone but “three” is still there
> 
> So the first dict instance works as it should, the second using opt values 
> allows to store nil but deletes the key when you assign nil. Is it bug, 
> feature, or both?
> 
> Best wishes,
> Artyom
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


[swift-users] Dictionary with optional values

2016-05-18 Thread Artyom Goncharov via swift-users
Hi, here is the playground snippet:

var noOptDict = ["one": 1, "two": 2, "three": 3 ]
noOptDict["one"] = nil
noOptDict // “one” is gone

var optDict: [String: Int?] = ["one": 1, "two": 2, "three": nil]
optDict["one"] = nil
optDict // “one” is gone but “three” is still there

So the first dict instance works as it should, the second using opt values 
allows to store nil but deletes the key when you assign nil. Is it bug, 
feature, or both?

Best wishes,
Artyom
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users