1
0
mirror of https://github.com/sharkdp/bat.git synced 2025-09-01 19:02:22 +01:00

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

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() { 
  print(A new instance of this class Person is created.) 
init() {   } 
 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
}  let personObj = Person()
// We can now create an instance of class Person - an object - by putting parentheses after the class name // Once an instance of Person class is created we can access its properties using the dot . syntax.
 print(This person age is \(personObj.age))
let personObj = Person()
// Once an instance of Person class is created we can access its properties using the dot . syntax. import Foundation
 class Friend : Comparable {
print(This person age is \(personObj.age))  let name : String
  let age : Int
import Foundation  
class Friend : Comparable {  init(name : String, age: Int) {
 let name : String  self.name = name
 let age : Int  self.age = age
   }
 init(name : String, age: Int) { }
 self.name = name func < (lhs: Friend, rhs: Friend) -> Bool {
 self.age = age  return lhs.age < rhs.age }; func > (lhs: Friend, rhs: Friend) -> Bool {
 }  return lhs.age > rhs.age
} }
func < (lhs: Friend, rhs: Friend) -> Bool { func == (lhs: Friend, rhs: Friend) -> Bool {
 return lhs.age < rhs.age } func > (lhs: Friend, rhs: Friend) -> Bool {  var returnValue = false
 return lhs.age > rhs.age  if (lhs.name == rhs.name) && (lhs.age == rhs.age)
}  {
func == (lhs: Friend, rhs: Friend) -> Bool {  returnValue = true
 var returnValue = false  }
 if (lhs.name == rhs.name) && (lhs.age == rhs.age)  return returnValue
 { }
 returnValue = true
 }  let friend1 = Friend(name: "Sergey", age: 35)
 return returnValue  let friend2 = Friend(name: "Sergey", age: 30)
}  
  print("Compare Friend object. Same person? (friend1 == friend2)")
 let friend1 = Friend(name: "Sergey", age: 35)
 let friend2 = Friend(name: "Sergey", age: 30) func sayHelloWorld() {
   print("Hello World")
 print("\Compare Friend object. Same person? (friend1 == friend2)") }
 // Call function
func sayHelloWorld() { sayHelloWorld()
 print("Hello World")
} func printOutFriendNames(names: String...) {
// Call function  
  for name in names {
sayHelloWorld()  
  print(name)
func printOutFriendNames(names: String...) {  }
   
 for name in names { }
  // Call the printOutFriendNames with two parameters
 print(name) printOutFriendNames("Sergey", "Bill")
 } // Call the function with more parameters
  printOutFriendNames("Sergey", "Bill", "Max")
}
// Call the printOutFriendNames with two parameters let simpleClosure = {
  print("From a simpleClosure")
printOutFriendNames("Sergey", "Bill") }
// Call the function with more parameters // Call closure
 simpleClosure() 
printOutFriendNames("Sergey", "Bill", "Max")
 let fullName = { (firstName:String, lastName:String)->String in
let simpleClosure = {  return firstName + " " + lastName
 print("From a simpleClosure") }
} // Call Closure
// Call closure let myFullName = fullName("Sergey", "Kargopolov")
 print("My full name is \(myFullName)")
simpleClosure() 
 let myDictionary = [String:String]()
let fullName = { (firstName:String, lastName:String)->String in // Another way to create an empty dictionary
 return firstName + " " + lastName let myDictionary2:[String:String] = [:]
} // Keys in dictionary can also be of type Int
// Call Closure let myDictionary3 = [Int:String]()

let myFullName = fullName("Sergey", "Kargopolov") var myDictionary = ["first_name": "Sergey", "last_name": "Kargopolov"]
print("My full name is \(myFullName)") // print to preview
 print(myDictionary)
let myDictionary = [String:String]() // Add a new key with a value
// Another way to create an empty dictionary myDictionary["user_id"] = "f5h7ru0tJurY8f7g5s6fd"
 // We should now have 3 key value pairs printed
let myDictionary2:[String:String] = [:] print(myDictionary)
// Keys in dictionary can also be of type Int
 var myDictionary = ["first_name": "Sergey", "last_name": "Kargopolov"]
