

Show HN: 2048 in Swift - austinz
https://github.com/austinzheng/swift-2048

======
olgagalchenko
>> Still have no idea how to make things private, public, or protected. In
Objective-C you could hide internal details away in the .m file. In Swift,
everything is exposed by default.

I think it just hasn't been released yet. It's part of the Known Issues
section of Xcode 6 release notes[0].

[0]
[http://adcdownload.apple.com//wwdc_2014/xcode_6_beta_ie8g3n/...](http://adcdownload.apple.com//wwdc_2014/xcode_6_beta_ie8g3n/xcode_6_beta_release_notes.pdf)

~~~
dzlobin
I asked a Swift engineer named Greg about this yesterday at a Swift lab at
WWDC -- he said that it is forthcoming, but gave no estimates or timelines.
Exception handling, however, doesn't seem to be on the roadmap.

~~~
joncooper
Count me in the "happy to not have exceptions" camp.

------
seanalltogether
Can someone give a quick overview of whats going on with this code? I'm
feeling a bit lost on how enums are being declared and used in swift.

    
    
      enum ActionToken {
        case NoAction(source: Int, value: Int)
        case Move(source: Int, value: Int)
        case SingleCombine(source: Int, value: Int)
        case DoubleCombine(source: Int, second: Int, value: Int)
    
        // Get the 'value', regardless of the specific type
        func getValue() -> Int {
          switch self {
          case let .NoAction(_, v): return v
          case let .Move(_, v): return v
          case let .SingleCombine(_, v): return v
          case let .DoubleCombine(_, _, v): return v
          }
        }
        // Get the 'source', regardless of the specific type
        func getSource() -> Int {
          switch self {
          case let .NoAction(s, _): return s
          case let .Move(s, _): return s
          case let .SingleCombine(s, _): return s
          case let .DoubleCombine(s, _, _): return s
          }
        }
      }

~~~
TheCoreh
Swift's enums are like Rust's enums, so they're much more powerful than C's
enums:

In C an enum can assume one of multiple predefined values of type `int`.

In Swift an enum can work like that too, but it can also assume values of
types other than int. This is similar to the behavior of the unions in C, but
more high level, with proper safety checks in place.

In Swift enums can also have methods, so there are two methods to return
fields from the values contained in the enum —regardless of the actual type
being held at that time.

~~~
seanalltogether

      case let .DoubleCombine(s, _, _): return s
    

Is the underscore a special character in this case to tell the compiler not to
read in the value of those arguments? Or is it actually loading in "second"
and then "value" into an argument named _?

~~~
weego
Assuming it's the same as other pattern matching langs it is a wildcard that
signals the intent that you don't care about what value those would be.

------
austinz
Creator here. Glad people like it! I do have plans for slightly less useless
projects in the future, but I did have a couple of reasons for building this
particular project:

\- I wanted to try out Swift and see how it felt in practice to build a
reasonably-sized project in it - and to see if the Xcode Developer Preview was
anywhere near up to the task. I also wanted to see how the Cocoa APIs were
going to work with Swift.

\- I had an existing Objective-C implementation that I could work from. I
wanted to see if Swift could make the implementation easier to read, more
elegant, etc. While the original is hardly a thing of beauty, I thought it
still might be useful to see how business logic to perform the same task might
differ between the two languages.

I hope people who take a look, fork the repo, download and play with the code
will enjoy reading through it and maybe take it as a taste of how things might
be done (or not done) in the future.

------
unfunco
As an aside, it's interesting that GitHub as added support for Swift language
detection so quickly, yet I frequently read stories (on HN too) about their
passiveness to correct or add other (sometimes more) popular languages.

~~~
nkvoll
This likely has a lot to do with how they do detection.

Swift uses the .swift extension, which is pretty unique, so detection is as
simple as checking for the file extension with a very low risk of a bad
classification.

Some of the older, more popular languages uses more common file-suffixes,
which may be shared between multiple programming languages, which has a
negative impact on classification accuracy.

~~~
GuiA
Yes. In support of this theory: Github does not syntax color Swift files yet
(eg.
[https://github.com/austinzheng/swift-2048/blob/master/swift-...](https://github.com/austinzheng/swift-2048/blob/master/swift-2048/NumberTileGame.swift))

------
Zaheer
There were article about how FlappyBird is the new HelloWorld these days. Most
probably some-what humorous but it's great to see how many resources and
projects have already been developed in Swift.

Shameless Plug: Want to learn Swift? Check out
[[http://www.LearnSwift.tips](http://www.LearnSwift.tips)]

~~~
AH4oFVbPT4f8
Normally I'd ignore the shameless plug but that's a nice set of tutorials
you've put together there.

------
k-mcgrady
I was playing with it a bit this morning porting a helper XML parsing class[0]
I have over to it and building a simple RSS Reader project.

I'm one of those people that really likes objective-c but I was surprised that
when I had to go back to working in objective-c this afternoon I started to
realise how much time Swift would save me. I feel like at a glance it's harder
to understand the code but it requires way fewer keystrokes and overtime will
probably become much easier to understand at a glance.

Overall I really like it. I'll have to write a lot more code in it first but I
could see myself switching to this for all my iOS code when iOS 8 is released.

[0]
[https://github.com/KieranMcGrady/KMXMLParser](https://github.com/KieranMcGrady/KMXMLParser)

------
talhof8
I guess this whole "X in Go" trend is now being replaced with "X in Swift"

~~~
dllthomas
I think the trend this joins is "2048 in Y", not "X in Go"...

------
kin
Kudos for crediting "Threes"! That game doesn't get enough credit amidst the
popularity of 2048.

~~~
saumotions
At least it got a design award
[https://developer.apple.com/design/awards/2014/Threes/](https://developer.apple.com/design/awards/2014/Threes/)

------
callmeed
Is it cool to post projects like this in GitHub? (give Apple's NDA/PLA for iOS
Dev Center members).

I ask because I have some things I'd like to post but am reluctant.

~~~
williwu
They just changed it in the new developer agreement, that you probably agreed
to if you logged into your developer portal.

It's under "Information Deemed Apple Confidential". "Further, Apple agrees
that You will not be bound by the foregoing confidentiality terms with regard
to technical information about pre-release Apple Software and services
disclosed by Apple at WWDC (Apple's Worldwide Developers Conference), except
that You may not post screen shots, write public reviews or redistribute any
pre-release Apple Software or services."

~~~
visarga
> You may not post screen shots, write public reviews or redistribute

Well, count me out. I like to work free.

------
oscargrouch
Funny how 2048 is now the new "hello world!" :)

------
ndomin
does anyone else fear there will be tons more duplicates on the app store of
apps re-written in Swift? End users won't see a difference.

------
klaussilveira
You're putting the whole thing inside GameModel? Are you serious?

