Extract an array from a collection [duplicate] - arrays

This question already has answers here:
Get an array of property values from an object array
(2 answers)
Closed 6 years ago.
I get an element from a collection with this code:
modelCollec[indexPath].model
I want to get all models in an array. Is there a function or shall I write a loop?
There is only one row so indexPath is always [0, index].

Use flatMap for that.
let modelArray = modelCollec.flatMap { $0.model }
modelArray type is [model].
For eg:
struct Person {
var name: String
}
Now if you have array of person and you want array of name from it you can get it using flatMap like this way.
let persons = [Person]()
let nameArray = persons.flatMap { $0.name } //nameArray type is [String]
Note: You can also use map instead of flatMap but it will give you optional objects if your model property is optional so it may contains nil where as flatMap ignore the nil object.

If you always want to access the 1st element of the collection then it is a better idea to hardcode the 0 index rather than using loop, however to it is always better to put a check for nil.

Related

Get the index of the last occurrence of each string in an array

I have an array that is storing a large number of various names in string format. There can be duplicates.
let myArray = ["Jim","Tristan","Robert","Lexi","Michael","Robert","Jim"]
In this case I do NOT know what values will be in the array after grabbing the data from a parse server. So the data imported will be different every time. Just a list of random names.
Assuming I don't know all the strings in the array I need to find the index of the last occurrence of each string in the array.
Example:
If this is my array....
let myArray = ["john","john","blake","robert","john","blake"]
I want the last index of each occurrence so...
blake = 5
john = 4
robert = 3
What is the best way to do this in Swift?
Normally I would just make a variable for each item possibility in the array and then increment through the array and count the items but in this case there are thousands of items in the array and they are of unknown values.
Create an array with elements and their indices:
zip(myArray, myArray.indices)
then reduce into a dictionary where keys are array elements and values are indices:
let result = zip(myArray, myArray.indices).reduce(into: [:]) { dict, tuple in
dict[tuple.0] = tuple.1
}
(myArray.enumerated() returns offsets, not indices, but it would have worked here too instead of zip since Array has an Int zero-based indices)
EDIT: Dictionary(_:uniquingKeysWith:) approach (#Jessy's answer) is a cleaner way to do it
New Dev's answer is the way to go. Except, the standard library already has a solution that does that, so use that instead.
Dictionary(
["john", "john", "blake", "robert", "john", "blake"]
.enumerated()
.map { ($0.element, $0.offset) }
) { $1 }
Or if you've already got a collection elsewhere…
Dictionary(zip(collection, collection.indices)) { $1 }
Just for fun, the one-liner, and likely the shortest, solution (brevity over clarity, or was it the other way around? :P)
myArray.enumerated().reduce(into: [:]) { $0[$1.0] = $1.1 }

how to get array value by using index value of another value?

let nameArray = ["ramesh","suresh","rajesh"]
let idArray = ["100","101","102"]
Now i want value of "idArray" by using index value of "nameArray".
if nameArray index is 0. Output is 100
In Object Oriented Programming, objects should own their properties. So instead of having two data structures describe the same object, either use structs like Mr. Vadian has suggested, or have one array store all the properties of the objects:
let zippedArray = Array(zip(nameArray, idArray))
And now to get the object in a given index, you can use the following:
let index = 0
let element = zippedArray[0]
print(element.0) //ramesh
print(element.1) //100

Swift - if string is nil. don't add it to the array

I have an Array of Image links -
let alamofireSource = [AlamofireSource(urlString: Img1!)!, AlamofireSource(urlString: Img2!)!,
AlamofireSource(urlString: Img3!)!, AlamofireSource(urlString: Img4!)!]
slideshow.setImageInputs(alamofireSource)
some posts have only one image or two or three, and so on. so, sometimes image 2 (for example) is nil, In that case, I don't want it to be added to the array, is that possible?
You can try ( Swift 4 )
let arr = [img1,img2].compactMap{$0}.map{AlamofireSource(urlString:$0)!}
or
let arr = alamofireSource.compactMap{$0}
for Swift 3
let arr = alamofireSource.flatMap{$0}
so, sometimes image 2 (for example) is nil, In that case, I don't want
it to be added to the array, is that possible?
Yes it is. Although I would go with Sh_Khan's suggestion to use the compactMap method to achieve it, but it would be useless for your current case:
Based on your code snippet, I'd assume that alamofireSource of type [AlamofireSource], but not [AlamofireSource?] and that's because you are forcibly unwrap its elements (by adding ! to each of its elements). So far alamofireSource doesn't contain nils (actually it could be more danger than just a declaration, your app might crash!)
So first of all, I would recommend to remove the ! from alamofireSource:
let alamofireSource = [AlamofireSource(urlString: Img1!),
AlamofireSource(urlString: Img2!),
AlamofireSource(urlString: Img3!),
AlamofireSource(urlString: Img4!)]
which means let it be as [AlamofireSource?], therefore you would gain the benefit of using compactMap(_:):
Returns an array containing the non-nil results of calling the given
transformation with each element of this sequence.
As:
let alamofireSourceWihoutNils = alamofireSource.compactMap { $0 }
Assuming you put your Optional url strings into an array, say urlStrings (of type [String?]), you can construct alamofireSource according to (Swift 4):
let alamofireSource = urlStrings.compactMap { $0.map(AlamofireSource.init) }
Which make use of the map(_:) operator of Optional and compactMap(_:) to unwrap the two-level optionality.
Details
Your example contains two levels of optionality:
The optional ImgX arguments of type String? - henceforth referred to and named as img1, ..., img4, as CapitalFirstLetter names are reserved for e.g. types, not type instances.
The failable initilizer init?(urlString: String, placeholder: UIImage? = nil) of AlamofireSource.
First of all, lets gather the optional image links (imgX) into an array
let urlStrings = [url1, url2, url3, url4] // [String?]
Swift 4
You can combine the map(_:) operator of Optional with compactMap(_:) to safely unwrap and make use of the .some entires of urlStrings, thereafter collect the successful invocations of the failable initializer of AlamofireSource:
let alamofireSource = urlStrings.compactMap { $0.map(AlamofireSource.init) }
// or, use a named closure argument
let alamofireSource = urlStrings.compactMap { str in str.map(AlamofireSource.init) }
Swift 3
If using Swift 3, replace the compactMap(_:) invocation above with flatMap(_:):
let alamofireSource = urlStrings.flatMap { $0.map(AlamofireSource.init) }
// or, use a named closure argument
let alamofireSource = urlStrings.flatMap { str in str.map(AlamofireSource.init) }

How to append to newly initialized Array() in single expression?

I have a Dictionary from which I need to have all keys plus one in an Array.
I thought:
let keysArray = Array(dictionary.keys).append("OneMoreKey")
would work. But it results in: Cannot use mutating member on immutable value: function call returns immutable value.
What is the nicest way to do this?
You can append to the array of keys by doing:
let keysArray = Array(dictionary.keys) + ["OneMoreKey"]
The problem with append is that it is attempting to mutate the non-variable array of keys.

Create array with key from dict with sorted by dict values

I have an dictionary that I want to iterate trough in a tableview. One way of doing this is to create an array with all the keys and just fetch the key at the given index. But for this array I want the keys to be ordered alphabetically by the values in the dictionary. How do I do this?
static let dict = [
"0P0000KBNA": "Länsförsäkringar Europa Indexnära",
"0P0000YVZ3": "Länsförsäkringar Global Indexnära",
"0P0000J1JM": "Länsförsäkringar Sverige Indexnära",
"0P00013668": "Länsförsäkringar Tillväxtmarknad Indexnära A",
"0P0000K9E7": "Länsförsäkringar USA Indexnära",
"0P00005U1J": "Avanza Zero"
]
static let sortedKeys = Array(dict.keys) // How?
You just need to sort your dictionary by value and map the key
let sortedKeys = dict.sorted{$0.value < $1.value}.map{$0.key} // ["0P00005U1J", "0P0000KBNA", "0P0000YVZ3", "0P0000J1JM", "0P00013668", "0P0000K9E7"]
As a minor variation of #LeoDabus answer, you could access the keys property of your dictionary and sort this array according to the corresponding value for each key
let sortedKeys = dict.keys.sorted { dict[$0]! < dict[$1]! }
print(sortedKeys)
/* ["0P00005U1J",
"0P0000KBNA",
"0P0000YVZ3",
"0P0000J1JM",
"0P00013668",
"0P0000K9E7"] */
Note also that the forced unwrapping operator ! is generally to be avoided, but in this particular case, we know that both shorthand arguments $0 and $1 in the predicate closure supplied to sorted(...) are guaranteed to be valid keys in the dictionary (since your dictionary is immutable: so no asynch risks here).
First, make a new array of strings called keysArray.
Second, loop over your array of dictionaries and add all the keys to the newly created array.
Now keysArray should be an array of the keys.
Third, sort sortedKeysArray as follow
var sortedArray = sortedKeysArray.sorted { $0.localizedCaseInsensitiveCompare($1) == NSComparisonResult.OrderedAscending}
Now we have an array of the sorted Keys.
After this if you need to have an array of the dictionaries, create a new array called sortedDictArray. Then loop over sortedKeysArray we made previously and get the dictionary by key and add it to sortedDictArray. Viola!
Hope this helps you!
Ordered Dictionary
I've used the open source OrderedDictionary created by Lukas Kubanek to solve problems like this.
For the usage of this library you can refer to the example playground.

Resources