Свойства только для чтения

Используйте условие if с циклом while и попробуйте.

например.

if ($result = $conn->query($query)) {

    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {

    }
    /* free result set */
    $result->free();
}
29
задан AllocSystems 21 December 2016 в 22:33
поделиться

3 ответа

Что-то вроде этого? (как предложено @vacawama в комментариях)

struct Triangle {
    let edgeA: Int
    let edgeB: Int
    let edgeC: Int

    var isEquilateral: Bool {
        return (edgeA, edgeB) == (edgeB, edgeC)
    }
}

Давайте проверим

let triangle = Triangle(edgeA: 5, edgeB: 5, edgeC: 5)
triangle.isEquilateral // true

или

let triangle = Triangle(edgeA: 2, edgeB: 2, edgeC: 1)
triangle.isEquilateral // false
16
ответ дан Luca Angeletti 21 December 2016 в 22:33
поделиться

Если вам нужно хранимое свойство «только для чтения», используйте private(set):

private(set) var isEquilateral = false

Если это свойство вычисляется из других свойств, тогда да, используйте свойство computed:

var isEquilateral: Bool {
    return a == b && b == c
}
87
ответ дан Rob 21 December 2016 в 22:33
поделиться

Свойство только для чтения - это свойство с getter, но без setter. Он всегда используется для возврата значения.

class ClassA {
    var one: Int {
        return 1
    }
    var two: Int {
        get { return 2 }
    }
    private(set) var three:Int = 3
    init() {
        one = 1//Cannot assign to property: 'one' is a get-only property
        two = 2//Cannot assign to property: 'two' is a get-only property
        three = 3//allowed to write
        print(one)//allowed to read
        print(two)//allowed to read
        print(three)//allowed to read
    }
}
class ClassB {
    init() {
        var a = ClassA()
        a.one = 1//Cannot assign to property: 'one' is a get-only property
        a.two = 2//Cannot assign to property: 'two' is a get-only property
        a.three = 3//Cannot assign to property: 'three' setter is inaccessible
        print(a.one)//allowed to read
        print(a.two)//allowed to read
        print(a.three)//allowed to read
    }
}
0
ответ дан RajeshKumar R 21 December 2016 в 22:33
поделиться
Другие вопросы по тегам:

Похожие вопросы: