Xcode Swift check if array contains object - arrays

I have this array :
var preferiti : [ModalHomeLine!] = []
I want to check if the array contains the same object.
if the object exists {
} else {
var addPrf = ModalHomeLine(titolo: nomeLinea, link: linkNumeroLinea, immagine : immagine, numero : titoloLinea)
preferiti.append(addPrf)
}

Swift has a generic contains function:
contains([1,2,3,4],0) -> false
contains([1,2,3,4],3) -> true

So it sounds like you want an array without duplicate objects. In cases like this, a set is what you want. Surprisingly, Swift doesn't have a set, so you can either create your own or use NSSet, which would look something like this:
let myset = NSMutableSet()
myset.addObject("a") // ["a"]
myset.addObject("b") // ["a", "b"]
myset.addObject("c") // ["a", "b", "c"]
myset.addObject("a") // ["a", "b", "c"] NOTE: this doesn't do anything because "a" is already in the set.
UPDATE:
Swift 1.2 added a set type! Now you can do something like
let mySet = Set<String>()
mySet.insert("a") // ["a"]
mySet.insert("b") // ["a", "b"]
mySet.insert("c") // ["a", "b", "c"]
mySet.insert("a") // ["a", "b", "c"] NOTE: this doesn't do anything because "a" is already in the set.

Related

Compare each element in two arrays Swift

I have two arrays of strings. for example:
let arrayFirst: [String] = ["A", "A", "A", "A", "A"]
let arraySecond: [String] = ["A", "C", "A", "B", "A"]
I need to compare this two arrays each element in array and return for every sequence bool state.
For example here will be answer:
let resultArray: [Bool] = [true, false, true, false, true]
how to do it better?
You can consider using the zip function.
let resultArray = zip(arrayFirst, arraySecond).map {
return $0.0 == $0.1
}
This will work even you have arrays of different length as zip will ignore the additional elements of the longer array.

Checking populated array in Swift4

Keep getting error Value of type [String] has no member removeAtIndex
Does any help, please?
Thanks
var strings2 = ["a", "b", "c", "d"]
if strings2.isEmpty {
print("empty")
}
else {
print("populated") // populated
}
strings2.insert("a", at: 0) // Insert, not replace
print(strings2.removeAtIndex(0)) // a
The function is called remove(at:).
print(strings2.remove(at: 0))

2D array Swift goes out of bounds when appending arrays [duplicate]

This question already has answers here:
Why is this array out of index?
(2 answers)
Closed 4 years ago.
I want to append elements from one 2D array to the another 2D array, but I get fatal error index out of bound.
the code is as follows:
var array = [["a", "b", "c"], ["d","e","f"],["g","h","i"]]
var array2 = [[String]]()
var x = array.count
var y = array[1].count
for j in 0..<x {
for i in 0..<y {
array2[j].append(array[j][i])
}
}
print(array2)
please don't tell me to just copy the array as this is not what I need, I am using this procedure to do something more complex than just copying an array.
Any suggestions as to why it goes out of bounds?
thanks
array2[j] doesn't exist as it's just an empty array.
It would be much easier to do this:
var array = [["a", "b", "c"], ["d","e","f"],["g","h","i"]]
var array2 = [[String]]()
for item in array {
array2.append(item)
}
print(array2)
[["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]]
But that is just copying the array exactly. You should provide a more precise example of what you are trying to achieve.
Another option (Which makes your current code work) is to create a 'row' in the first loop ready for insertion:
var array = [["a", "b", "c"], ["d","e","f"],["g","h","i"]]
var array2 = [[String]]()
var x = array.count
var y = array[1].count
for j in 0..<x {
array2.append([String]())
for i in 0..<y {
array2[j].append(array[j][i])
}
}
print(array2)
Which gives the same output:
[["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]]
It is going out of bounds because array2[0] doesn't exist. You need to create an an empty array there before appending to it. Then append that array to the outer array.
var array = [["a", "b", "c"], ["d","e","f"],["g","h","i"]]
var array2 = [[String]]()
for j in 0..<array.count {
var stringArray: [String] = []
for i in 0..<array[j].count {
stringArray.append(array[j][i])
}
array2.append(stringArray)
}
print(array2)

Swift 3 - Check if a string exist inside an array and sort it

I have this special array var myArray : [Array<String>] = [["a"],["b"],["c"]]
I want to detect if "a" is already inside myArray
and after i would like to sort my array alphabetically but i havn't found any function to do all these things for my array
To find if your [[String]] contains "a", you can use contains twice:
var myArray : [Array<String>] = [["a"],["b"],["c"]]
if myArray.contains(where: { $0.contains("a") }) {
print("a found")
}
To sort the inner arrays, apply map to the outer array and sort each element:
var myArray : [Array<String>] = [["c", "a"], ["e", "b"], ["d"]]
let sorted = myArray.map { $0.sorted() }
print(sorted)
[["a", "c"], ["b", "e"], ["d"]]

Create dictionary in swift with array of strings as value

When creating a dictionary in swift with array of strings as value as follows:
lazy var info : Dictionary = {
var dictionary = [String: [String]]()
dictionary["Key1"] = ["A", "B", "C"]
dictionary["Key2"] = ["D", "E", "F"]
return dictionary
}()
Cannot assign to immutable expression of type 'Value?'
Any ideas whats wrong here?
I've seen your own answer, which works, but there's a better way: use a Swift dictionary by declaring its proper type, in your case [String: [String]]:
lazy var info : [String: [String]] = {
var dictionary = [String: [String]]()
dictionary["Key1"] = ["A", "B", "C"]
dictionary["Key2"] = ["D", "E", "F"]
return dictionary
}()
Change
lazy var info : Dictionary = {
var dictionary = [String: [String]]()
dictionary["Key1"] = ["A", "B", "C"]
dictionary["Key2"] = ["D", "E", "F"]
return dictionary
}()
to
lazy var info : NSDictionary = {
var dictionary = [String: [String]]()
dictionary["Key1"] = ["A", "B", "C"]
dictionary["Key2"] = ["D", "E", "F"]
return dictionary
}()
and all is good :)

Resources