I'm new in Swift and do not know everything , i have this kind of an array
var arrayOFJumpForwardBack: [Int: [String: [String]]] =
[1: ["chapter": ["15 sec","30 sec","45 sec","60 sec"]]]
and , i'd like to know how can i get certain elements from this array?
Swift - Arrays & Dictionaries
The syntax of an array is as follows:
["value1","value2","value3"]
The syntax of a dictionary is as follows:
["ABC":"valueForABC","XYZ":"valueForXYZ"]
In this case you're using a dictionary, and the main difference with the array is that the order doesn't matter, each value is represented with a key, so with the following example:
Example:
Let's say we have a dictionary where we have stored the word "apple" whose key is "JHC" and "pear" whose key is "IOP". We want to print "I have an apple", then:
var myFridge:[String:String] = ["JHC":"apple","IOP":"pear"]
print("I have an \(myFridge["JHC"]!)")
A multidimensional array is just an array inside of another, x dimensions you want.
I'd rather prever to create a dictionary as above ↑
Related
let var1 = "AnyCode".sorted()
print(var1.joined(separator:""))
ERROR: No exact matches in call to instance method 'joined'
I am trying to join the array after sorting the string. = "AnyCode"
I was expecting the output was = ACdenoy
But it is giving an error.
A Swift String is a collection of Characters, and sorted() applied to a collection returns an array with the collection elements in sorted order.
So var1 has the type [Character], and you can simply create a new string from that array with:
let var1 = "AnyCode".sorted()
print(String(var1)) // ACdenoy
Alternatively to Martin R's answer (but not better than that answer), you might have said
print(var1.map(String.init).joined())
...turning the characters to strings before trying to join the array elements.
I'm wondering about the reversed() method on a swift Array:
var items = ["a", "b", "c"]
items = items.reversed()
the signature of the reversed method from the Apple doc says that it returns a
ReversedRandomAccessCollection<Array<Element>>
could that be assigned back to items without doing what the apple doc say which is
For example, to get the reversed version of an array, initialize a new Array instance from the result of this reversed() method.
or would it give problem in the future? (since the compiler doesn't complain)
There are 3 overloads of reversed() for an Array in Swift 3:
Treating the Array as a RandomAccessCollection,func reversed() -> ReversedRandomAccessCollection<Self> (O(1))
Treating the Array as a BidirectionalCollection,func reversed() -> ReversedCollection<Self> (O(1))
Treating the Array as a Sequence,func reversed() -> [Self.Iterator.Element] (O(n))
By default, reversed() pick the RandomAccessCollection's overload and return a ReversedRandomAccessCollection. However, when you write
items = items.reversed()
you are forcing the RHS to return a type convertible to the LHS ([String]). Thus, only the 3rd overload that returns an array will be chosen.
That overload will copy the whole sequence (thus O(n)), so there is no problem overwriting the original array.
Instead of items = items.reversed(), which creates a copy of the array, reverse that and copy it back, you could reach the same effect using the mutating function items.reverse(), which does the reversion in-place without copying the array twice.
Let's say I have an array of type Record[] and I want to create an associative array from it, key being rec.key. Is there an easy way to do it?
Yes, you can use std.array, std.typecons and std.algorithm libraries and construct this one-liner:
Record[Key] assocArray = array.map!( item => tuple( item.key, item ) ).assocArray;
It takes array, maps it to a tuple (Key, Record) and then takes that list of tuples and creates an associative array from it.
I am trying to create a fixed size 3 dimensional array in Swift with datatype of string like this:
var 3dArray:[[[String]]] = [[[50]],[[50]],[[50]]]
But it is giving error:
Cannot convert value of type 'Int' to expected element type string
From your question title you are trying to create 3d array with datatype [[[String]]] and in your question detail you are assigning it an Int value which is not possible in swift. That's why you are getting an error. Because your 3dArray object is expecting a String values
Either you can assign String values this way:
var yourArr: [[[String]]] = [[["50"]],[["50"]],[["50"]]]
Or if you want to make Int array you can do it this way:
var yourArr: [[[Int]]] = [[[50]],[[50]],[[50]]]
Note:
As #Martin R suggested variable names cannot start with a digit.
There is no typical way of creating multidimensional arrays in Swift.
But, you can create an array which will contain only String arrays and then append 3 different String arrays. Please find the code below:
var array = [[String]]()
array.append([String]())
array.append([String]())
array.append([String]())
At the end your array will be looking this way:
[[], [], []]
Or you can just create same array this way:
var array:[[String]] = [["50"],["50"],["50"]]
But keep in mind, "50" are strings which are content of those arrays and not their size.
If you want to have a fixed-size array, then you have to use some third party solutions like this one.
SQL1.ExecNonQuery2("INSERT INTO table1 VALUES(?,?,?)",Array As Object("def",3,4))
I don't seem to understand why the argument list in the above statement is declared in the form of Array as Object('xx','xx''xx').How is it exactly being converted into a list parameter ?
Array As xxx is a shorthand syntax for declaring a new array and assigning the values.
Array As Object("def", 3, 4)
Is equivalent to:
Dim arr As Object(3)
arr(0) = "def" : arr(1) = 3 : arr(1) = 4
Basic4android automatically wraps arrays as lists when needed. The items are not copied, it is the whole array that is wrapped in a list. Therefore the above code is valid as it creates an array which is then wrapped as a List.