0

and good job , well basically i have array like this :

let imageArray = [ ["image0"] , ["image11","image12"] , ["image2"], ["image31","image32","image33"] ]

In this point i want to put for example first item of each nested array into a new array like this :

var newArray = ["image0","image11","image2","image31"] 

And also i want to have a condition for example if "image31" clicked we have a new page that show ervery images of that first array show us, like ["image31", "image32", "image33"]

So could you tell any idea how can implement like this?

2 Answers 2

1

You can try

let res = imageArray.map { $0.first! }

When clicked use the index to access other elements , say from above index 3 image is clicked then use

let resImages = imageArray[clickedIndex] 

Edit:

let arr = ["images/product/2021-05-02T09-47-17.699Z-download (2).jpg"]

let res = arr.map { $0[$0.range(of: "images/product/")!.upperBound...] }
        
print(res)
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for attention, i updated my question , in second part , i want this if user clicked each item of newArray then in new page show each image array that related to clicked image
set imageView tag = index of the image you set it to from above res then use that tag of the clicked imageView to get resImages from above
Ok i will try that , again thanks and good luck
I have a problem with that, when I use your solution , I mean first line , and my string is "images/product/2021-05-02T09-47-17.699Z-download (2).jpg" its just return to me first character of this string not all of the string, what do I do?
Do u need 2021-05-02T09-47-17.699Z-download (2).jpg ?
|
1

Using simple data structures (like arrays) to model complex data is rarely the best approach.

In this case I would create a struct ImageList and then create an array of this struct

struct ImageList {
   let images: [String]
   var firstImage: String? {
       return self.images.first
   }
}

You can use map to create the array from your current source, if required

   imageListArray = imageArray.map { ImageList(images:$0) }

Now you can have an index that is associated with an ImageList struct - imageListArray[i].firstImage! is the value you want for your list and iageListArray[i].images is the array you want for your second requirement.

2 Comments

hi brother could you say what is imageListArray? because its unknown for me
It would be var imageListArray[ImageList] - you haven't said what you are doing with the array, but this would be the array for your tableview or lost view or whatever.

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.