Infonity Tech
Published in

Infonity Tech

SWIFTip#6: Casual insight of the week

Learning around swift.

Here is a weekly insight to enhance skills of Swift good practice. Do comment if you have the better suggestion for swift learners.

Thank you.


Array Instance Method

// The following code uses this method to test whether all
// the scores in an array is greater than 85:
let scores = [85, 88, 95, 92]
let passed = scores.allSatisfy { $0 >= 85 }
//OUTPUT: passed ==> Truelet names = ["Swift", "Junk", "box"]
let isMoreThanThree = names.allSatisfy({ $0.count >= 3})
//OUTPUT: isMoreThanThree ==> true

Difference between map(_:) and compactMap(_:)

// Use compactMap function to receive an array of nonoptional values
// when your transformation produces an optional value.
let possibleNumbers = ["1", "2", "three", "///4///", "5", "Fish"]
let mapped: [Int?] = { Int($0) }
// [1, 2, nil, nil, 5, nil]
let compactMapped: [Int] = possibleNumbers.compactMap { Int($0) }
// [1, 2, 5]

Which one is better for Constant? Struct or enum

The advantage of using a case-less enumeration is that it can’t accidentally be instantiated and works as a pure namespace.

Easy Keyboard dismiss

Dimiss Keyboard on Drag of any scrolling event

// UITableView
tableView.keyboardDismissMode = .onDrag //.interactive
CollectionView.keyboardDismissMode = .onDrag
scrlView.keyboardDismissMode = .onDrag

- What is Failable Initializers?

An initialization function (“initializer”), can be declared to be optional (init?). You must return nil if the initialization process fails.

- Can I initialise enum? How?

An enumeration (enum) does not require an explicit initializer, but enumerations can be extended to have custom initializers.

- is it mandatory to Initialze structure?

A structure (struct) is a group of values, so each value must be initialized before the structure can be accessed.





Recommended from Medium

The Zecrey Protocol Public Testnet is currently operational!

Modals Are The Worst

Create a complete Azure Function project in .NET 5 using VSCode

Blur face using open CV

CS371p Spring 2022: Christopher Carrasco — Week 5

I can help for the engineers who started writing in Medium and struggle to gain followers…

Achieve parallelism in Your own server with UDP protocol

A study for parallel computational models

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Darshit Shah

Darshit Shah

Sr. Software Developer, Passionate for App UI/UX, Blogger

More from Medium

2022 Week #1 — Working on Leadway Health

Releasing the release strategy for SaaS software development teams.

Why Building an Inclusive App Matters

Join Our Software Workshops for Polish Dev Community