gpt4 book ai didi

c# - 将 C# 函数转换为 Swift 4.2

转载 作者:行者123 更新时间:2023-11-28 09:49:04 29 4
gpt4 key购买 nike

我找到了 this thread谈论this article .我在线程中发现了一些代码,看起来 正是我的项目所需要的。但是,它是用 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
}

我需要帮助才能正确翻译此函数,以便它返回 255 以下的 47 个可能的整数。

最佳答案

在 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
}
}

关于c# - 将 C# 函数转换为 Swift 4.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52757511/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com