I have an array of type [[String]] and I am trying to move first 4 elements from the array to another array.
For example:
var arr1: [[String]] = [["1", "2", "3"], ["2", "3", "4"], ["3", "4", "5"], ["1", "2", "3"], ["2", "3", "4"], ["3", "4", "5"]]
var arr2: [[String]] = arr1[0...3]
But I am getting error saying that Cannot convert value of type 'Array<[String]>.SubSequence' (aka 'ArraySlice<Array<String>>') to specified type '[[String]]'
My required output is arr1 contains [["2", "3", "4"], ["3", "4", "5"]] and arr2 contains [["1", "2", "3"], ["2", "3", "4"], ["3", "4", "5"], ["1", "2", "3"]]
You have to (re)create an Array from the slice
let arr2 = Array(arr1[0...3])
Side note: It's not necessary to annotate types the compiler can infer.
In this case you could proceed without creating an array if the next step accepts a slice.
First initialize the array
var arr2: [[String]] = [[String]]()
Then you can add elements to your array using 'append'
arr2.append(contentsOf: arr1[0...3])
Related
This question already has answers here:
Swift equivalent to `[NSDictionary initWithObjects: forKeys:]`
(5 answers)
Closed 3 years ago.
I want to combine 2 array.
arr1 = ["a", "b", "c"]
arr2 = ["1", "2", "3"]
i want to make it to be :
"a" = "1"
"b" = "2"
"c" = "3"
so if i call value "1" into label1, it means value "a" is also called into label2 just like dictionary or index.
Just like that:
let arr1 = ["a", "b", "c"]
let arr2 = ["1", "2", "3"]
let dictionary = Dictionary(uniqueKeysWithValues: zip(arr1, arr2))
It's one line code
let dict = zip(["a", "b", "c"], ["1", "2", "3"]).compactMap{[$0.0:$0.1]}.reduce([:]) { $0.merging($1) { (current, _) in current } }
print(dict)
["a": "1", "b": "2", "c": "3"]
I suggests you to use user28434's answer which is more effective than mine. I am keeping my answer as alternative second best solution
var params = [[String:Any]]()
for i in 0 ..< QuesCount {
self.params.append([self.key1[i] : self.value1[i],
self.key2[i] : self.value2[i]])
}
Output:
self.params= [["answer[0][question_id]": "1", "answer[0][ans_id]": "21"], ["answer[1][question_id]": "2", "answer[1][ans_id]": "26"], ["answer[2][ans_id]": "36", "answer[2][question_id]": "5"], ["answer[3][ans_id]": "40", "answer[3][question_id]": "6"], ["answer[4][question_id]": "7", "answer[4][ans_id]": "41"], ["answer[5][question_id]": "8", "answer[5][ans_id]": "46"], ["answer[6][ans_id]": "49", "answer[6][question_id]": "9"], ["answer[7][question_id]": "10", "answer[7][ans_id]": "54"], ["answer[8][question_id]": "11", "answer[8][ans_id]": "57"]]
But the problem is that I want :
Desirable Output :
["answer[0][question_id]": "1", "answer[0][ans_id]": "21"], ["answer[1][question_id]": "2", "answer[1][ans_id]": "26"], ["answer[2][ans_id]": "36", "answer[2][question_id]": "5"], ["answer[3][ans_id]": "40", "answer[3][question_id]": "6"], ["answer[4][question_id]": "7", "answer[4][ans_id]": "41"], ["answer[5][question_id]": "8", "answer[5][ans_id]": "46"], ["answer[6][ans_id]": "49", "answer[6][question_id]": "9"], ["answer[7][question_id]": "10", "answer[7][ans_id]": "54"], ["answer[8][question_id]": "11", "answer[8][ans_id]": "57"]
How Can I obtain desirable Output??
I will not answer what you are asking as it not looks very convenient solution to me.
You should avoid using that complex structure of dictionary that will be very hard to manage in future.You can use struct or class as per your requirement that is easy and readable.
As I don't know your actual structure you are using so for just example
For example
struct Question {
var questionID:String?
var question:String?
var answersOption:[Options]?
var correctAnswer:Options?{
return self.answersOption.filter{$0.isCorrectAnswer == true}
}
}
struct Options {
var answerID:String?
var answer:String?
var isCorrectAnswer = false
}
Now you can create Array of questions and inside it you have options as well as correct answer; Cool right
Hope it is helpful
You can get all the values by this way -
for i in 0 ..< params.count
{
print(params[i] , terminator:",")
}
I want to match a string from the DicX to a an existing title (title of a table which changes according to the cell selection).
var DicX = ["xx",
"yy",
"zz",
"qq"]
let DicYY = [["11", "22", "33", "44"],
["1", "2", "3", "4"],
["m", "n", "k", "b"],
["bb", "kk", "mm", "nn"]]
the title I'm comparing with is like this:
title = detailX.insideTitle
so I want that when the title string equal to one of the DicX strings, to show the corresponding strings for it in DicYY each one of the 4 on a button.
but can't get the match correct, I tried to do like:
var currentX = detailX.insideTitle
if DicX == currentX["DicX"] {
}
I get this message :
Cannot subscript a value of type 'String' with an index of type 'String'
how can I do the if statement? and how to get the corresponding from DicYY?
This will do the job (if i got it right).
import Foundation
let DicX = ["xx",
"yy",
"zz",
"qq"]
let DicYY = [["11", "22", "33", "44"],
["1", "2", "3", "4"],
["m", "n", "k", "b"],
["bb", "kk", "mm", "nn"]]
let searchterm = "yy"
for (index, elem) in DicX.enumerated()
{
if (searchterm != elem) { continue }
print(DicYY[index]) // This will print ["1","2","3","4"]
}
I want to match a string from the DicX to a an existing title (title of a table which changes according to the cell selection).
var DicX = ["xx",
"yy",
"zz",
"qq"]
let DicYY = [["11", "22", "33", "44"],
["1", "2", "3", "4"],
["m", "n", "k", "b"],
["bb", "kk", "mm", "nn"]]
the title I'm comparing with is like this:
title = detailX.insideTitle
so I want that when the title string equal to one of the DicX strings, to show the corresponding strings for it in DicYY each one of the 4 on a button.
but can't get the match correct, I tried to do like:
var currentX = detailX.insideTitle
if DicX == currentX["DicX"] {
}
I get this message :
Cannot subscript a value of type 'String' with an index of type 'String'
how can I do the if statement? and how to get the corresponding from DicYY?
This will do the job (if i got it right).
import Foundation
let DicX = ["xx",
"yy",
"zz",
"qq"]
let DicYY = [["11", "22", "33", "44"],
["1", "2", "3", "4"],
["m", "n", "k", "b"],
["bb", "kk", "mm", "nn"]]
let searchterm = "yy"
for (index, elem) in DicX.enumerated()
{
if (searchterm != elem) { continue }
print(DicYY[index]) // This will print ["1","2","3","4"]
}
["[\"1\", \"7\", \"13\", \"19\", \"25\"]", "[\"6\", \"12\", \"13\"]"]
I am getting this as output how to get string array like this in Swift
["1", "7", "13", "19", "25", "6", "12", "13"]
I tried all to convert this array in proper array but no luck. Is there a solution for this ?
Thank You!!
Simple solution (without significant error handling), the strings can be treated as JSON
let array = ["[\"1\", \"7\", \"13\", \"19\", \"25\"]", "[\"6\", \"12\", \"13\"]"]
var result = [String]()
for item in array {
let jsonArray = try! JSONSerialization.jsonObject(with: item.data(using: .utf8)!) as! [String]
result.append(contentsOf: jsonArray)
}
print(result)
First, assuming you had a simple array of arrays, you can flatten it with flatMap:
let input = [["1", "7", "13", "19", "25"], ["6", "12", "13"]]
let output = input.flatMap { $0 }
That outputs
["1", "7", "13", "19", "25", "6", "12", "13"]
Or, even easier, just append the second array to the first one, bypassing the array of arrays altogether:
let array1 = ["1", "7", "13", "19", "25"]
let array2 = ["6", "12", "13"]
let output = array1 + array2
But your example looks like it's not an array of arrays, but rather array of descriptions of arrays, e.g. something like:
let array1 = ["1", "7", "13", "19", "25"]
let array2 = ["6", "12", "13"]
let input = ["\(array1)", "\(array2)"]
let output = ... // this is so complicated, I wouldn't bother trying it
Rather than figuring out how to reverse this array of interpolated strings, I'd suggest you revisit how you built that, bypassing interpolated strings (or description of the arrays).