let myDictionary3 = [Int:String]() // Loop through dictionary keys and print values
 for (key,value) in myDictionary {
var myDictionary = ["first_name": "Sergey", "last_name": "Kargopolov"]  print("\(key) = \(value)")
// print to preview }
  class Friend {
print(myDictionary)  let name : String
// Add a new key with a value  let age : Int
  
myDictionary["user_id"] = "f5h7ru0tJurY8f7g5s6fd"  init(name : String, age: Int) {
// We should now have 3 key value pairs printed  self.name = name
  self.age = age
print(myDictionary)  }
 }
var myDictionary = ["first_name": "Sergey", "last_name": "Kargopolov"]
// Loop through dictionary keys and print values  var friends:[Friend] = []
  
for (key,value) in myDictionary {  let friend1 = Friend(name: "Sergey", age: 30)
 print("\(key) = \(value)")  let friend2 = Friend(name: "Bill", age: 35)
}  let friend3 = Friend(name: "Michael", age: 21)
 class Friend {  
 let name : String  friends.append(friend1)
 let age : Int  friends.append(friend2)
   friends.append(friend3)
 init(name : String, age: Int) {  
 self.name = name  printFriends(friends: friends)
 self.age = age  
 }  // Get sorted array in descending order (largest to the smallest number)
}  let sortedFriends = friends.sorted(by: { $0.age > $1.age })
  printFriends(friends: sortedFriends)
 var friends:[Friend] = []  
   // Get sorted array in ascending order (smallest to the largest number)
 let friend1 = Friend(name: "Sergey", age: 30)  let sortedFriendsAscendingOrder = friends.sorted(by: { $0.age < $1.age })
 let friend2 = Friend(name: "Bill", age: 35)  printFriends(friends: sortedFriendsAscendingOrder)
 let friend3 = Friend(name: "Michael", age: 21)
 
 friends.append(friend1)  func printFriends(friends: [Friend])
 friends.append(friend2)  {
 friends.append(friend3)  for friendEntry in friends {
   print("Name: \(friendEntry.name), age: \(friendEntry.age)")
 printFriends(friends: friends)  }
   }
 // Get sorted array in descending order (largest to the smallest number)
 import UIKit
 let sortedFriends = friends.sorted(by: { $0.age > $1.age }) class ViewController: UIViewController {
 printFriends(friends: sortedFriends) override func viewDidLoad() {
   super.viewDidLoad()
 // Get sorted array in ascending order (smallest to the largest number) }
 override func viewWillAppear(_ animated: Bool) {
 let sortedFriendsAscendingOrder = friends.sorted(by: { $0.age < $1.age })  super.viewWillAppear(animated)
 printFriends(friends: sortedFriendsAscendingOrder)  
  // Create destination URL 
  let documentsUrl:URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first as URL!
 func printFriends(friends: [Friend])  let destinationFileUrl = documentsUrl.appendingPathComponent("downloadedFile.jpg")
 {  
 for friendEntry in friends {  //Create URL to the source file you want to download
 print("Name: \(friendEntry.name), age: \(friendEntry.age)")  let fileURL = URL(string: "https://s3.amazonaws.com/learn-swift/IMG_0001.JPG")
 }  
 }  let sessionConfig = URLSessionConfiguration.default
  let session = URLSession(configuration: sessionConfig)
import UIKit  
class ViewController: UIViewController {  let request = URLRequest(url:fileURL!)
override func viewDidLoad() {  
 super.viewDidLoad()  let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in
}  if let tempLocalUrl = tempLocalUrl, error == nil {
override func viewWillAppear(_ animated: Bool) {  // Success
 super.viewWillAppear(animated)  if let statusCode = (response as? HTTPURLResponse)?.statusCode {
   print("Successfully downloaded. Status code: \(statusCode)")
 // Create destination URL   }
  
 let documentsUrl:URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first as URL!  do {
 let destinationFileUrl = documentsUrl.appendingPathComponent("downloadedFile.jpg")  try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)
   } catch (let writeError) {
 //Create URL to the source file you want to download  print("Error creating a file \(destinationFileUrl) : \(writeError)")
  }
 let fileURL = URL(string: "https://s3.amazonaws.com/learn-swift/IMG_0001.JPG")  
   } else {
 let sessionConfig = URLSessionConfiguration.default  print("Error took place while downloading a file. Error description: %@", error?.localizedDescription);
 let session = URLSession(configuration: sessionConfig)  }
   }
 let request = URLRequest(url:fileURL!)  task.resume()
   
 let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in  }
 if let tempLocalUrl = tempLocalUrl, error == nil { }
 // Success
  do {
 if let statusCode = (response as? HTTPURLResponse)?.statusCode {  
 print("Successfully downloaded. Status code: \(statusCode)")  // Convert JSON Object received from server side into Swift NSArray.
 }  // Note the use "try"
   if let convertedJsonIntoArray = try JSONSerialization.JSONObjectWithData(data!, options: []) as? NSArray {
 do {  }
 try FileManager.default.copyItem(at: tempLocalUrl, to: destinationFileUrl)  
 } catch (let writeError) {  } catch let error as NSError {
 print("Error creating a file \(destinationFileUrl) : \(writeError)")  print(error.localizedDescription)
 }  }
 
 } else { DispatchQueue.global(qos: .userInitiated).async {
 print("Error took place while downloading a file. Error description: %@", error?.localizedDescription);  // Do some time consuming task in this background thread
 }  // Mobile app will remain to be responsive to user actions
 }  
 task.resume()  print("Performing time consuming task in this background thread")
   
 }  DispatchQueue.main.async {
}  // Task consuming task has completed
  // Update UI from this block of code
 do {  print("Time consuming task has completed. From here we are allowed to update user interface.")
   }
 // Convert JSON Object received from server side into Swift NSArray.  }

 // Note the use "try" import UIKit
 class ViewController: UIViewController {
 if let convertedJsonIntoArray = try JSONSerialization.JSONObjectWithData(data!, options: []) as? NSArray {  
 }  override func viewDidLoad() {
   super.viewDidLoad()
 } catch let error as NSError {  // Do any additional setup after loading the view, typically from a nib.
 print(error.localizedDescription)  
 }  let button = UIButton(type: UIButtonType.system) as UIButton
  
DispatchQueue.global(qos: .userInitiated).async {  let xPostion:CGFloat = 50
 // Do some time consuming task in this background thread  let yPostion:CGFloat = 100
  let buttonWidth:CGFloat = 150
 // Mobile app will remain to be responsive to user actions  let buttonHeight:CGFloat = 45
  
   button.frame = CGRect(x:xPostion, y:yPostion, width:buttonWidth, height:buttonHeight)
 print("Performing time consuming task in this background thread")  
   button.backgroundColor = UIColor.lightGray
 DispatchQueue.main.async {  button.setTitle("Tap me", for: UIControlState.normal)
 // Task consuming task has completed  button.tintColor = UIColor.black
  button.addTarget(self, action: #selector(ViewController.buttonAction(_:)), for: .touchUpInside)
 // Update UI from this block of code  
  self.view.addSubview(button)
 print("Time consuming task has completed. From here we are allowed to update user interface.")  }
 }  
 }  func buttonAction(_ sender:UIButton!)
  {
import UIKit  print("Button tapped")
class ViewController: UIViewController {  }
   
 override func viewDidLoad() {  override func didReceiveMemoryWarning() {
 super.viewDidLoad()  super.didReceiveMemoryWarning()
 // Do any additional setup after loading the view, typically from a nib.  // Dispose of any resources that can be recreated.
  }
   
 let button = UIButton(type: UIButtonType.system) as UIButton  
  }
 let xPostion:CGFloat = 50
 let yPostion:CGFloat = 100 import UIKit
 let buttonWidth:CGFloat = 150 class ViewController: UIViewController {
 let buttonHeight:CGFloat = 45  
   override func viewDidLoad() {
 button.frame = CGRect(x:xPostion, y:yPostion, width:buttonWidth, height:buttonHeight)  super.viewDidLoad()
   
 button.backgroundColor = UIColor.lightGray  //Create Activity Indicator
 button.setTitle("Tap me", for: UIControlState.normal)  let myActivityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)
 button.tintColor = UIColor.black  
 button.addTarget(self, action: #selector(ViewController.buttonAction(_:)), for: .touchUpInside)  // Position Activity Indicator in the center of the main view
   myActivityIndicator.center = view.center
 self.view.addSubview(button)  
 }  // If needed, you can prevent Acivity Indicator from hiding when stopAnimating() is called
   myActivityIndicator.hidesWhenStopped = false
 func buttonAction(_ sender:UIButton!)  
 {  // Start Activity Indicator
 print("Button tapped")  myActivityIndicator.startAnimating()
 }  
   // Call stopAnimating() when need to stop activity indicator
 override func didReceiveMemoryWarning() {  //myActivityIndicator.stopAnimating()
 super.didReceiveMemoryWarning()  
 // Dispose of any resources that can be recreated.  
  view.addSubview(myActivityIndicator)
 }  }
   
   override func didReceiveMemoryWarning() {
}  super.didReceiveMemoryWarning()
  }
import UIKit  
class ViewController: UIViewController { }
 
 override func viewDidLoad() {
 super.viewDidLoad()
 
 //Create Activity Indicator