Swift Foundation Guide

Use Swift Foundation on iOS and Android with the exact same Swift code

Introduction

Using the Swift Foundation on Android is easy.

Just use the exact same code as you do on iOS.

๐Ÿ‘

UgFoundation Examples

Please find the example code here https://github.com/scadedoc/UgExamples/tree/master/UgSwiftFoundationDemo

SCADE translated the entire Swift Foundation framework to the Android ecosystem and you can use it the exact same way as you use it on iOS. Here are are few samples to get you started.

For more information on our journey of building Swift Foundation for Android, see the medium article here https://medium.com/@SCADE/swift-foundation-4-0-for-android-b6274fb9c322

Swift Foundation Examples

Use exactly the same code on iOS and Android. There are no differences in code. Sometimes, there are minor changes in how it works on the different platforms.

Making an HTTP request

Again, the code is 100% the same for iOS and Android:

class HttpClientSample {

	let plainTextData = "SCADE plain text data"
	let jsonData = 
		"""
			{
			  "SimpleData" : [
			    1,2,3]
			}
		"""
		
	func postSample() -> Bool {

		// define target
		let url = URL(string: "https://httpbin.org/post")! 

	   //create the session object
	   let session = URLSession.shared

	   //now create the URLRequest object using the url object
	   var request = URLRequest(url: url)
	   
	   // set to post - don't know why apple guys didn't use constants
	   request.httpMethod = "POST"
	   request.httpBody = plainTextData.data(using: .utf8) 
	
	   // create task
	   let task = session.dataTask(with:request,completionHandler: { data, response, error in
		   print(response!)
		   print("executed task successfully")
		   
		   let str = String(data: data!, encoding: .utf8)
		   print(str!)
	   })
	   // Resume task to execute it
	   task.resume()
	   
	   return true
		
	}	
}

Using Date

Again, the code is 100% the same for iOS and Android:

import Foundation

class DateDemo {

 func testDistantFuture() -> Bool {
  // check working of distanceFuture
  let d = Date.distantFuture
  let now = Date()
  return d > now
 }
}

Other examples

Swift Foundation examples can be found in the official Apple Repository here https://github.com/apple/swift-corelibs-foundation/tree/master/TestFoundation

If you like to have examples added, please let us know.