Asked  7 Months ago    Answers:  5   Viewed   40 times

I am trying to find an item index by searching a list. Does anybody know how to do that?

I see there is list.StartIndex and list.EndIndex but I want something like python's list.index("text").



As swift is in some regards more functional than object-oriented (and Arrays are structs, not objects), use the function "find" to operate on the array, which returns an optional value, so be prepared to handle a nil value:

let arr:Array = ["a","b","c"]
find(arr, "c")!              // 2
find(arr, "d")               // nil

Use firstIndex and lastIndex - depending on whether you are looking for the first or last index of the item:

let arr = ["a","b","c","a"]

let indexOfA = arr.firstIndex(of: "a") // 0
let indexOfB = arr.lastIndex(of: "a") // 3
Tuesday, June 1, 2021
answered 7 Months ago

It's not simple really. The line:

var arr : [[Int]] = []

Creates a variable of type Array of Array of Int and initially the array is empty. You need to populate this like any other other array in Swift.

Let's step back to a single array:

var row : [Int] = []

You now have an empty array. You can't just do:

row[6] = 10

You first have to add 7 values to the array before you can access the value at index 6 (the 7th value).

With your array of arrays, you need to fill in the outer array with a whole set of inner arrays. And each of those inner arrays need to be filled out with the proper number of values.

Here is one simple way to initialize your array of arrays assuming you want a pre-filled matrix with every value set to 0.

var matrix : [[Int]] = Array(repeating: Array(repeating: 0, count: 10), count: 10)

The outer count represents the number of rows and the inner count represents the number of columns. Adjust each as needed.

Now you can access any cell in the matrix:

matrix[x][y] = 1 // where x and y are from 0 to rows-1/columns-1
Friday, June 18, 2021
answered 6 Months ago

You can create your own extension for arrays.

extension Array where Element: Equatable {
    func indexes(of element: Element) -> [Int] {
        return self.enumerated().filter({ element == $0.element }).map({ $0.offset })

You can simply call it like this

items.indexes(of: "A") // [0, 2, 4]
items.indexes(of: "B") // [1]
Wednesday, July 21, 2021
answered 5 Months ago

Try this:

import Foundation

protocol Section {
    associatedtype InfoT: Equatable
    associatedtype ItemsT: Equatable
    var info: InfoT? { get }
    var items: [ItemsT] { get }

extension Array where Element: Section {
    // Just dummy example function:
    func debugDummy() {
        for section in self {
            let info =
            print("section info: (String(describing: info)).")
            for item in section.items {
                print("item: (item).")
Monday, August 16, 2021
answered 4 Months ago

What you are looking for is available inside object returned by window.getSelection()

        var txt = this.innerText;
        var selection = window.getSelection();
        var start = selection.anchorOffset;
        var end = selection.focusOffset;
        if (start >= 0 && end >= 0){
    	    console.log("start: " + start);
    	    console.log("end: " + end);
<div id="ip">YOLO Cobe</div>

And here is example for more complex selections on page based on @Kaiido comment:

        var txt = this.innerText;
        var selection = window.getSelection();
        var start = selection.anchorOffset;
        var end = selection.focusOffset;
        console.log('start at postion', start, 'in node', selection.anchorNode.wholeText)
        console.log('stop at position', end, 'in node', selection.focusNode.wholeText)
<div><span>Fragment1</span> fragment2 <span>fragment3</span></div>
Thursday, September 30, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :