Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
lord funk
Feb 16, 2004

Doctor w-rw-rw- posted:

Swift code:
// Playground - noun: a place where people can play

import Cocoa


func foo(inout x:Array<String>) -> Array<String> {
    x[0] = "qwer"
    return x
}

foo(["asdf", "1234"])
This crashes my Xcode. Known problem? Anyone else able to reproduce?
Yeah me too.

Adbot
ADBOT LOVES YOU

WORLDS BEST BABY
Aug 26, 2006

lord funk posted:

Yeah me too.

Same. While we're on the topic of playground bugs… are you supposed to be able to import UIKit into playgrounds? I've tried 'import UIKit' in all the configurations I could think of, including in an iOS project — but every time I just get "No such module 'UIKit'".

Doctor w-rw-rw-
Jun 24, 2008

WORLDS BEST BABY posted:

Same. While we're on the topic of playground bugs… are you supposed to be able to import UIKit into playgrounds? I've tried 'import UIKit' in all the configurations I could think of, including in an iOS project — but every time I just get "No such module 'UIKit'".

Playgrounds, for now, seem to be OS X only, without the simulator environment, so if that's intended it doesn't seem to be supported, currently.

WORLDS BEST BABY
Aug 26, 2006

Doctor w-rw-rw- posted:

Playgrounds, for now, seem to be OS X only, without the simulator environment, so if that's intended it doesn't seem to be supported, currently.

Thanks, that seems to be it.

Kobayashi
Aug 13, 2004

by Nyc_Tattoo
I think y'all talked about this earlier, but the stuff about collections being copies but arrays being references unless the length changes and then it's a copy seems a little weird to me. Is that normal?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

lord funk posted:

Yeah me too.

Filed.

ptier
Jul 2, 2007

Back off man, I'm a scientist.
Pillbug
rjmcall, I just watched the SotU part on Swift.

All I can say is:

Thank you, thank you, thank you.

extensions and generics alone will make my life so much easier. I cannot wait to start beating on it and porting an app or two to see how easy it is (seems like really really easy).

bumnuts
Dec 10, 2004
mmm...crunchy
Why is @lazy the only keyword with the @ prefix? Why not just lazy?

pokeyman
Nov 26, 2006

That elephant ate my entire platoon.

bumnuts posted:

Why is @lazy the only keyword with the @ prefix? Why not just lazy?

There's a ton of attributes with the @ prefix. A list is in the book, in the chapter called "Language Reference", in the section called "Attributes".

Hughlander
May 11, 2005

Any chance of getting the RC4 benchmarks in Obj-C and Swift posted? Curious what the code was that was 1.7X faster in swift.

Simulated
Sep 28, 2001
Lowtax giveth, and Lowtax taketh away.
College Slice

bumnuts posted:

Why is @lazy the only keyword with the @ prefix? Why not just lazy?

I don't know if they plan on supporting custom attributes but @ is the syntax for attributes in general.

LP0 ON FIRE
Jan 25, 2006

beep boop

rjmccall posted:


I'll mention custom operators in :siren: my talk :siren:, and I'm pretty sure they're covered in the guide, but essentially you just declare the operator somewhere in the program:

Swift code:
operator infix|postfix|prefix %%% {
  // You can skip these and Swift will just complain if you ever write this next to another binary operator.
  // associativity left|right
  // precedence some-magic-number
}
And then you define a global function with that name:

Swift code:
func %%%(a: Int, b: Int) -> (Int,Bool) {
  // whatever
}
(Advanced Swift, Thursday at 11:30)

I was just reading about this. If I'm understanding correctly, it can be any sequence of the following 14 characters: / = - + * % < > ! & | ^ . ~
https://developer.apple.com/library...097-CH27-XID_48

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
Looking forward to the inevitable Brainfuck.swift.

lord funk
Feb 16, 2004

If Swift is a recent name, what did you guys call the language internally while you were developing it?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

LP0 ON FIRE posted:

I was just reading about this. If I'm understanding correctly, it can be any sequence of the following 14 characters: / = - + * % < > ! & | ^ . ~
https://developer.apple.com/library...097-CH27-XID_48

. is restricted. You can also use non-ASCII characters; we haven't decide exactly which set, but it'll include the math symbols for sure.

