Fix line endings

This commit is contained in:
sharkdp 2020-10-09 22:55:16 +02:00 committed by David Peter
parent fdef133d3d
commit a81254ed2a
1 changed files with 268 additions and 268 deletions

View File

@ -1,268 +1,268 @@
class Person { class Person {
// We can define class property here  // We can define class property here
var age = 25 var age = 25
// Implement Class initializer. Initializers are called when a new object of this class is created  // Implement Class initializer. Initializers are called when a new object of this class is created
init() {  init() { 
 print(A new instance of this class Person is created.)   print(A new instance of this class Person is created.) 
 }   } 
}  } 
// We can now create an instance of class Person - an object - by putting parentheses after the class name  // We can now create an instance of class Person - an object - by putting parentheses after the class name
let personObj = Person() let personObj = Person()
// Once an instance of Person class is created we can access its properties using the dot . syntax.  // Once an instance of Person class is created we can access its properties using the dot . syntax.
print(This person age is \(personObj.age)) print(This person age is \(personObj.age))

import Foundation import Foundation
class Friend : Comparable { class Friend : Comparable {
 let name : String  let name : String
 let age : Int  let age : Int
   
 init(name : String, age: Int) {  init(name : String, age: Int) {
 self.name = name  self.name = name
 self.age = age  self.age = age
 }  }
} }
func < (lhs: Friend, rhs: Friend) -> Bool { func < (lhs: Friend, rhs: Friend) -> Bool {
 return lhs.age < rhs.age } func > (lhs: Friend, rhs: Friend) -> Bool {  return lhs.age < rhs.age }; func > (lhs: Friend, rhs: Friend) -> Bool {
 return lhs.age > rhs.age  return lhs.age > rhs.age
} }
func == (lhs: Friend, rhs: Friend) -> Bool { func == (lhs: Friend, rhs: Friend) -> Bool {
 var returnValue = false  var returnValue = false
 if (lhs.name == rhs.name) && (lhs.age == rhs.age)  if (lhs.name == rhs.name) && (lhs.age == rhs.age)
 {  {
 returnValue = true  returnValue = true
 }  }
 return returnValue  return returnValue
} }

 let friend1 = Friend(name: "Sergey", age: 35)  let friend1 = Friend(name: "Sergey", age: 35)
 let friend2 = Friend(name: "Sergey", age: 30)  let friend2 = Friend(name: "Sergey", age: 30)
   
 print("\Compare Friend object. Same person? (friend1 == friend2)")  print("Compare Friend object. Same person? (friend1 == friend2)")

func sayHelloWorld() { func sayHelloWorld() {
 print("Hello World")  print("Hello World")
} }
// Call function  // Call function
sayHelloWorld() sayHelloWorld()

func printOutFriendNames(names: String...) { func printOutFriendNames(names: String...) {
   
 for name in names {  for name in names {
   
 print(name)  print(name)
 }  }
   
} }
// Call the printOutFriendNames with two parameters  // Call the printOutFriendNames with two parameters
printOutFriendNames("Sergey", "Bill") printOutFriendNames("Sergey", "Bill")
// Call the function with more parameters  // Call the function with more parameters
printOutFriendNames("Sergey", "Bill", "Max") printOutFriendNames("Sergey", "Bill", "Max")

let simpleClosure = { let simpleClosure = {
 print("From a simpleClosure")  print("From a simpleClosure")
} }
// Call closure  // Call closure
simpleClosure()  simpleClosure() 

let fullName = { (firstName:String, lastName:String)->String in let fullName = { (firstName:String, lastName:String)->String in
 return firstName + " " + lastName  return firstName + " " + lastName
} }
// Call Closure  // Call Closure
let myFullName = fullName("Sergey", "Kargopolov") let myFullName = fullName("Sergey", "Kargopolov")
print("My full name is \(myFullName)") print("My full name is \(myFullName)")

let myDictionary = [String:String]() let myDictionary = [String:String]()
// Another way to create an empty dictionary  // Another way to create an empty dictionary
let myDictionary2:[String:String] = [:] let myDictionary2:[String:String] = [:]
// Keys in dictionary can also be of type Int  // Keys in dictionary can also be of type Int
let myDictionary3 = [Int:String]() let myDictionary3 = [Int:String]()

var myDictionary = ["first_name": "Sergey", "last_name": "Kargopolov"] var myDictionary = ["first_name": "Sergey", "last_name": "Kargopolov"]
// print to preview  // print to preview
print(myDictionary) print(myDictionary)
// Add a new key with a value  // Add a new key with a value
myDictionary["user_id"] = "f5h7ru0tJurY8f7g5s6fd" myDictionary["user_id"] = "f5h7ru0tJurY8f7g5s6fd"
// We should now have 3 key value pairs printed  // We should now have 3 key value pairs printed
print(myDictionary) print(myDictionary)

