Steps to use Legacy Swift in Xcode 8

Xcode Swift iOS10

Xcode 8.0 is released with Swift 3 and Swift 2.3 compatibility.


If you already having a project with Swift 2.2 language support with Xcode 7.3.1 and you open the project in Xcode 8.0 then, you will be prompted for the migration assistant to do a migration pass. The assistant can also be invoked manually from the menu Edit -> Convert -> To Current Swift Syntax…

  • Use Swift 2.3 Modifies your project to enable the Use Legacy Swift build setting and provides source changes to be able to build against the new SDKs.
  • Use Swift 3 This is recommended. You will get source changes to be able to build your project using Swift 3 and take advantage of all the new features in Xcode 8.0.

If you want to work with new project and you want to work with Legacy code (Swift 2.3) or 3.0 then you can do settings from build settings as follows :

Build Settings - Swift Legacy Code
Build Settings – Swift Legacy Code

Default Setting is No (Swift 3). But if you want to do legacy code (Swift 2.3) then you have to select Yes (Swift 2.3).

Make sure you do this setting by starting of new project because many methods will change.

For Example :

Swift 3 Code

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    return true
}

Swift 2.3 Code

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
    return true
}

As per my opinion Swift 3 is recommended.

Happy Coding 🙂

Use of Blocks(Closures) or Completion Handlers with Function in Swift – iOS

Blocks in Objective-C

In Objective-C we are using the blocks(completion handlers) with functions as follows :

- (void)yourFunctionName:(NSString *)yourString withCompletionHandler:(void (^)(NSString *yourResult))block;

Closures in Swift

Closures are self-contained blocks of functionality that can be passed around and used in your code. Closures in Swift are similar to blocks in C and Objective-C and to lambdas in other programming languages.

Syntax with Function

func yourFunctionName(parameter:Type, ... , withCompletionHandler:(result:Type) -> Void)

For more closure syntax : goshdarnclosuresyntax.com

Example

Function Definition:

func closureReturn(isTest:Bool, withCompletionHandler:(result:String) -> Void) {
    if(isTest){
        withCompletionHandler(result: "Yes")
    }
    else{
        withCompletionHandler(result: "No")
    }
}

Calling Function:

closureReturn(true) { (result) -> Void in
    print(result)
}

Output should be respective to value true/false.


In next post I will write a tutorial on a wrapper class for Alamofire with use of SwiftyJSON by using closures.

It will be related to this post How to use Alamofire and SwiftyJSON with Swift? – Swift 2 – iOS 9 – Xcode 7

Happy Coding 🙂

Work with Core Data in Swift Language

Lets take a brief idea about how to use core data with swift language.

Create a new project by check on Use Core Data.

Core Data Project Setting
Core Data Project Setting

It will create the basic methods of core data in to the AppDelegate.swift

There is core data model called {YOUR-PROJECT.xcdatamodeld} will be there with the project.

It will be look like as follows :

Core Data Model
Core Data Model

Continue reading

Use of constant (#define) in Swift Language – iOS

Swift Constant Test
Swift Constant Test

In Objective-C we are using a header file to create constant variables like as

// Objective-C
#define APP_ALERT_TITLE "Objective-C Constant"

Swift has new syntax to define the constant (#define)

// Swift
let APP_ALERT_TITLE = "Swift Constants"

Lets try

Create a swift file with the constants

import Foundation

class Constants {
    
    // MARK: List of Constants
    
    static let APP_ALERT_TITLE = "Swift Constants"
    static let SAMPLE_MESSAGE = "The alert is working !!"
}

Note : Here the MARK statement is also changed.

// Objective-C
#pragma mark -
#pragma mark List of Constants
// Swift
// MARK: List of Constants

Continue reading

How to use Alamofire and SwiftyJSON with Swift? – Swift 3 – iOS 10 – Xcode 8

Swift JSON
Swift JSON

Alamofire is an HTTP networking library written in Swift.
SwiftyJSON makes it easy to deal with JSON data in Swift.
Continue reading