Check item contains in model object array in swift 3 [duplicate] - arrays

This question already has answers here:
how can I check if a structure is in the array of structures based on its field in Swift3?
(3 answers)
Get element from array of dictionaries according to key
(1 answer)
Closed 5 years ago.
I have a model object called user.
how to find my user.id from all users collection.
I need something like this UsersCollection.contains(user.id)
and returns Bool value.

Here you go.
let contains = UsersCollection.contains{ $0.id == 0 }
Edit:
let object = UsersCollection.first{ $0.id == 0 } // object is optional here.

Related

What are the implicit differences among these array declarations in Swift? [duplicate]

This question already has answers here:
What is an optional value in Swift?
(15 answers)
Confused about Swift Array Declarations
(2 answers)
Closed 5 years ago.
According to the Apple documentation, this is the proper way to create an empty array of a specific object type in Swift 4:
var output_list = [Output]()
In the wild I have seen slightly different array declarations. I believe the following is functionally identical to the previous declaration:
var output_list: [Output] = []
However I'm fairly confident that the following two declaration methods have nuanced differences from the above:
var output_list: [Output]?
var output_list: [Output]!
What are the effective differences to the above listed methods of declaring an array as as instance variable in a Swift 4 class?
EDIT: Okay I understand that for the last two examples the variable declaration is an optional, not a generic array declaration. What about this:
var output_list: [Output?]
Is this effectively the same as var output_list = [Output]()?
Also, is var output_list = [Output]() === var output_list: [Output] = []?

How to create array and display required array value [duplicate]

This question already has answers here:
How does one declare an array in VBScript?
(2 answers)
Closed 5 years ago.
how to create array and display required array value. For example:
Arr[0]="anand" Arr[1]="bala".print Arr optin:2, Bala .
i need the required array output
Queat seems a bit unclear.
May be below piece of code can be relevent to your Question.
'Initializing & Filling up array
Dim testArr(3)
testArr(0)="First"
testArr(1)="Second"
testArr(2)="Third"
Dim Iterator,userInput
'If you Need ro validate the User input you need to use Err Object.
'Like if it's number or not/Is teh User Provided value is positive or not/ etc. etc. which i'm not doing now)
userInput=Cint(inputbox("Please enter a Positive number between 1-" & Ubound(testArr)))
If userInput<=Ubound(testArr) Then
MsgBox(testArr(userInput-1))
Else
MsgBox("Wrong Value Provided. Not Applicable Array Index Number !!!")
End If

Delete all occurrences of value in Swift Array [duplicate]

This question already has answers here:
Remove matched item from array of objects?
(5 answers)
Closed 7 years ago.
So I have an array of Int's and I am trying to create a function to delete a certain value from the array. However, the removeAtIndex() function only deletes the first occurrence and the removeLast() only removes the last. I tried enumerating through the array but I end up with an Array index out of range error, possible due to the reshifting that occurs when deleting an item from the array.
for (index, value) in connectionTypeIDs.enumerate() {
if (value == connectionTypeToDelete){
connectionTypeIDs.removeAtIndex(index)
}
}
Any ideas on how to accomplish this?
The quickest way is to use filter. I don't know is that answer your question but you can have a look on this:
// remove 1 from array
arr = arr.filter{$0 != 1}

How do I check if an array index is out of range SWIFT [duplicate]

This question already has answers here:
Swift Array - Check if an index exists
(12 answers)
Closed 7 years ago.
I want to be able to check when my array index goes out of range. The array elements are all strings, so I tried to do something like this but it doesn't work:
if questions[3] != nil {
}
Can someone just show me how to check?
Before indexing into the array, you need to check:
The intended index is not below 0
array's count is above the intended index
let intendedIndex: Int = 3
if (intendedIndex >= 0 && questions.count > intendedIndex) {
// This line will not throw index out of range:
let question3 = questions[intendedIndex]
}

Java 8 frequency Object in array [duplicate]

This question already has answers here:
Word frequency count Java 8
(11 answers)
Closed 7 years ago.
I have an Object[] array
I need to create map Map<Obejct, Integer>, where Integer value contains frequency of key Object in array.
How can i do it in java 8 style, using Collectors?
You can do (I hope I don't have any typos) :
Map<Object,Long> map = Stream.of(array)
.collect(Collectors.groupingBy(o -> o,
Collectors.counting()));
This should group the elements of the array by equality and count the number of Objects in each group.

Resources