Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.82 KB | None | 0 0
  1. //: Playground - noun: a place where people can play
  2. var str = "Hello, playground"
  3.  
  4. print(str)
  5.  
  6. // Inicio dos estudos de swift
  7.  
  8. var optionalString: String? = "Hello"
  9.  
  10. print(optionalString == nil)
  11.  
  12. var optionalName: String? = "Jota"
  13.  
  14. var greeting = "Hello"
  15.  
  16. if let name = optionalName{
  17. greeting = "Hello, \(name)"
  18. print(name)
  19. }
  20.  
  21. print(greeting)
  22.  
  23. let nickName: String? = "Jotinha"
  24.  
  25. let fullName: String = "Jota Freitas Jr"
  26.  
  27. let informalGreeting = "Hi \(nickName ?? fullName)"
  28.  
  29. print(informalGreeting)
  30.  
  31. // PG9
  32.  
  33. let vegetable = "red pepper"
  34.  
  35. switch vegetable {
  36. case "celery":
  37. print("Add some raisins and make ants on a log.")
  38. case "cucumber", "watercress":
  39. print("That would make a good tea sandwich.")
  40. case let x where x.hasSuffix("pepper"):
  41. print("It is a spicy \(x)?")
  42. default:
  43. print("Everything tastes good in this soup.")
  44. }
  45.  
  46. let interestingNumbers = [
  47. "Prime": [2, 3, 5, 7, 11, 13],
  48. "Fibonacci": [1, 1, 2, 3, 5, 8],
  49. "Square": [1, 4, 9, 16, 25]
  50. ]
  51.  
  52. var lagest = 0
  53.  
  54. for(kind, numbers) in interestingNumbers {
  55. for number in numbers {
  56. if number > lagest {
  57. lagest = number
  58. }
  59. }
  60. }
  61.  
  62. print("The lagest number is \(lagest)")
  63.  
  64. var n = 2
  65.  
  66. while n < 100 {
  67. n = n * 2
  68. }
  69.  
  70. print(n)
  71.  
  72. var m = 2
  73.  
  74. repeat {
  75. m = m * 2
  76. } while m < 100
  77.  
  78. print(m)
  79.  
  80.  
  81. var total = 0
  82.  
  83. for i in 0..<4 {
  84. total += 1
  85. }
  86.  
  87. print(total)
  88.  
  89.  
  90. for i in 0...4 {
  91. total += 1
  92. }
  93.  
  94. print(total)
  95.  
  96. // Functions
  97.  
  98. func greet(person: String, day: String) -> String {
  99. return "Hello \(person), today is \(day)."
  100. }
  101.  
  102. print(greet(person: "BoB", day: "Wednesday"))
  103.  
  104. func greet2(_ person: String, on day: String) -> String {
  105. return "Hello \(person), today is \(day)."
  106. }
  107.  
  108. print(greet2("Jota", on: "Quarta"))
  109.  
  110. func calculateStatistics(scores: [Int]) -> (min: Int, max: Int, sum: Int) {
  111. var min = scores[0]
  112. var max = scores[0]
  113. var sum = 0
  114.  
  115. for score in scores {
  116. if score > max {
  117. max = score
  118. } else if score < min {
  119. min = score
  120. }
  121.  
  122. sum += score
  123. }
  124.  
  125. return(min, max, sum)
  126.  
  127. }
  128.  
  129. let statistics = calculateStatistics(scores: [5, 3, 100, 3, 9])
  130.  
  131. print(statistics.sum)
  132.  
  133. print(statistics.2)
  134.  
  135. func sumOf(numbers: Int...) -> Int {
  136. var sum = 0
  137. for number in numbers {
  138. sum += number
  139. }
  140. return sum
  141. }
  142.  
  143. print(sumOf(numbers: 42, 32, 56, 79, 100))
  144.  
  145.  
  146. func returnFifteen() -> Int {
  147. var y = 10
  148. func add() {
  149. y += 5
  150. }
  151. add()
  152. return y
  153. }
  154.  
  155. print(returnFifteen())
  156.  
  157. // Funcoes sao um tipo, isso significa que elas podem retornar outra funcao, por exemplo:
  158. func makeIncrementer() -> ((Int) -> Int) {
  159. func addOne(number: Int) -> Int {
  160. return 1 + number
  161. }
  162. return addOne
  163. }
  164.  
  165. var increment = makeIncrementer()
  166.  
  167. print(increment(7))
  168.  
  169. // e funcoes podem tambem ser argumentos de funcoes
  170. func hasAnyMatches(list: [Int], condition: (Int) -> Bool) -> Bool {
  171.  
  172. for item in list {
  173.  
  174. if condition(item) {
  175. return true
  176. }
  177. }
  178. return false
  179. }
  180.  
  181.  
  182. func lessThanTen(number: Int) -> Bool {
  183. return number < 10
  184. }
  185.  
  186. var numbers = [20, 19, 7, 12]
  187.  
  188. print(hasAnyMatches(list: numbers, condition: lessThanTen))
  189.  
  190.  
  191. // classes
  192.  
  193. class Shape {
  194.  
  195. var numberOfSides = 0
  196.  
  197. func simpleDescription() -> String {
  198. return "A shape with \(numberOfSides) sides."
  199. }
  200. }
  201.  
  202. var shape = Shape()
  203.  
  204. shape.numberOfSides = 4
  205.  
  206. print(shape.simpleDescription())
  207.  
  208. class NamedShape {
  209.  
  210. var numberOfSides = 0
  211.  
  212. var name : String
  213.  
  214. init(name: String) {
  215. self.name = name
  216. }
  217.  
  218. func simpleDescription() -> String {
  219. return "A shape called \(name) with \(numberOfSides) sides."
  220. }
  221.  
  222. }
  223.  
  224. var namedShape = NamedShape(name: "Quadrado")
  225. namedShape.numberOfSides = 4
  226. print(namedShape.simpleDescription())
  227.  
  228. class Square : NamedShape {
  229.  
  230. var sideLength: Double
  231.  
  232. init(sidelength: Double, name: String) {
  233. self.sideLength = sidelength
  234. super.init(name: name)
  235. numberOfSides = 4
  236. }
  237.  
  238. func area() -> Double {
  239. return sideLength * sideLength
  240. }
  241.  
  242. override func simpleDescription() -> String {
  243. return "A square with sides of length \(sideLength)."
  244. }
  245.  
  246. }
  247.  
  248. let test = Square(sidelength: 5.2, name: "my test square")
  249.  
  250. test.area()
  251.  
  252. test.simpleDescription()
  253.  
  254. class EquilateralTriangle: NamedShape {
  255.  
  256. var sideLength: Double = 0
  257.  
  258. init(sideLength: Double, name: String) {
  259. self.sideLength = sideLength
  260. super.init(name: name)
  261. numberOfSides = 3
  262. }
  263.  
  264. var perimeter: Double {
  265. get {
  266. return 3.0 * sideLength
  267. }
  268. set {
  269. sideLength = newValue / 3.0
  270. }
  271. }
  272.  
  273. override func simpleDescription() -> String {
  274. return "A equilateral triangle with sides of length \(sideLength)"
  275. }
  276.  
  277. }
  278.  
  279. var triangle = EquilateralTriangle(sideLength: 3.1, name: "a triangle")
  280.  
  281. triangle.perimeter = 9
  282. print(triangle.sideLength)
  283. print(triangle.perimeter)
  284.  
  285. class TriangleAndSquare {
  286. var triangle: EquilateralTriangle {
  287. willSet {
  288. square.sideLength = newValue.sideLength
  289. }
  290. }
  291.  
  292. var square: Square {
  293. willSet {
  294. triangle.sideLength = newValue.sideLength
  295. }
  296. }
  297.  
  298. init(size: Double, name: String) {
  299. square = Square(sidelength: size, name: name)
  300. triangle = EquilateralTriangle(sideLength: size, name: name)
  301. }
  302. }
  303.  
  304.  
  305. var triangleAndSquare = TriangleAndSquare(size: 10, name: "another test shape")
  306. print(triangleAndSquare.square.sideLength)
  307. print(triangleAndSquare.triangle.sideLength)
  308. triangleAndSquare.square = Square(sidelength: 50, name: "large square")
  309. print(triangleAndSquare.triangle.sideLength)
  310.  
  311.  
  312. let optionalSquare: Square? = Square(sidelength: 12, name: "optional square")
  313. let side = optionalSquare?.sideLength
  314.  
  315. // Enums
  316. enum Rank: Int {
  317. case ace = 1
  318. case two, three, four, five, six, seven, eight, nine, ten
  319. case jack, queen, king
  320. func simpleDescription() -> String {
  321. switch self {
  322. case .ace:
  323. return "ace"
  324. case .jack:
  325. return "jack"
  326. case .queen:
  327. return "queen"
  328. case .king:
  329. return "king"
  330. default:
  331. return String(self.rawValue)
  332. }
  333. }
  334. }
  335.  
  336. let ace = Rank.ace
  337.  
  338. let aceRawValue = ace.rawValue
  339.  
  340. let three = Rank.eight.simpleDescription()
  341.  
  342. enum Suit {
  343. case spades, hearts, diamonds, clubs
  344. func simpleDescription() -> String {
  345. switch self {
  346. case .spades:
  347. return "spades"
  348. case .hearts:
  349. return "hearts"
  350. case .diamonds:
  351. return "diamonds"
  352. case .clubs:
  353. return "clubs"
  354. }
  355. }
  356. func color() -> String {
  357. switch self {
  358. case .spades, .clubs:
  359. return "black"
  360. case .diamonds, .hearts:
  361. return "red"
  362. }
  363. }
  364. }
  365.  
  366. let hearts = Suit.hearts.color()
  367. print(hearts)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement