I Have multiple arrays and I want to choose one to be my main array. I want the main Array to be described as a String. How can I name the new array by using my StationID String and my DirectionID String?
let Farbhof1 = ["hi","some","Strings"]
var StationID = "Farbhof"
var DirectionID = "1"
DestViewController.TableViewArray = "\(StationID)\(DirectionID)"
You cannot do that in Swift. Variable names need to be known at compile time, they cannot be calculated at runtime.
Related
I have a small problem. I created a large array.
It looks like this:
var Array = [
["text10", "text11", ["text01", "text02"]],
["text20", "text21", ["text11", "text12"]]
]
If we write this way: Array[0] that shows all the elements.
If we write this way: Array[0][0] that shows "text1".
If we write this way: Array[0][2] that shows
-2 elements
-- 0: "text01"
-- 1: "text02"
.
If we write this way: Array[0][2].count or Array[0][2][0] it will not work
How do I choose each item, I need these elements for the tableView
The problem basically is that your inner array is illegal. Swift arrays must consist of elements of a single type. You have two types of element, String and Array Of String. Swift tries to compensate but the result is that double indexing can’t work, not least because there is no way to know whether a particular element will have a String or an Array in it.
The solution is to rearchitect completely. If your array entries all consist of the same pattern String plus String plus Array of String, then the pattern tells you what to do; that should be a custom struct, not an array at all.
as #matt already answered but I want to add this thing
Why Array[0][2].count or Array[0][2][0] not work
If you Define array
var array = [
["text10", "text11", ["text01", "text02"]],
["text20", "text21", ["text11", "text12"]]
]
And when you type array you can see it's type is [[Any]] However it contains String as well as Array
So When you try to get Array[0][2] Swift does not know that your array at position 2 has another array which can have count
EDIT
What you are asking now is Array of dictionary I suggest you to go with model i.e create struct or class and use it instead of dictionary
Now If you want to create dictionary then
var arrOfDict = ["text10" : ["text01", "text02"] , "text11" : ["text11", "text12"]]
And you can access with key name let arrayatZero = arrOfDict["text10"] as? [String]
So I have this problem where I can't convert an element of my array to a string
I have a string like this
var description:[String] =["blue","yellow","red"]
and I want to give one element of my array to another variable which is chosen by another integer like this
var pick:[Int] = 2
var chosen:[String] = description[pick]
it says Cannot assign value of type 'String' to type '[String]' and to fix it xcode suggests to do it like this
var chosen:[String] = [description[pick]]
now if I want to cast this variable to another one or give it to a function or whatever it will say Cannot assign value of type 'String' to type '[[String]]' please help.
You are getting very confused here...
First...
var array = ["red", "yellow"]
Is an array of strings. Don't call it description. Call things what they are.
Second...
var pick: [Int]
Is declaring an array. Setting it = 2 doesn't make sense.
Change your last line to...
var chosen: String = array[pick]
In the above line using [String] here is telling the system that you are getting an array of Strings. You're not. You're getting a String here.
Is required for variable chosen to be an array?
Otherwise you could just:
var chosen: String = description[pick]
In a nutshell,
I create an object consisting of an Integer and an Array of Strings.
I put something into both the integer and the array
I put the object into a dictionary
I get a new reference to the object and attempt to append to the array. This is where it fails
//: Playground - noun: a place where people can play
class IdAndArray {
var id: Int = 0
var stringArray:[String] = [String]()
}
var dict:[Int:IdAndArray] = [Int:IdAndArray]()
var testRec = IdAndArray()
testRec.id = 1
testRec.stringArray.append("1 line")
dict[56] = testRec
var ref = (dict[56]?.stringArray)!
print(ref.count) // 1
ref.append("2 line")
var anotherRef = (dict[56]?.stringArray)!
print(anotherRef.count) // 1 instead of 2???
I suspect this may be related to this:
Implementing a multimap in Swift with Arrays and Dictionaries
I also suspect it is something to do with optionals but I am forcing the unwrap with the !
For the record, I'm coming from a java background.
Swift arrays and dictionaries are value types. When you assign one that is in a variable to another variable, you are making a copy of the original.
So when you do this:
var ref = (dict[56]?.stringArray)!
ref is an entirely new copy of the array and not the one that is in the dictionary, so modifying it has no effect on the original copy in the dictionary.
If instead you had done:
dict[56]?.stringArray.append("2 line")
then you would have modified the copy that is in the dictionary.
Note: In reality, Swift doesn't make a copy of the array until you modify one of the copies. This is done behind the scenes to avoid unnecessary copying and to keep things quick. The copy logically happens immediately when you assign it, but you wouldn't notice the difference until you start modifying one of the copies, so Swift delays the copy until it matters.
Now consider this change to your code:
var ref = dict[56]!
print(ref.stringArray.count) // 1
ref.stringArray.append("2 line")
var anotherRef = dict[56]!
print(anotherRef.stringArray.count) // 2 this time!!!
Here, ref points to an instance of a class which is a reference type. In this case, both ref and anotherRef point to the same object, and thus you are modifying the array in the dictionary this time.
I want to give a number to a variable, such that name of that variable is the ith component of array with variable names. Below is the
code I want to run, but I can't.
varname = {'test1','test2'};
rangenameraw = {'A','B','C'};
rangenamecolstart = {'1','11'};
rangenameend = {'10', '20'};
for i = 1:1:length(varname)
varname(i) =xlsread(filename, strcat(rangenameraw,rangenamecolstart,':'rangenameraw,rangenameend);
end
As far as I understand you want to create variables dynamically which can be achieved using the assignin function of MATLAB.
for i = 1:1:length(varname)
assignin('base', varname{i}, xlsread(filename, strcat(rangenameraw,rangenamecolstart,':'rangenameraw,rangenameend));
end
I want to create an Array, if i make it like this it works:
var arrayEingabe = Array(count:30, repeatedValue:0)
If i make it like this it does not work:
var sizeArray = 30
var arrayEingabe = Array(count:sizeArray, repeatedValue:0)
At the end i want to change the size of my Array depending on what the user typed in.
I was searching the web for one hour now, but i could not find the answer.
Thanks for your help guys
Greets
Kove
Actually both your examples compiled OK for me, but you should be more specific about types. Something like:
var arrayCount:Int = 30
var arrayEingabe = Array(count:arrayCount, repeatedValue:Int())
actually this might be better for you:
var arrayEingabe = [Int]()
This creates an empty array, and as mentioned in the comments Swift arrays are mutable. You can add, replace and delete members as you want.
On Swift 3.0.2 :-
Use Array initializer method give below:-
override init(){
let array = Array(repeating:-1, count:6)
}
Here, repeating :- a default value for Array.
count :- array count.