var myDictionary = ["first_name": "Sergey", "last_name": "Kargopolov"] var myDictionary = ["first_name": "Sergey", "last_name": "Kargopolov"]
// Loop through dictionary keys and print values  // Loop through dictionary keys and print values
for (key,value) in myDictionary { for (key,value) in myDictionary {
 print("\(key) = \(value)")  print("\(key) = \(value)")
} }
 class Friend {  class Friend {
 let name : String  let name : String
 let age : Int  let age : Int
   
 init(name : String, age: Int) {  init(name : String, age: Int) {
 self.name = name  self.name = name
 self.age = age  self.age = age
 }  }
} }

 var friends:[Friend] = []  var friends:[Friend] = []
   
 let friend1 = Friend(name: "Sergey", age: 30)  let friend1 = Friend(name: "Sergey", age: 30)
 let friend2 = Friend(name: "Bill", age: 35)  let friend2 = Friend(name: "Bill", age: 35)
 let friend3 = Friend(name: "Michael", age: 21)  let friend3 = Friend(name: "Michael", age: 21)
   
 friends.append(friend1)  friends.append(friend1)
 friends.append(friend2)  friends.append(friend2)
 friends.append(friend3)  friends.append(friend3)
   
 printFriends(friends: friends)  printFriends(friends: friends)
   
 // Get sorted array in descending order (largest to the smallest number)   // Get sorted array in descending order (largest to the smallest number)
 let sortedFriends = friends.sorted(by: { $0.age > $1.age })  let sortedFriends = friends.sorted(by: { $0.age > $1.age })
 printFriends(friends: sortedFriends)  printFriends(friends: sortedFriends)
   
 // Get sorted array in ascending order (smallest to the largest number)   // Get sorted array in ascending order (smallest to the largest number)
 let sortedFriendsAscendingOrder = friends.sorted(by: { $0.age < $1.age })  let sortedFriendsAscendingOrder = friends.sorted(by: { $0.age < $1.age })
 printFriends(friends: sortedFriendsAscendingOrder)  printFriends(friends: sortedFriendsAscendingOrder)


 func printFriends(friends: [Friend])  func printFriends(friends: [Friend])
 {  {
 for friendEntry in friends {  for friendEntry in friends {
 print("Name: \(friendEntry.name), age: \(friendEntry.age)")  print("Name: \(friendEntry.name), age: \(friendEntry.age)")
 }  }
 }  }

import UIKit import UIKit
class ViewController: UIViewController { class ViewController: UIViewController {
override func viewDidLoad() { override func viewDidLoad() {
 super.viewDidLoad()  super.viewDidLoad()
} }
override func viewWillAppear(_ animated: Bool) { override func viewWillAppear(_ animated: Bool) {
 super.viewWillAppear(animated)  super.viewWillAppear(animated)
   
 // Create destination URL    // Create destination URL 
 let documentsUrl:URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first as URL!  let documentsUrl:URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first as URL!
 let destinationFileUrl = documentsUrl.appendingPathComponent("downloadedFile.jpg")  let destinationFileUrl = documentsUrl.appendingPathComponent("downloadedFile.jpg")
   
 //Create URL to the source file you want to download   //Create URL to the source file you want to download
 let fileURL = URL(string: "https://s3.amazonaws.com/learn-swift/IMG_0001.JPG")  let fileURL = URL(string: "https://s3.amazonaws.com/learn-swift/IMG_0001.JPG")
   
 let sessionConfig = URLSessionConfiguration.default  let sessionConfig = URLSessionConfiguration.default
 let session = URLSession(configuration: sessionConfig)  let session = URLSession(configuration: sessionConfig)
   
 let request = URLRequest(url:fileURL!)  let request = URLRequest(url:fileURL!)
   
 let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in  let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
 if let tempLocalUrl = tempLocalUrl, error == nil {  if let tempLocalUrl = tempLocalUrl, error == nil {
 // Success   // Success
 if let statusCode = (response as? HTTPURLResponse)?.statusCode {  if let statusCode = (response as? HTTPURLResponse)?.statusCode {
 print("Successfully downloaded. Status code: \(statusCode)")  print("Successfully downloaded. Status code: \(statusCode)")
 }  }
   
 do {  do {
 try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)  try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)
 } catch (let writeError) {  } catch (let writeError) {
 print("Error creating a file \(destinationFileUrl) : \(writeError)")  print("Error creating a file \(destinationFileUrl) : \(writeError)")
 }  }
   
 } else {  } else {
 print("Error took place while downloading a file. Error description: %@", error?.localizedDescription);  print("Error took place while downloading a file. Error description: %@", error?.localizedDescription);
 }  }
 }  }
 task.resume()  task.resume()
   
 }  }
} }

 do {  do {
   
 // Convert JSON Object received from server side into Swift NSArray.   // Convert JSON Object received from server side into Swift NSArray.
 // Note the use "try"   // Note the use "try"
 if let convertedJsonIntoArray = try JSONSerialization.JSONObjectWithData(data!, options: []) as? NSArray {  if let convertedJsonIntoArray = try JSONSerialization.JSONObjectWithData(data!, options: []) as? NSArray {
 }  }
   
 } catch let error as NSError {  } catch let error as NSError {
 print(error.localizedDescription)  print(error.localizedDescription)
 }  }

