Я нашел эту тему , говорящую о этой статье .Я нашел некоторый код в потоке, который выглядит как как раз то, что мне нужно для моего проекта.Но это на 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.