Tuesday, December 23, 2014

Swift: A new programming language for iOS and OS X.

WelCome TO Swift

Introduction to Swift

Swift is an innovative new programming language for Cocoa and Cocoa Touch. Writing code is interactive and fun, the syntax is concise yet expressive, and apps run lightning-fast. Swift is ready for your next iOS and OS X project — or for addition into your current app — because Swift code works side-by-side with Objective-C.




With the release of Xcode 6.1 and iOS 8, Swift 1.1 is final, and you can build and submit your iOS apps written with Swift to the App Store



The Basics 

Swift does away with the standard of declaring variables by starting with their type names, and instead opts to use a Javascript-like ‘var’ keyword to define any variable.
    So for instance in Objective-C where you have this:


NSString *myString = @"This is my string.";
    You now have this:

var myString = "This is my string."

Meanwhile constants are expressed with the ‘let’ keyword


let kSomeConstant = 40
In this case kSomeConstant is implicitly defined as an integer. If you want to be more specific you can specify which type it is like so:


let kSomeConstant: Int = 40

With both arrays and dictionaries, they are described using brackets [ ]


var colorsArray = ["Blue", "Red", "Green", "Yellow"]
var colorsDictionary = ["PrimaryColor":"Green", "SecondaryColor":"Red"]



There’s a lot more to go over, but I think these basics are important to get a start going on to the tutorial. So with that, let’s move on to Hello World.


EX.
Hello World
First, we’re going to write the simplest app imaginable to get started, Hello World. Our app will only do one thing: print “Hello World” to the console.
Set up an Xcode project using the single-view application template, and make sure you opt for Swift as the language.



You should now find an AppDelegate.swift file in the project hierarchy
IN didFinishLaunchingWithOptions write below code.

println("Hello World")

Now press run and you should see a blank app boot up, and the words “Hello World” print to the console.
Note that this will not show up in the iPhone simulator. Look at the bottom of your Xcode window and you’ll see a console that says ‘Hello World!’.



Adding a Table View

In this section, we’re going to actually put some stuff on the screen, yay!
Open up your Main.storyboard file in Xcode and lets drag in a “Table View” object from the Object Library (don’t use a table view controller.) Position this fullscreen in your app window and make sure it lines up with the edges.

Then resize the height by dragging down the top edge and giving a little bit of space (this gives room for the status bar at the top of the phone.) If you run the app at this point, you should see an empty table view in the simulator.




NOW, in ViewController.swift add Data Source & Delegate :

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

& then modify our View Controller class by adding these two functions for UITableView methods

func tableView(tableView: UITableView, numberOfRowsInSection section:    Int) -> Int {
   return 10
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
   let cell: UITableViewCell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "MyTestCell")

   cell.textLabel?.text = "Row #\(indexPath.row)"
   cell.detailTextLabel?.text = "Subtitle #\(indexPath.row)"

   return cell
}

The first method is asking for the number of rows in our section, in this simple tutorial we just hard-code 10, but normally it would be the length of an array controller. This example is intentionally simple.

The second method is where the magic happens. Here we create a new instance of a UITableViewCell called cell, using the Subtitle cell style.
Then, we assign the text value of this cell to the string “Row #\(indexPath.row)”
In Swift, this is how variables are embedded within a string. What we’re doing is retrieving the value of indexPath.row by inserting \(indexPath.row) in to our string, and dynamically replacing it with the row number of the cell. This allows results such as “Row #1″, “Row #2″, etc.
The detail text label is only available in the Subtitle cell class, which we are using here. We set it similarly to “Subtitle #1″, “Subtitle #2″, and so on.

So, Complete Code is Given Below.
import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "MyTestCell")
        
        cell?.textLabel?.text = "Row #\(indexPath.row)"
        cell?.detailTextLabel?.text = "Subtitle #\(indexPath.row)"
        
        return cell!
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

//Output --------------->>>



Go ahead and run your app and you’ll now see an amazing list of cells with titles and subtitles indicating their row numbers. This is one of the most common ways to display data in iOS, and will be sure to serve you well.

For More info about Swift ,  Click Here




No comments:

Post a Comment