DispatchQueue.global(qos: .userInitiated).async { DispatchQueue.global(qos: .userInitiated).async {
 // Do some time consuming task in this background thread   // Do some time consuming task in this background thread
 // Mobile app will remain to be responsive to user actions   // Mobile app will remain to be responsive to user actions
   
 print("Performing time consuming task in this background thread")  print("Performing time consuming task in this background thread")
   
 DispatchQueue.main.async {  DispatchQueue.main.async {
 // Task consuming task has completed   // Task consuming task has completed
 // Update UI from this block of code   // Update UI from this block of code
 print("Time consuming task has completed. From here we are allowed to update user interface.")  print("Time consuming task has completed. From here we are allowed to update user interface.")
 }  }
 }  }

import UIKit import UIKit
class ViewController: UIViewController { class ViewController: UIViewController {
   
 override func viewDidLoad() {  override func viewDidLoad() {
 super.viewDidLoad()  super.viewDidLoad()
 // Do any additional setup after loading the view, typically from a nib.   // Do any additional setup after loading the view, typically from a nib.
   
 let button = UIButton(type: UIButtonType.system) as UIButton  let button = UIButton(type: UIButtonType.system) as UIButton
   
 let xPostion:CGFloat = 50  let xPostion:CGFloat = 50
 let yPostion:CGFloat = 100  let yPostion:CGFloat = 100
 let buttonWidth:CGFloat = 150  let buttonWidth:CGFloat = 150
 let buttonHeight:CGFloat = 45  let buttonHeight:CGFloat = 45
   
 button.frame = CGRect(x:xPostion, y:yPostion, width:buttonWidth, height:buttonHeight)  button.frame = CGRect(x:xPostion, y:yPostion, width:buttonWidth, height:buttonHeight)
   
 button.backgroundColor = UIColor.lightGray  button.backgroundColor = UIColor.lightGray
 button.setTitle("Tap me", for: UIControlState.normal)  button.setTitle("Tap me", for: UIControlState.normal)
 button.tintColor = UIColor.black  button.tintColor = UIColor.black
 button.addTarget(self, action: #selector(ViewController.buttonAction(_:)), for: .touchUpInside)  button.addTarget(self, action: #selector(ViewController.buttonAction(_:)), for: .touchUpInside)
   
 self.view.addSubview(button)  self.view.addSubview(button)
 }  }
   
 func buttonAction(_ sender:UIButton!)  func buttonAction(_ sender:UIButton!)
 {  {
 print("Button tapped")  print("Button tapped")
 }  }
   
 override func didReceiveMemoryWarning() {  override func didReceiveMemoryWarning() {
 super.didReceiveMemoryWarning()  super.didReceiveMemoryWarning()
 // Dispose of any resources that can be recreated.   // Dispose of any resources that can be recreated.
 }  }
   
   
} }

import UIKit import UIKit
class ViewController: UIViewController { class ViewController: UIViewController {
   
 override func viewDidLoad() {  override func viewDidLoad() {
 super.viewDidLoad()  super.viewDidLoad()
   
 //Create Activity Indicator   //Create Activity Indicator
 let myActivityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)  let myActivityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
   
 // Position Activity Indicator in the center of the main view   // Position Activity Indicator in the center of the main view
 myActivityIndicator.center = view.center  myActivityIndicator.center = view.center
   
 // If needed, you can prevent Acivity Indicator from hiding when stopAnimating() is called   // If needed, you can prevent Acivity Indicator from hiding when stopAnimating() is called
 myActivityIndicator.hidesWhenStopped = false  myActivityIndicator.hidesWhenStopped = false
   
 // Start Activity Indicator   // Start Activity Indicator
 myActivityIndicator.startAnimating()  myActivityIndicator.startAnimating()
   
 // Call stopAnimating() when need to stop activity indicator   // Call stopAnimating() when need to stop activity indicator
 //myActivityIndicator.stopAnimating()   //myActivityIndicator.stopAnimating()
   
   
 view.addSubview(myActivityIndicator)  view.addSubview(myActivityIndicator)
 }  }
   
 override func didReceiveMemoryWarning() {  override func didReceiveMemoryWarning() {
 super.didReceiveMemoryWarning()  super.didReceiveMemoryWarning()
 }  }
   
} }