0

I have an array of string which to convert to an array of images.

var inviteStatus = [Any]()    
inviteStatus = ["accepted", "accepted", "pending", "pending"]

When the invite status is "accepted", I want it to be replaced with an image at that index. The desired result is this:

inviteStatus = [UIImage(named: "accepted.png"), UIImage(named: "accepted.png"), UIImage(named: "pending.png"),, UIImage(named: "pending.png")]

I tried with this following code but it's not working:

for (index, str) in self.arrayInviteStatus.enumerated() {
    self.arrayInviteStatus[index] = str.replacingOccurrences(of: "accepted", with: UIImage(name: "accepted.png"))
    self.arrayInviteStatus[index] = str.replacingOccurrences(of: "pending", with: UIImage(name: "pending"))
}

Thank you for your help.

2
  • Whats the problem with your current code? what does it do/not do? Commented Feb 22, 2018 at 11:19
  • @Scriptable Getting two errors : Value of type 'Any' has no member 'replacingOccurrences' and Use of unresolved identifier 'UIImage(name:)' Commented Feb 22, 2018 at 11:53

4 Answers 4

4

Use map

let inviteStatusImages = inviteStatus.map{ UIImage(named: $0+".png") }

You are discouraged from using [Any]. Better use two separate arrays with distinct types.

Sign up to request clarification or add additional context in comments.

Comments

1

I think map is definitely the way to go, but maybe consider using an enum, so you get a type safe way to distinguish between the two cases:

enum InviteStatus {
  case accepted
  case pending
}

Now you could have an Array of InviteStatus and combine map with pattern matching:

let inviteStatus: [InviteStatus] = [.accepted, .accepted, .pending, .pending]

func statusToImage(status: InviteStatus) -> UIImage {
  switch status {
    case let .accepted: return UIImage(named: "accepted.png")
    case let .pending: return UIImage(named: "pending.png")
  }
}

let imageArray = inviteStatus.map(statusToImage)

What this gives you is the ability to easily refactor without losing type safety. E.g. if you want to add another status case in the future like "declined", or something similar, you can add it to your enum and the switch-case pattern match will tell you at compile time that you'll have to add another case to it.

Comments

0

you can do like this

var inviteStatus: [String] = [] 
inviteStatus = ["accepted", "accepted", "pending", "pending"]
var imgArray: [UIImage] = []
imageArray = inviteStatus.map { UIImage(named: "\($0).png") }

2 Comments

Thanks for the response, how can i input a different image for "accepted" and "pending" using this code ?
include all images in your assests, you have to mention each image with different name
0

You can try to create an integrate array and then pass the variables to the original array

var inviteStatus = [Any]()
        inviteStatus = ["accepted", "accepted", "pending", "pending"]
        //inviteStatus = [UIImage(named: "accepted.png"), UIImage(named: "accepted.png"), UIImage(named: "pending.png"),, UIImage(named: "pending.png")]
        var newArray = [Any]()
        for invStat in inviteStatus {
            newArray.append(UIImage(named: "\(invStat).png"))
        }
        inviteStatus.removeAll()
        for na in newArray{
            inviteStatus.append(na)
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.