lord funk posted:

If Swift is a recent name, what did you guys call the language internally while you were developing it?

I don't know why, but we never disclose stuff like that. It's not unlikely that somebody will slip up and say it at some point, though.

LP0 ON FIRE
Jan 25, 2006

beep boop

rjmccall posted:

. is restricted. You can also use non-ASCII characters; we haven't decide exactly which set, but it'll include the math symbols for sure.

Awesome! Someone in another thread about Swift wanted to use ∈ really badly.

spongeh
Mar 22, 2009

BREADAGRAM OF PROTECTION
Is there any chance at being able to use this for cross-platform dev in the future? If it outputs LLVM, and integrates with C libraries, it should be feasible once it hits open source, right? Or is too much useful stuff tied up in Cocoa that you'd lose most of the nice bits if you were trying to build on Linux or Windows.

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...
Having some problems with interop. The docs seem to suggest that I should import "ModuleName-Swift.h" into all the ObjC files that need to have access to Swift classes.

My module name is for sure AdultQuest because Xcode generated "AdultQuest-Bridging-Header.h" for the other side. I don't see AdultQuest-Swift.h in my project anywhere, though, and when I try to import it, I get an error complaining that it's unable to find it.

Also, when I had a protocol defined on the ObjC side which used UIKit classes, I couldn't use any classes that implemented that protocol in my Swift classes; I got compile errors about UILocalNotification being an unknown class. I converted that one to Swift and now it works fine, but now I'm stuck with this bridging issue which is much more painful.

I'm having a blast with the language so far though. Yes let's please convert two files into one that's half the length of either one.

spongeh posted:

Is there any chance at being able to use this for cross-platform dev in the future? If it outputs LLVM, and integrates with C libraries, it should be feasible once it hits open source, right? Or is too much useful stuff tied up in Cocoa that you'd lose most of the nice bits if you were trying to build on Linux or Windows.

I don't see any reason why the core language couldn't be ported. This isn't like Objective-C where really the only reason anyone would write code in it is because of Cocoa.

Axiem
Oct 19, 2005

I want to leave my mind blank, but I'm terrified of what will happen if I do
So while trying to fiddle with Unit Testing, I've gotten to the point where I want to create mocks. I've imported the mocking framework that I want to use into the Xcode project as a subproject. I'm able to use it fine with Objective-C code in the project, and create mocks no problem. However, I can't get Swift to recognize that it's there (or, therefore, let me use the methods to mock something).

What is the name of the module? I'm trying "import Stubble", and it says there is no such module. In Objective-C, I just need to "#import <Stubble/Stubble.h>", and it works. Or is there other voodoo I need to incant to make it work?

(Or is this going to be answered in the Integrating Swift talk?)

Edit: I do have a bridging header in my Test project now that has the "#import <Stubble/Stubble.h>" in it. That doesn't throw an error, but doesn't otherwise seem to do anything.

Axiem fucked around with this message at 06:22 on Jun 4, 2014

Deus Rex
Mar 5, 2005

nebby posted:

for future reference, this is a classic fallacy:

http://en.wikipedia.org/wiki/Nirvana_fallacy

for future reference, this is a classic fallacy:

https://en.wikipedia.org/wiki/Argument_from_fallacy

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Dessert Rose posted:

Having some problems with interop. The docs seem to suggest that I should import "ModuleName-Swift.h" into all the ObjC files that need to have access to Swift classes.

My module name is for sure AdultQuest because Xcode generated "AdultQuest-Bridging-Header.h" for the other side. I don't see AdultQuest-Swift.h in my project anywhere, though, and when I try to import it, I get an error complaining that it's unable to find it.

It sounds like Xcode decided to name your bridging header "AdultQuest-Bridging-Header.h" for some reason. The file name isn't magical, and I assume there's a build setting for it somewhere. Basically, Swift "includes" it to find local definitions from your project that it should make visible to Swift files.

Not sure what's going on with your protocol. I know there's an occasional issue where we import protocols as FooProto instead of Foo, but IIRC that should only happen when there's an actual name conflict with an ObjC protocol (which live in a different namespace in ObjC).

Doctor w-rw-rw-
Jun 24, 2008
Swift code:
class A<T> {
    var B : T?

