Swift - 使用for循环读取嵌套结构

问题描述 投票:1回答:1

我正在尝试在我的BLE项目中使用for循环读取嵌套结构。主要目的是使用外设的新值更新全局结构。总共我有60个属性,所以使用循环可以帮助我很多。

结构是:

/* -----------------------------------------------------------------------------
 Attributes structure
 ----------------------------------------------------------------------------- */
struct IthetisAttribute
{
    let UUID: String
    let Length: UInt8
    let Service: String
    var Value: UInt32
}

/* -----------------------------------------------------------------------------
 Pump state profile
 ----------------------------------------------------------------------------- */
struct IthetisPump_state
{
    /* Steps from start: 0xFFF1 and 3 byte */
    static var StepsFromStart : IthetisAttribute =
        IthetisAttribute(UUID: "FFF1", Length: 3,
                         Service: IthetisConstant.STATE_SERVICE_UUID, Value: 0)

    /* Steps of current dose : 0xFFF2 and 3 byte */
    static var StepsCurrent : IthetisAttribute =
        IthetisAttribute(UUID: "FFF2", Length: 3,
                         Service: IthetisConstant.STATE_SERVICE_UUID, Value: 0)

/* ... */ 
}

现在我试图从IthetisPump_state中取出属性结构来检查UUID。我首先尝试过类似的东西:

for attr: IthetisAttribute in IthetisPump_state
{
    /* ... */
}

但它显然不起作用。所以我尝试了别的东西:

let Profile = IthetisPump_state()
for attr: IthetisAttribute in Mirror(reflecting: Profile).children
{
    /* ... */
}

Xcode告诉我Mirror.child不能转换为IthetisAttribute。我尝试了其他几种技术,但我总是遇到错误。

通过嵌套结构的最佳方法是什么?提前致谢 !

- 根据洛伦佐的回答更新---

我试过这种方式:我用数组​​创建了一个新结构(它工作,但后来我尝试使用指针)

struct IthetisPump_profiles
{
    /* State profile */
    static var State: [UnsafePointer<IthetisAttribute>] =
    [
        &IthetisPump_state.StepsFromStart,
        &IthetisPump_state.StepsCurrent,
        /* ... */
    ]
}

在我的主要代码现在我使用这样的东西:

var Attribute_counter = 0
for attr: IthetisAttribute in IthetisPump_profiles.State
{
    if characteristic.uuid.description.isEqual(attr.UUID)
    {
        let temp: UInt32 = SharedMethods.ConvertDataToUInt32(characteristic.value!,
                                                             byteLength:attr.Length)

        IthetisPump_profiles.State[Attribute_counter].Value = temp

        if(attr.UUID == "FFF1")
        {
            print("UUID \(IthetisPump_state.StepsFromStart.UUID)
                value is \(IthetisPump_state.StepsFromStart.Value)")
            print("UUID \(IthetisPump_profiles.State[0].UUID)
                value is \(IthetisPump_profiles.State[0].Value)")
        }
    }
    Attribute_counter = Attribute_counter + 1
}

第一行(使用state.StepsFromStart)给我0,另一行(使用State [0])给我正确的数字。我可以使用枚举,因为它很简单,但我怎么能用指针实现一个解决方案呢?再次感谢 !

swift xcode nested bluetooth-lowenergy structure
1个回答
0
投票

欢迎来到SO。首先,你将static与实例声明混合在一起。我会改变一点。

关于你的问题,你可以使用KVC。这是一个简单的例子:

struct IthetisAttribute {
    let UUID: String
    let length: UInt8
    let service: String
    var value: UInt32
}

struct IthetisPump_state {
    let states = [
        IthetisAttribute(UUID: "1", length: 9, service: "", value: 1),
        IthetisAttribute(UUID: "2", length: 9, service: "", value: 1),
        IthetisAttribute(UUID: "3", length: 9, service: "", value: 1),
        IthetisAttribute(UUID: "4", length: 9, service: "", value: 1),
        IthetisAttribute(UUID: "5", length: 9, service: "", value: 1)
    ]
}

let pump = IthetisPump_state()    
for i in 0..<pump.states.count {
    print(pump[keyPath:\IthetisPump_state.states[i].UUID])
}

如果您需要处理更复杂的事情,可以将enum与关联值一起使用。

让我知道任何事情。

附:实例变量应该以较低的字母开头。例如service而不是Service

© www.soinside.com 2019 - 2024. All rights reserved.