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

SOLVED: Using .map function to transform [Int] to [String]

Forums > Swift

Dear Team,

Requesting your help here. Please see this rough, practice bit of code:

let a = [5,1,3,2,4]

var b = a.sorted()
b = b.filter{$0 % 2 != 0}
b = b.map{String($0)} //Error: Cannot convert value of type 'String' to closure result type 'Int'

print(b)

I'm trying to:

  1. Take an array of Ints -- called 'a'.
  2. Use sorted() and filter() -- through a var array called 'b', cuz I don't want to destroy the original.
  3. And then finally use the .map() -- to transform the array of Ints into an array of strings.

But the error I keep getting is: Cannot convert value of type 'String' to closure result type 'Int' -- Could you please help me understand why?

Looking forward to your replies.

Best, Aradroid.

PS: If you've followed Pauls 100DaysWithSwiftUI, you might probably guess what I am trying to do. But please ignore that; my objective here is to understand how to use the .map function here with a closure, where I am going wrong, and what the error message means. For context however, I have only gotten as far as Day 8.

2      

This line defines b as an array of integers, because a is an array of integers, just in a different order.

var b = a.sorted()

This line keeps b as an array of integers, just filtering some out.

b = b.filter{$0 % 2 != 0}

However, this line tries to redefine b from an array of integers to an array of strings. So you are trying to change the underlying type, which is not allowed.

You could write this

let c = b.map{String($0)}
print(c)

or this

let a = [5,1,3,2,4]
let b = a.sorted().filter({ !$0.isMultiple(of: 2) }).map( { String($0) } )
print(b)

And if you do not actually need b, this

let a = [5,1,3,2,4]
print(a.sorted().filter( { !$0.isMultiple(of: 2) } ).map( { String($0) } ) )

3      

Thank you for your response, @Greenamberred. I think I get what you're saying. Please correct me if I'm wrong:

If a 'var' or 'let' is already an array of Ints, you cannot use .map() to transform it into an array of strings and assign it back to the same 'var' or 'let'. The best you can do, is to use .map() to transform it into an array of strings, and assign it to a different 'var' or 'let'.

In other words, once a 'var' or 'let' datatype is set, .map() is not going to let you change it. "Find another 'var' or 'let'," it says, "This one's taken." —— Am I right?

Also, for my deeper understanding, imagine this:

  • You have a basket of normal eggs.
  • You use .map() to transform them into Fabergé eggs.
  • Now you want the cashier to put the Fabergé eggs back into the same basket you had the normal eggs in.
  • The cashier does a backflip and says, "No way! That basket is for normal eggs! Don't you have any sense? If you want to store Fabergé, you need a NEW basket. Extra charges apply."
  • So I cough up a new 'basket', and now I have a brand new basket, full of Fabergé.

Question: What happens to my original basket, and the normal eggs it contains?

Answer 1: Both the original basket and the normal eggs are still there (because the normal eggs were only used as a stencil by .map() to create entirely new, separete entities of Fabergé. The normal eggs were actually left untouched in its original basket).

Answer 2: Original basket is there, but it is now empty because every normal egg was tranformed into Fabergé by .map(), all of which were then put into a new basket meant only for Fabergé. So I don't have the normal eggs anymore, they're gone, but I can use the original basket to store new normal eggs (and nothing else), anytime I want.

Answer 3: Original basket is gone. Poof. All the normal eggs are gone. Poof. I'll never get them back, just like my youth. Poof.

Answer 4: None of the above. Please uninstall XCode.

Looking forward to your answers.

2      

In other words, once a 'var' or 'let' datatype is set, .map() is not going to let you change it.

Nothing will let you change it. Once a variable's type is set, it can't be changed. All you can do is create a new variable with a different type through some kind of transformation of the original variable.

Answer 1: Both the original basket and the normal eggs are still there (because the normal eggs were only used as a stencil by .map() to create entirely new, separete entities of Fabergé. The normal eggs were actually left untouched in its original basket).

This is the correct answer.

3      

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.