Apple's next-gen programming language

Error365!

Well-known member
  • Jun 27, 2012
    12,831
    9,191
    113
    එක තැනක නෑ
    Meet Swift: Apple's next-gen programming language that may replace Objective-C

    Apple shocked longtime Mac and iOS developers at WWDC 2014 by introducing Swift, a new programming language that will likely replace Objective-C.

    Here's a quick guide to using Swift
    :):)


    swifthero.png


    Image: Apple

    Swift is the new, powerful programming language developed by Apple. It's designed to be safe and interoperable with existing code bases, and it takes a new approach to development on the Mac and iOS devices. Swift works with Xcode 6, and is compatible with modern versions of iOS and OS X.
    Swift works with and is interoperable with Objective-C. It provides new syntax and syntactics that we've not seen before from Apple, and allows developers to experience a modern language without any of the legacy baggage of the C language, which has been a staple of programming since the '70s.
    We give you the full details on how to use Swift in your development projects.
    Can I use Swift now?

    Swift is shipping with the latest version of Xcode 6 beta. You can create a Swift-based project by performing the following steps:

    1. Open Xcode.
    2. Click File | New | Project... .
    3. After selecting your project type, select Swift from the Language drop-down menu (Figure A).
    Figure A

    swiftfigurea062214.png
    Image: Apple
    Selecting Swift in Xcode couldn't be easier with the Language selection box.

    Note: Xcode 6 defaults to Swift, so keep that in mind if you're still creating Objective-C projects for iOS or OS X.
    Experimenting with Swift via the REPL

    Swift includes something a bit new to Objective-C and compiled language users: a Read-Eval-Print-Loop (REPL). With the REPL, you can type in a line of code, have it evaluated for a result, and then have it returned to you. This means you can experiment with an idea by simply typing an expression in the command line -- it's a pretty nifty feature.
    A REPL is standard with interpreted languages such as Ruby, but many developers may not have experience with it. And, even though Swift has a REPL, it's still a compiled language.
    Experimenting with Swift via the Xcode Playgrounds

    Figure B

    swiftfigureb062214.jpg
    Image: Apple
    Playgrounds let you visually see what is happening when working with visual elements such as games in SpriteKit and SceneKit, or standard images when doing animations.

    Using the REPL, Xcode implements a new feature called Playgrounds for experimenting with code and seeing the results in real-time. A Playground is a file that can be included in your iOS or OS X projects, or live outside those projects as a standalone file. It gives you a place to work like a sketchpad when doing graphics, testing algorithms, and more.
    Follow these steps to create a Playground:

    1. Open Xcode 6.
    2. Select File | New | File.
    3. Select either Source under either iOS or OS X templates.
    4. Choose the Playground file option.
    5. Click Next.
    As you type into the code editor, the evaluation of the code is displayed in the right-hand sidebar, giving you the near-instant ability to see the output of any function or calculation.
    Submitting Swift apps to the App Store

    Using Swift requires creating and signing your binaries using Xcode 6; because of this, you cannot submit applications using Swift code to the iTunes or Mac App Stores at this time. When Yosemite and iOS 8 are released later this year, you will be able to begin submitting Swift binaries to the stores.
    How do Swift and Objective-C differ?

    The biggest and perhaps most drastic change between Objective-C and Swift is that the new language doesn't use any braces in the syntax. Swift is minimalist and tries to get away from distractions in the written code such as brackets, parenthesis around control statement conditionals, the ending-line semicolon, and more. Below are a few examples of how this code has changed.
    Constants, variables, and properties... oh my

    In Objective-C, you might see a constant defined like this, using the compiler declaration #define:

    #define kMyConstantName 0.33

    In Swift, this can easily be redefined using the let keyword, like this:

    let kMyConstantName = 0.33


    Using type inference, you no longer need to tell the compiler what type the constant should be; however, you should not confuse type inference with dynamically typed languages such as Ruby. Swift is still a strongly-typed language, but the compiler can automatically infer the type and type the variable or constant correctly.
    Using this let keyword, it tells the compiler that this variable will not be changed during run time, and you will get a compile time error if you try to set another value with let-defined constants.
    If you wish to use a variable, then the var keyword is what you're looking for. In Objective-C, you might see a string defined like this:

    NSString *myString = @"This is a string.";

    With Swift, you can write this code to do the same thing:

    var myString = "This is a string."


    With Swift you no longer need to define @properties declarations like you did in Objective-C. When you use the var keyword in your Swift file, it'll work just like an @property did in Objective-C, allowing you to set values from other classes when instantiating an object.
    Blocks vs. closures

    In Objective-C 2.0, Apple introduced the idea of blocks. While not revolutionary, this addition to the language helped modernize Objective-C. With Swift, Apple continues this modernization with closures.
    Closures are essentially simplified blocks, and blocks and closures can be used interchangeably in Swift when dealing with Objective-C bridged code.
    In Objective-C, you might have something like this when dealing with UIView animations, which uses blocks for completion handlers:

    [UIView animateWithDuration: 0.33 animations:^{ //Code here to handle animations }];

    This is the same code in Swift using closures instead of blocks to interact with the same UIKit code written in Objective-C:

    UIView.animateWithDuration(0.2, animations: { //Code here to handle animations })


    Closures can be very powerful, and can also be used to store a reusable code block in a constant or variable for use throughout your classes in Swift, just as you could do with blocks in Objective-C.
    Can I use Swift with Objective-C and vice versa?

    Objective-C code can be bridged into Swift, meaning that you can access all of your existing Objective-C classes, objects, and their properties with ease. You can use your Swift classes inside of your Objective-C classes to do things like open display a View Controller written in Swift through an Objective-C presenting view controller.
    Learn more about Swift with these Apple resources

    Apple has pulled out all of the stops to ensure that developers know exactly how to use Swift. I bet that by the next WWDC most if not all of the sessions will show API usage examples in Swift, so it's definitely worthwhile to spend the time to learn this new language. Here are some resources to get you started.
    iBooks


    • The Swift Programming Language: This book goes into the gritty details of the new language and how to use it. You won't find any mention of OS-specific details here -- it's just about using the language and its new syntax. This should be your first look at Swift.
    • Using Swift with Cocoa and Objective-C: This book delves into the word of Cocoa and how to use existing Cocoa APIs in your applications. It also covers using existing Objective-C classes in your Swift code. This book should be read after you have a good understanding of the concepts in the first book.
    Videos from WWDC sessions


    http://www.techrepublic.com/article/meet-swift-apples-next-gen-programming-language-that-may-replace-objective-c/

    :):):)

    Follow the Language Guide from developer.apple.com >> :):) https://developer.apple.com/library...ml#//apple_ref/doc/uid/TP40014097-CH5-XID_399
     
    Last edited:
    • Like
    Reactions: IDG

    K_ZONE

    Well-known member
  • May 28, 2009
    5,150
    4,041
    113
    invoke db "Injected Memory"

    Post එක කියවන්න කලින් කියපංකො බලන්න ඕක PC වල Develop කරන්න පුලුවන්ද කියල.. නැත්නම් කියවන එක තේරුමක් නැ, :D ඇපල් ලොකුසීන්..

    Windows වලදි ඇප් එක Sign කරන්න පුලුවන්ද ?
     

    mjayanatha

    Well-known member
  • Jan 9, 2008
    28,378
    1,885
    113
    යන එන මං නැහැ

    Post එක කියවන්න කලින් කියපංකො බලන්න ඕක PC වල Develop කරන්න පුලුවන්ද කියල.. නැත්නම් කියවන එක තේරුමක් නැ, :D ඇපල් ලොකුසීන්..

    Windows වලදි ඇප් එක Sign කරන්න පුලුවන්ද ?

    1. :lol: :eek:
     

    Error365!

    Well-known member
  • Jun 27, 2012
    12,831
    9,191
    113
    එක තැනක නෑ

    Post එක කියවන්න කලින් කියපංකො බලන්න ඕක PC වල Develop කරන්න පුලුවන්ද කියල.. නැත්නම් කියවන එක තේරුමක් නැ, :D ඇපල් ලොකුසීන්..

    Windows වලදි ඇප් එක Sign කරන්න පුලුවන්ද ?


    Xcode eka windows walata enne naha kiyala hamoma dannawane ban :yes:
    Meka iOS and Mac app develop karanda aluthin introduce karapu language ekak. me thread eke thiyanne e language eka gana ban. mama methana aluth software ekak gana dala naha ban :no: mulinma topic eka balala indapalla :frown::frown:
     

    NEMISIS

    Well-known member
  • Nov 13, 2013
    11,348
    19,441
    113
    Colombo
    e wage thamai machan penne. mamath start karanda yanne :) rep back machan +

    මම නම් දැනටම පටන් අරන් තියෙන්නෙ. සේරම හොදයි සෙමි කෝලන් එක නැති එක නම් නිකං මොකද්ද වගේ. ඒ ඇරුනම ලියන කෝඩ් එක හොදට තේරුම් ගන්න පුලුවන්.
     
    • Like
    Reactions: Error365!

    Error365!

    Well-known member
  • Jun 27, 2012
    12,831
    9,191
    113
    එක තැනක නෑ
    මම නම් දැනටම පටන් අරන් තියෙන්නෙ. සේරම හොදයි සෙමි කෝලන් එක නැති එක නම් නිකං මොකද්ද වගේ. ඒ ඇරුනම ලියන කෝඩ් එක හොදට තේරුම් ගන්න පුලුවන්.

    ow eka thamai machan. Semicolon eka thibba nam thawath pahadili :)