    init() {
    }
}
results in:
code:
error: unimplemented IR generation feature non-fixed class layout
    var B : T?
        ^
LLVM ERROR: unimplemented IRGen feature! non-fixed class layout
This is going to be supported later, right?

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe
Yes. That one is definitely on the list.

As a workaround, you can probably afford to add AnyObject to the protocol constraints on your type parameter.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

spongeh posted:

Is there any chance at being able to use this for cross-platform dev in the future? If it outputs LLVM, and integrates with C libraries, it should be feasible once it hits open source, right? Or is too much useful stuff tied up in Cocoa that you'd lose most of the nice bits if you were trying to build on Linux or Windows.

There's a significant amount of code dedicated to Cocoa-specific interop, but non-Darwin platforms should just never trigger it by virtue of not having ObjC/CF code. I expect that a port would need to duplicate a pretty simple subset of the ObjC runtime (just for class-layout resilience) and then be done.

Obviously, if you want a Windows port to e.g. import COM types as Swift classes, that is (1) implementable but (2) significantly more work.

ATM Machine
Aug 20, 2007

I paid $5 for this
Mind you, this is coming from a non-iOS developer, but is there any reason for variables in strings to be surrounded by parentheses, with the opening one escaped? All the examples of strings with variables I've seen use this, and I haven't seen any other methods of using variables in strings.

An example from the docs regarding Strings and Characters:

code:
println("unusualMenagerie has \(countElements(unusualMenagerie)) characters")
This just seems really strange when a curly bracket is rarely used outside of maths, music and programming; parentheses are used quite commonly by comparison, so it seems strange on first glance that to pass a variable into a string, you have to escape a character for it.


Unless Swift has a string format method that I haven't been able to find, but it seems like an odd design choice to me.

ultramiraculous
Nov 12, 2003

"No..."
Grimey Drawer
Is there any plan to build out the collections a bit more? I'm necessarily asking if you're going to do completely reimplement Scala's collection library, but man would that be convenient sometimes.

shrughes
Oct 11, 2008

(call/cc call/cc)

ATM Machine posted:

This just seems really strange when a curly bracket is rarely used outside of maths, music and programming; parentheses are used quite commonly by comparison, so it seems strange on first glance that to pass a variable into a string, you have to escape a character for it.

The way Perl and PHP do it is bad, for all the obvious reasons, and that only works if you have $ in front of all your variables. The way Ruby does it is bad because it introduces a second escape character, #. Having a naked curly bracket would be surprising and weird. It makes sense to reuse the existing escape character.

tstm
Jun 4, 2014
The optimizer seems to do some really weird things for me.

I ported this benchmarking simulation to Swift.

After long periods of toying around with different things I actually got out something that performs nicely: http://tstm.info/files/nbody.swift

But for some reason I still have to do very odd things to be able to make it performant. In the function advance() I'm doing an extra
code:
 let _planets = planets 
without any good reason.

If I take it away, and replace the advance() function with something like this:
code:
func advance(dt: Double) {
    var n = planets.count
    for var i = 0; i < n ; i++ {
        let iPlanet = planets[i]
        for var j = i+1; j < n; j++ {
            let jPlanet = planets[j]
            let dx = iPlanet.x - jPlanet.x
            let dy = iPlanet.y - jPlanet.y
            let dz = iPlanet.z - jPlanet.z
            
            let dSquared = dx * dx + dy * dy + dz * dz
            let distance = sqrt(dSquared)
            let mag = dt / (dSquared * distance)
            
            planets[i].vx -= dx * jPlanet.mass * mag
            planets[i].vy -= dy * jPlanet.mass * mag
            planets[i].vz -= dz * jPlanet.mass * mag
            
            planets[j].vx += dx * iPlanet.mass * mag
            planets[j].vy += dy * iPlanet.mass * mag
            planets[j].vz += dz * iPlanet.mass * mag
        }
    }
    for var i = 0; i < n ; i++ {
        planets[i].x += dt * planets[i].vx
        planets[i].y += dt * planets[i].vy
        planets[i].z += dt * planets[i].vz
    }
}
The code slows down tenfold. I do not understand why this might be happening - and I'm really trying to code within reasonable standards here, I'm not a good programmer by any stretch, but something doesn't seem quite right in the optimizer. Or is it me doing something horribly wrong?

