Конвертировать функцию C # в Swift 4.2 - PullRequest
0 голосов
/ 11 октября 2018

Я нашел эту тему , говорящую о этой статье .Я нашел некоторый код в потоке, который выглядит как как раз то, что мне нужно для моего проекта.Но это на C #, и я не знаю, как перевести его на Swift.

C # Код:

[Flags]
public enum Directions
{
    NorthWest  = 1 << 0,
    North = 1 << 1,
    NorthEast   = 1 << 2,
    West   = 1 << 3,
    East  = 1 << 4,
    SouthWest = 1 << 5,
    South   = 1 << 6,
    SouthEast   = 1 << 7,
}

private static Directions CalculateTileFlags(bool east, bool west, bool north, bool south, bool northWest, bool northEast, bool southWest, bool southEast)
{
    var directions = (east ? Directions.East : 0) | (west ? Directions.West : 0)  | (north ? Directions.North : 0) | (south ? Directions.South : 0);
    directions |= ((north && west) && northWest) ? Directions.NorthWest : 0;
    directions |= ((north && east) && northEast) ? Directions.NorthEast : 0;
    directions |= ((south && west) && southWest) ? Directions.SouthWest : 0;
    directions |= ((south && east) && southEast) ? Directions.SouthEast : 0;
    return directions;
}

Моя попытка до сих пор:

var Flags = [Int]()
enum Directions : Int {
    case NorthWest
    case North
    case NorthEast
    case West
    case East
    case SouthWest
    case South
    case SouthEast

    func getTuple() -> Int {
        switch self {
        case .NorthWest:
            return 1 << 0
        case .North:
            return 1 << 1
        case .NorthEast:
            return 1 << 2
        case .West:
            return 1 << 3
        case .East:
            return 1 << 4
        case .SouthWest:
            return 1 << 5
        case .South:
            return 1 << 6
        case .SouthEast:
            return 1 << 7
        }
    }
}

Я получил эту часть.Это было легко.Эта функция - часть, которую я не могу понять.Я думаю, что я близко, но я не знаю.

func CalculateTileFlags(east: Bool, west: Bool, north: Bool, south: Bool,
                            northWest: Bool, northEast: Bool, southWest: Bool, southEast: Bool) -> Int {

    var eastD = Directions.East.getTuple()
    var westD = Directions.West.getTuple()
    var northD = Directions.North.getTuple()
    var southD = Directions.South.getTuple()
    var northWestD = Directions.NorthWest.getTuple()
    var northEastD = Directions.NorthEast.getTuple()
    var southWestD = Directions.SouthWest.getTuple()
    var southEastD = Directions.SouthEast.getTuple()

    var directions = east ? true : false || west ? true : false || north ? true : false || south ? true : false

    directions != ((north && west) && northWest) ? northWestD : 0
    directions != ((north && east) && northEast) ? northEastD : 0
    directions != ((south && west) && southWest) ? southWestD : 0
    directions != ((south && east) && southEast) ? southEastD : 0

    return directions
}

Мне нужна помощь, чтобы правильно перевести эту функцию, чтобы она возвращала 47 возможных целых чисел ниже 255.

Ответы [ 3 ]

0 голосов
/ 11 октября 2018

Ну, теперь я чувствую себя идиотом.C # и Swift являются языками на основе C, поэтому они имеют схожую функциональность и схожие операции.В данном случае это был почти прямой перевод.

Окончательный код перевода:

enum Dir : Int {
    case NorthWest = 1
    case North     = 2
    case NorthEast = 4
    case West      = 8
    case East      = 16
    case SouthWest = 32
    case South     = 64
    case SouthEast = 128
}

И функция:

func CalculateTileFlags(east: Bool, west: Bool, north: Bool, south: Bool,
                        northWest: Bool, northEast: Bool, southWest: Bool, southEast: Bool) -> Int {

    var directions = (east ? Dir.East.rawValue : 0) | (west ? Dir.West.rawValue : 0)  | (north ? Dir.North.rawValue : 0) | (south ? Dir.South.rawValue : 0)

    directions |= ((north && west) && northWest) ? Dir.NorthWest.rawValue : 0
    directions |= ((north && east) && northEast) ? Dir.NorthEast.rawValue : 0
    directions |= ((south && west) && southWest) ? Dir.SouthWest.rawValue : 0
    directions |= ((south && east) && southEast) ? Dir.SouthEast.rawValue : 0

    return directions
}

Возвращает двоичное целое число в точности так, как мне нужно.

0 голосов
/ 11 октября 2018

В Swift OptionSet является ближайшей структурой данных для перечисления со сдвинутыми битами значениями на другом языке:

Ваш код C # может быть переведен следующим образом:

struct Directions: OptionSet {
    var rawValue: Int
    init(rawValue: Int) {self.rawValue = rawValue}

    static let northWest = Directions(rawValue: 1 << 0)
    static let north     = Directions(rawValue: 1 << 1)
    static let northEast = Directions(rawValue: 1 << 2)
    static let west      = Directions(rawValue: 1 << 3)
    static let east      = Directions(rawValue: 1 << 4)
    static let southWest = Directions(rawValue: 1 << 5)
    static let south     = Directions(rawValue: 1 << 6)
    static let southEast = Directions(rawValue: 1 << 7)
}

extension Directions {
    static func calculateTileFlags(
        east: Bool = false,
        west: Bool = false,
        north: Bool = false,
        south: Bool = false,
        northWest: Bool = false,
        northEast: Bool = false,
        southWest: Bool = false,
        southEast: Bool = false) -> Directions
    {
        var directions: Directions = [
            east ? Directions.east : [],
            west ? Directions.west : [],
            north ? Directions.north : [],
            south ? Directions.south : [],
        ]

        directions.formUnion((north && west) && northWest ? Directions.northWest : [])
        directions.formUnion((north && east) && northEast ? Directions.northEast : [])
        directions.formUnion((south && west) && southWest ? Directions.southWest : [])
        directions.formUnion((south && east) && southEast ? Directions.southEast : [])

        return directions
    }
}
0 голосов
/ 11 октября 2018

Ваше начальное значение для directions неверно.Вам следует использовать логические значения направления для выбора значения или 0.

. Кроме того, вы используете !=, который является логическим сравнением, вместо |=, который является двоичной операцией ИЛИ.

Попробуйте это:

var directions = (east ? eastD : 0) | (west ? westD : 0)  | (north ? northD : 0) | (south ? southD : 0)

directions |= ((north && west) && northWest) ? northWestD : 0
directions |= ((north && east) && northEast) ? northEastD : 0
directions |= ((south && west) && southWest) ? southWestD : 0
directions |= ((south && east) && southEast) ? southEastD : 0

return directions

Кроме того, я бы рекомендовал просто установить rawValues ​​для вашего перечисления следующим образом:

enum Directions : Int {
    case NorthWest = 1    // 1 << 0
    case North     = 2    // 1 << 1
    case NorthEast = 4    // 1 << 2
    case West      = 8    // 1 << 3
    case East      = 16   // 1 << 4
    case SouthWest = 32   // 1 << 5
    case South     = 64   // 1 << 6
    case SouthEast = 128  // 1 << 7
}

Тогда вместо Directions.East.getTuple() you 'буду использовать Directions.East.rawValue.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...