iOS Swift Development: Extensions

iOS Swift Development: Extensions

memorize.aimemorize.ai (lvl 286)
Section 1

Preview this deck

Create an extension of double that calculates the area of a circle, alongside creating a class called circle that initializes the radius value. Use mutate to make to change the self value to the area value.

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

0

All-time users

0

Favorites

0

Last updated

6 years ago

Date created

Mar 1, 2020

Cards (9)

Section 1

(9 cards)

Create an extension of double that calculates the area of a circle, alongside creating a class called circle that initializes the radius value. Use mutate to make to change the self value to the area value.

Front

extension Double { mutating func calculateArea() { let pi = 3.1415 self = pi selfself } } class Circle { var radius: Double init(radius: Double) { self.radius = radius } } var circle = Circle(radius: 3.3) print(circle.radius) circle.radius.calculateArea() print(circle.radius)

Back

Create an extension of String that returns the string reversed

Front

extension String { func reverse() -> String { var characterArray = [Character]() for character in self.characters { characterArray.insert(character, at: 0) } return String(characterArray) } } var name = "Marty McFly" print(name.reverse())

Back

Create extension called SomeType

Front

extension SomeType { // new functionality to add to SomeType goes here }

Back

What does the keyword mutating in front of a function do?

Front

It allows the function to modify the value of which it is being called upon. So the value itself. In this case the self value. self = selfself3.14

Back

What are extensions?

Front

Extensions add new functionality to an existing class, structure, enumeration, or protocol type. This includes the ability to extend types for which you do not have access to the original source code (known as retroactive modeling).

Back

Create 3 buttons that implement 3 separate features called colorize, fade, and wiggle, which will be extensions of UIButton

Front

extension UIButton { func wiggle(){ let wiggleAnimation = CABasicAnimation(keyPath: "position") wiggleAnimation.duration = 0.05 wiggleAnimation.repeatCount = 5 wiggleAnimation.autoreverses = true wiggleAnimation.fromValue = CGPoint(x: self.center.x - 4, y: self.center.y) wiggleAnimation.toValue = CGPoint(x: self.center.x + 4, y: self.center.y) layer.add(wiggleAnimation, forKey: "position") } func fade(){ UIView.animate(withDuration: 0.5, animations: { self.alpha = 0.75 }) {(finished) in UIView.animate(withDuration: 0.15, animations: { self.alpha = 1.0 }) } } func colorize(){ let randomNumberArray = generateRandomNumbers(quantity: 3) let randomColor = UIColor(red: randomNumberArray[0]/255, green: randomNumberArray[1]/255, blue: randomNumberArray[2]/255, alpha: 1) UIView.animate(withDuration: 0.3) { self.backgroundColor = randomColor } } } //Seperate File import UIKit func generateRandomNumbers(quantity:Int)->[CGFloat] { var randomNumberArray = [CGFloat]() for _ in 1...quantity { let randomNumber = CGFloat(arc4random_uniform(255)) randomNumberArray.append(randomNumber) } return randomNumberArray }

Back

Convert an array of characters to a string?

Front

String(character: sequence) select from dropdown

Back

extend the extension with "SomeProtocol" and "Another Protocol"

Front

extension SomeType: SomeProtocol, AnotherProtocol { // implementation of protocol requirements goes here }

Back

Create an extension of Int that squares a number

Front

extension Int { func square() -> Int { return self*self } } var number = 5 print(number.square())

Back