How to find index of Int element in an array? [duplicate] - ios

This question already has answers here:
How to find index of list item in Swift?
(23 answers)
Closed 7 years ago.
Is there a "better"(less verbose) way of doing this?
var found: Int?
for i in 0...myArray-1 {
if myArray[i] == 3 {
found = i
}
}
similar to
let i = find(myArray, "1")
but for Int ?
This question is slightly "different" than How to find index of list item in Apple's swift? because it directly address the find function for Int, instead of String. Granted, the only difference is the title and pointing out that you can remove the quotation marks to get Int.

Here's an Int example
let myArray = [1, 2, 3]
var i = find(myArray, 2)
Good luck

Related

Testing if array contains at least one object that has some property [duplicate]

This question already has answers here:
Shorthand to test if an object exists in an array for Swift?
(2 answers)
Closed 4 years ago.
I want to test if the given array contains at least one object that contains a specific 'string' in it. Is it useful and possible?
Try filter().
struct S { let string: String }
let array = [ S(string: "a"), S(string: "b") ]
let hasAtleastOneA = array.filter({ $0.string == "a" }).count > 0
something like this :
let array = ["a","b","c"]
if array.count > 0 {
for name in array {
if name.contains("a"){
print("YES")
}
}
}
You check do this way,
let filtered = data.filter({$0.contains("test")})
Reference Higher order functions in swift: Filter, Map, Reduce, flatmap

Swift: Determine if array contains array [duplicate]

This question already has answers here:
Why can't I check to see if my array of arrays contains a specific array?
(2 answers)
Closed 7 years ago.
Im trying to figure out how to check if array contains another array:
var grid: [[Int]]!
grid = []
grid.append([1,1])
if grid.contains([1,1]) {
}
but the line
if grid.contains([1,1]) {
throws the error:
Contextual type '#noescape ([Int]) throws -> Bool' cannot be used with
array literal
Swift arrays doesn't conform to Equatable by default. But you can still compare them in the predicate:
if (grid.contains { $0 == [1,1] } == true) {
}

Convert Equation String to Int [duplicate]

This question already has answers here:
Swift - Resolving a math operation in a string
(4 answers)
Closed 7 years ago.
I'm trying to convert a string like so:
var equation = "8+3*4/5-1*(5+5)"
to an Int so that it will calculate it with order of operations but with my code it is just return nil. Here is what I have tried
var answer = Int(equation)
Try it this way:
var equation = "\(8+3*4/5-1*(5+5))" //"0"
And this way you can convert it to Int
var answer = equation.toInt() // 0
Or you can directly do it this way:
var equation = 8+3*4/5-1*(5+5) // 0
And As Martin R suggested if you want to do math operation in a string you can do it this way:
let expn = NSExpression(format:"8+3*4/5-1*(5+5)")
println(expn.expressionValueWithObject(nil, context: nil)) //"0"
Which will result as String but you can convert it to Int as I suggested above.
Here is original link for math operation in a string: Swift - Resolving a math operation in a string

How can I convert a String to an Int in swift? [duplicate]

This question already has answers here:
Converting String to Int with Swift
(31 answers)
Closed 8 years ago.
I'm working in a small project and I faced this problem : text field is String by default so I need to take the input and convert it to integer then calculate I was searching for a way to solve the converting problem and I didn't find any working solution so can any one help me please ?
try like this
In Swift 2.0, toInt(),replaced with initializers. (In this case, Int(someString).)
let myString : String = "50"
let x : Int? = Int(myString)
if (x != null) {
// converted String to Int
}
myString.toInt() - convert the string value into int .

EXC_BAD_INSTRUCTION - Fetching a Random Item from an Array [duplicate]

This question already has answers here:
Crash when casting the result of arc4random() to Int
(7 answers)
Closed 8 years ago.
I am trying to fetch a random item from an array containing 3 strings as follows :
func selectRandomImage () {
var imageName : String? //Optional
var arrayCount : NSNumber = animalsArray.count //Bridges to an NSNumber
var x : Int = Int(arc4random())%(arrayCount.integerValue)
var name : String = animalsArray[x] as String
println("Name is \(name)")
}
However every 5 runs or so of the code, I am getting a crash with EXC_BAD_INSTRUCTION.
Could anyone advise on what could be wrong or how to troubleshoot ?
Thanks !
arc4random doesent work well with swift, it crushes when you try to cast it to Int
Try Int(rand()) instead of arc4random()

Resources