Re: [swift-users] Mapping C semantics to Swift

2016-05-19 Thread Ramakrishna Mallireddy via swift-users
An working example,

let fm = NSFileManager.defaultManager()

  var mh = mach_header()   // Defined as C struct, 

  var lc = load_command()  // Defined as C struct 

  var location = 0

  if let data = fm.contentsAtPath("home/user/projects/some.o") {

data.getBytes(, length: sizeof(mach_header))

location += sizeof(mach_header)

data.getBytes(, range: NSRange(location: location, length: sizeof(
load_command)) )

location += sizeof(load_command)

}

-Rk

On Thu, May 19, 2016 at 10:52 PM, Joe Groff via swift-users <
swift-users@swift.org> wrote:

>
> > On May 19, 2016, at 10:00 AM, Ken Burgett via swift-users <
> swift-users@swift.org> wrote:
> >
> > I would like to know if a struct in Swift has any guarantee of
> contiguity, like a struct in C.  I am attempting to port a C program that
> makes many assumptions about the underlying semantics of C structs, and
> does things like overlaying an array over the struct using a cast, and then
> performs and cryptographic hashing of the entire struct by treating it as
> an array.
> >
> > 1.  I a Swift struct contiguous?
> >
> > 2.  If 1 above is true, can I somehow cast an array over the struct?
>
> Swift structs have unspecified layout. If you depend on a specific layout,
> you should define the struct in C and import it into Swift for now.
>
> -Joe
> ___
> 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] Mapping C semantics to Swift

2016-05-19 Thread Joe Groff via swift-users

> On May 19, 2016, at 10:00 AM, Ken Burgett via swift-users 
>  wrote:
> 
> I would like to know if a struct in Swift has any guarantee of contiguity, 
> like a struct in C.  I am attempting to port a C program that makes many 
> assumptions about the underlying semantics of C structs, and does things like 
> overlaying an array over the struct using a cast, and then performs and 
> cryptographic hashing of the entire struct by treating it as an array.
> 
> 1.  I a Swift struct contiguous?
> 
> 2.  If 1 above is true, can I somehow cast an array over the struct?

Swift structs have unspecified layout. If you depend on a specific layout, you 
should define the struct in C and import it into Swift for now.

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


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

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

> On May 19, 2016, at 12:58 AM, Quinn The Eskimo! via swift-users 
>  wrote:
> 
> The reason I ask is that most of the time when I mess around with files I 
> transfer the entire file to and from memory, which is something that the 
> Foundation APIs excel at.

I believe Mac/iOS developers usually transfer the entire file because that’s 
the particular hammer that Foundation provides. For some reason Foundation has 
never had good stream APIs (and they used to be worse!), so it’s much more 
convenient to just use NSData or NSString methods to read and write the entire 
file, than it is to mess around with NSFileHandle or NSStream.

Conversely, most other platforms (certainly C/POSIX and Java) make it easier to 
stream the file (which I believe is more efficient; certainly more scalable) 
than to block-read/write it.

I’m hoping that the Swift translation of Foundation provides greatly-improved 
Stream classes.

—Jens___
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-19 Thread Jeremy Pereira via swift-users

> On 18 May 2016, at 11: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?
> 


It’s correct behaviour, albeit confusing.

The type of a dictionary subscript is Optional where V is the value type. If 
V is itself Optional the the type of the subscript is Optional 
(T?? for shorthand).

Normally, when you assign a value in a context where an optional is required, 
the compiler implicitly wraps the value as an optional. i.e.

let foo: Int? = 2
let bar: Int? = nil

is compiled as if you wrote 

let foo:Int? = Optional.Some(2)
let bar: Int? = Optional.None


When you have a nested optional type combined with the implicit conversion, the 
meaning of nil becomes ambiguous since it could either be the .None value of 
the outer type or a .Some value of the outer type wrapping the .None of the 
inner type.

let foo: Int?? = nil

could be

let foo: Int?? = Optional.Some(Optional.None)

or

let foo: Int?? = Optional.None

depending on where you stick the implicit wrapping. The Swift compiler chooses 
the latter.

You need to force the compiler to choose the former. The most terse way I have 
found to do this so far is

   optDict["one"] = Int?.None



> 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


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

2016-05-19 Thread Quinn "The Eskimo!" via swift-users

On 18 May 2016, at 23:02, John Myers via swift-users  
wrote:

> I've been having trouble figuring out how to read and write data to a 
> textfile …

Your examples show you transferring the data line-by-line.  Is that the end 
goal?  Or just fallout from what you happened to get working?

The reason I ask is that most of the time when I mess around with files I 
transfer the entire file to and from memory, which is something that the 
Foundation APIs excel at.  OTOH, there are times when line-by-line is necessary.

Share and Enjoy
--
Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware


___
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-19 Thread Jens Alfke via swift-users

> On May 18, 2016, at 7:35 PM, Nathan Day  wrote:
> 
> 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

Yeah, this comes from JavaScript, which weirdly has both ‘null’ and ‘undefined’ 
values; they’re kind of similar but not the same, and the latter is more like 
what we think of as null/nil in Swift or Obj-C. I think this was a bad design, 
and unfortunately it crept into JSON, which was based on JavaScript literals.

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