UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Question: Is the method .filter only used in Array

Forums > 100 Days of SwiftUI

@boat  

Is the method .filter only used in Array, in order to return a new array on conditions ?

Or it is also useful in other data types ?

Thanks

Boat

2      

filter(_:) belongs to the Sequence protocol, which means anything that conforms to Sequence can use it.

That includes, but is not limited to: Dictionary, Range, Set and a whole host of various sequencey types.

import Foundation

let dict = ["Charlotte": true,
            "Shauna": true,
            "Mildred": true,
            "Linton": false,
            "Sonny": false,
            "Jack": false]
let mysteryGirls = dict.filter { $0.value }
print(mysteryGirls)

let range = 1...100
let evens = range.filter { $0.isMultiple(of: 2) }
print(evens)

let set: Set<Int> = [1,2,3,4,5,6,7,8,9]
let odds = set.filter { !$0.isMultiple(of: 2) }
print(odds)

3      

Hi Boat,

I not sure what types you want but the function loops over elements to see if they match if so returns that element.

Nigel

3      

@boat  

@roosterboy,

what is the .value mean in. let mysteryGirls = dict.filter { $0.value } ? does it mean the true/false of Boolean ?

Also, I tried to run your code (under import SwiftUi and import Foundation , like you did), but complier tells me "Experssions are not allowed at the top level" at the line of print(mysteryGirls).

Thanks in advance

Boat

2      

@boat asks:

what is does the .value mean in let mysteryGirls = dict.filter { $0.value }

Think about what a printed dictionary is. You have a book that has lots of words in it. Each word has a related definition.

For example if you looked up Boat in a dictionary, you'd see the definition

BOAT: A large hole in the water into which one pours money.

In Swift, a dictionary follows a similar concept. In @rooster's example, he created a dictionary named dict. (Pick a better name, svp!) Swift dictionaries follow a <Key, Value> design. In his example, the KEY is a person's name. The VALUE is boolean representing gender.

In a printed dictionary, the KEY is the word you're looking up, and the VALUE is the word's definition.

When you retrieve items from a Swift dictionary, you can examine the item's KEY, its VALUE, or both. In @rooster's example, he's going through each item in the dictionary, and performing a filter.

In Swift $0 is a placeholder for a dictionary entry. It contains BOTH the KEY and the VALUE. $0.value is the syntax to ask Swift to examine just the VALUE portion of the dictionary item passed to the filter.

Time for @boat to brush up on Swift dictionaries. Please review @twostraw's 60 second video:

Swift Dictionaries

3      

@boat reports:

Also, I tried to run your code (under import SwiftUi and import Foundation , like you did), but complier tells me "Experssions are not allowed at the top level" at the line of print(mysteryGirls).

This code works fine in Playground. However, you do not need to import SwiftUI because @rooster did not include any user interface code.

This makes me wonder if you are trying to run this in an Xcode project outside of a ContentView struct? We can't tell without a screen shot. Instead of troubleshooting, please just open a new playground and copy / paste his code into the Playground.

3      

@boat  

Thank you guys !

Very clear.

I'm so grateful my silly questions always got your help. I tried google sometimes, but answers can be from years back which I don't know if still applicable, or answers can be a rabbit hole giving me more confusion

yes now I understand $0.value is the [the person: the gender] in the .filter () context.

@obelix yes I ran @roosterboy's code again in Playground, and this time it worked.

I bought this mac this 2020-M1 Macbook this year, and it only has 8GB memeory. So I feel like walking on thin ice when running SwiftUI & Playground, and Simulator altogether , while the chrome is opened with 20 pages. ( I don't use safari much because it keeps refreshing pages and I lost the page I wanted to keep open)

Anyway have a great weekend everyone !

Boat

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.