Also, I've found that using a static or a dynamic Array to store the bodies in will result in lots and lots and lots of retain/release, and that's why I had to go and use a static-size ContiguousArrayBuffer instead of an Array.

As for the actual results, with the weird "optimizations" I had to do, I can run 5 000 000 iterations of nbody in ~3.8 seconds on my rMBP. The original Java code I ported does the same in 0.9s - swift is slower but not by that much, and I'm sure there could be other things still going wrong.

shodanjr_gr
Nov 20, 2007
So is there a way to explicitly define a main() entry point for a program written in Swift? The iBook that's available online doesn't specify anything (it just says that stuff in global scope gets executed first) and I don't have access to the Betas so I can play around.

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...

rjmccall posted:

It sounds like Xcode decided to name your bridging header "AdultQuest-Bridging-Header.h" for some reason. The file name isn't magical, and I assume there's a build setting for it somewhere. Basically, Swift "includes" it to find local definitions from your project that it should make visible to Swift files.

Not sure what's going on with your protocol. I know there's an occasional issue where we import protocols as FooProto instead of Foo, but IIRC that should only happen when there's an actual name conflict with an ObjC protocol (which live in a different namespace in ObjC).

There is a build setting for it, but it starts out generated from your module name. It's just how I know for sure what the module name is.

I thought maybe this could be due to some circular dependency issues, but now I'm thinking it's because the Swift part of my code isn't compiling properly.

Also, I think I have a reproducible segfault in the compiler, which I'm going to try and trim down. Unfortunately for me, it's in one of the core methods of my main model class. (Which should really be broken into pieces.)

Doctor w-rw-rw-
Jun 24, 2008

WORLDS BEST BABY posted:

Thanks, that seems to be it.

Nevermind. I think that Xcode might have a right-sidebar option for iOS playgrounds.

Carthag Tuek
Oct 15, 2005

Tider skal komme,
tider skal henrulle,
slægt skal følge slægters gang



What's the difference between Array<String> and String[]? Are they just two different ways of describing the same thing?

Filburt Shellbach
Nov 6, 2007

Apni tackat say tujay aaj mitta juu gaa!
Yep, though I think it was mentioned in the book that String[] is the preferred style.

WORLDS BEST BABY
Aug 26, 2006

Doctor w-rw-rw- posted:

Nevermind. I think that Xcode might have a right-sidebar option for iOS playgrounds.

Oh hey, they hid that well! Unfortunately, after selecting iOS in the platform dropdown all I get is "Error running playground. Cannot find suitable target device". :sigh: Thanks anyway.

Edit: Got it. For whatever reason playgrounds require at least one device profile to be added in iOS Simulator, and I didn't have any.

WORLDS BEST BABY fucked around with this message at 20:38 on Jun 4, 2014

Carthag Tuek
Oct 15, 2005

Tider skal komme,
tider skal henrulle,
slægt skal følge slægters gang



Filburt Shellbach posted:

Yep, though I think it was mentioned in the book that String[] is the preferred style.

Nice, I prefer that too :)

Doh004
Apr 22, 2007

Mmmmm Donuts...
Read some of the book while flying this morning, excited to start trying it out.

Thanks for answering our questions rjmccall!

Siguy
Sep 15, 2010

10.0 10.0 10.0 10.0 10.0
I've now converted a couple of tiny classes to Swift for fun. It's certainly much cleaner looking than the code it replaced.

I think the language's true potential won't be clear until the Cocoa APIs start to be rewritten with Swift in mind. It looks like a modern, friendly language until you try to do something that would be simple in a scripting language like capture substrings using regular expression matches. Then you're neck deep in objective C APIs, creating NSRegularExpression objects with bitmask options and pointer based error handling to do something that is baked into strings in a scripting language.

Dessert Rose
May 17, 2004

awoken in control of a lucid deep dream...
Yeah, on that note. What's the easiest way to go from an NSSet to a strongly typed Swift array?

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
set.allObjects as Foo[]

Adbot
ADBOT LOVES YOU

ufarn
May 30, 2009


Ouch?

  • Locked thread