How to get 4th value from array swift

get an array of property values from an object array swift
swift set to array
swift for loop
swift dictionary
swift array of objects
swift array get object at index
swift array filter
array of dictionary in swift 4

I am trying to get 4th items from the arrays. These given below array:

let mainArray = ["1", "2", "3", "7", "10", "4", "5", "6", "9", "12", "16", "11", "13", "14", "15"]
let valueCheck = "3"

//check value present or not in array

if mainArray.contains(valueCheck){

    //if yes print next value from match value
    Let valuePresent = self.mainArray.index(after: Int(valueCheck)!)
    print(valuePresent)
}

output = "4"

but i want "7"

When i am trying to above code to get 4th value they give me 4th index but i want 4th index value.

Can someone please explain to me how to solve this, i've tried to solve this but no results yet.

Any help would be greatly appreciated.

Thanks in advance.

let mainArray = ["1", "2", "3", "7", "10", "4", "5", "6", "9", "12", "16", "11", "13", "14", "15"]
let valueCheck = "3" // index for this is 2

//check value present or not in array
if let index = mainArray.firstIndex(of: valueCheck){
    let value = mainArray[index + 1]
    print(value)
}

While this will work, if for example, you want the item after "15", valueCheck = "15" this will crash.

You can prevent this by extending collection with:

extension Collection {

    subscript(safe index: Index) -> Element? {
        return return index < endIndex && index >= startIndex ? self[index] : nil
    }
}

And then update your code with:

let mainArray = ["1", "2", "3", "7", "10", "4", "5", "6", "9", "12", "16", "11", "13", "14", "15"]
let valueCheck = "3" // index for this is 2

//check value present or not in array
if let index = mainArray.firstIndex(of: valueCheck){
    let value = mainArray[safe: index + 1]
    print(value)
}

Collection Types — The Swift Programming Language (Swift 5.3), It also means you can be confident about the type of values you will retrieve from a The type of a Swift array is written in full as Array<Element> , where Element is the shoppingList += ["Baking Powder"]; // shoppingList now contains 4 items � To get the value using a key from a Swift Dictionary, use the following syntax. var value = myDictionary [ key ] The syntax is similar to accessing a value from an array using the index.

you have to set the index of the third element and that is "7" because the array starts with the index of zero

let mainArray = ["1", "2", "3", "7", "10", "4", "5", "6", "9", "12", "16", "11", "13", "14", "15"]
let valueCheck = "7" // index for this is 3

//check value present or not in array

if mainArray.contains(valueCheck){

//if yes print next value from match value
Let valuePresent = self.mainArray.index(after: Int(valueCheck)!)

let output = mainArray[valuePresent]
print(output) // the output will be 10

}

Swift Arrays: How to Use it and Why? (With Examples), Also, we have initialized array with 1, 2, 3, 4, 5, 6, 7, 8, 9 values. Swift 4.2 introduces a new protocol named CaseIterable. enum Fruit : CaseIterable { case apple , apricot , orange, lemon } that when you conforms to , you can get an array from the enum cases like this . for fruit in Fruit.allCases { print("I like eating \(fruit).")

First you have to check whether index is not out of bound then you can get the value via Array Subscript like below

let mainArray = ["1", "2", "3", "7", "10", "4", "5", "6", "9", "12", "16", "11", "13", "14", "15"]
let valueCheck = "3"

if let index = Int(valueCheck) {
if mainArray.count > index {
     let result = mainArray[index]
 }
}

Hope it will help you.

Find an item in an array in Swift with first(where:) – LearnAppMaking, Before we dive into Swift's ways you can find items in an array, let's The challenge is simple: given an array of an arbitrary number of items, find the index of a given value. firstIndex(of: "Marvin") { print(index) } // Output: 4. - Get first element of array - Get last element of array - Get maximum element from array - Get minimum element from array - Get the size of an array - Insert item at array index - Join array elements using separator - Loop through array - Loop through array with index - Map an array to another array - Pop last item from array - Reduce array to

let mainArray = ["1", "2", "3", "7", "10", "4", "5", "6", "9", "12", "16", "11", "13", "14", "15"]
let valueCheck = "3"

//check value present or not in array

if mainArray.contains(valueCheck){

    //if yes print next value from match value
    let checkedValueindex = mainArray.firstIndex(of: valueCheck)
    let valuePresentIndex = mainArray.index(after: checkedValueindex!)
    print(mainArray[valuePresentIndex])
}

Swift - Arrays, Swift - Arrays - Swift 4 arrays are used to store ordered lists of values of the same You can retrieve a value from an array by using subscript syntax, passing the� Specifically, you use the Array type to hold elements of a single type, the array’s Element type. An array can store any kind of elements—from integers to strings to classes. Swift makes it easy to create arrays in your code using an array literal: simply surround a comma-separated list of values with square brackets.

let mainArray = ["1", "2", "3", "7", "10", "4", "5", "6", "9", "12", "16", "11", "13", "14", "15"]

if mainArray.indices.contains(3) { let value = mainArray[3] print(value) }

This code will help you.

How to get 4th value from array swift - arrays - php, mainArray.index(after: Int(valueCheck)!) print(valuePresent) } output = "4" but i want "7" When i am trying to above code to get 4th value they give me 4th index� Few Operation relates to Array in Swift. Create Array. var stringArray = ["One", "Two", "Three", "Four"] Add Object in Array. stringArray = stringArray + ["Five"] Get Value from Index object. let x = stringArray[1] Append Object. stringArray.append("At last position") Insert Object at Index. stringArray.insert("Going", atIndex: 1) Remove Object

The Essentials of Arrays in Swift, The need to work with collections of values is pretty common in Swift. you can see we have five elements in our example array, located at indexes 0 through 4. From Apple Docs. You can use subscript syntax to retrieve a value from the dictionary for a particular key. Because it is possible to request a key for which no value exists, a dictionary’s subscript returns an optional value of the dictionary’s value type.

How to find the index of the first matching array element, That will send back 4 as an optional integer, because the first odd number (9) is at index four. Hacking with Swift is sponsored by Paw. How to convert a multidimensional array to a single-dimensional array; About the Swift Knowledge Base. This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

Replacing elements in an array - Mastering Swift 4, Using the subscript, we pick the element of the array we wish to update … - Selection Get Mastering Swift 4 - Fourth Edition now with O'Reilly online learning. Returns a Boolean value indicating whether the sequence contains an element that satisfies the given predicate. func all Satisfy ((Element) -> Bool) -> Bool Returns a Boolean value indicating whether every element of a sequence satisfies a given predicate.

Comments
  • mainArray[3]?
  • docs.swift.org/swift-book/LanguageGuide/CollectionTypes.html#//…
  • Did my answer solve your problem?
  • index < endIndex or even index < endIndex && index >= startIndex is more efficient than indices.contains(index)
  • Nicely spotted @vadian :)
  • No, the output is 8. You are mixing up index and object at index like the OP.
  • yes, you are right i forgot to get the output by the index. @vadian
  • thank you but your ans give me total count, but i want 4th index value
  • Why mainArray.count > value this ??
  • @Sham can you print value of result like print("Result: (result)") , what value your are getting ?
  • @dahiya_boy if index is out of the bound of mainArray then it will crash if we will directly access via index. for example if you execute this statement mainArray[15] then it will crash so i added check to avoid the crash.
  • @MuhammadWaqasBhati It's bcz you have written mainArray[value]. Its never happen you got array with the same index values like you are using. You have to find out the index of value in array. If you do this IndexOutOfBound never happen. Right now check your code for array ["105", "110", "200", "224", "89","119"] and check does you got same response for 110 element.