在swift中设置c struct里面的char []

时间:2016-06-28 00:59:35

标签: swift

C struct中的char [10]被导入为10个UInt8项的元组。有很多关于如何从该struct成员中读取swift字符串值的示例,但我希望能够在成员中设置某些内容?

NHibernate.ISession

如何在swift项目中设置值?

2 个答案:

答案 0 :(得分:0)

C语法中出现语法错误:

var a = CStruct(item: (0, 1, 2, 3, 4, 5, 6, 7, 8, 9))
a.item.0 = 42
a.item.1 = 30

print(a)

如果你想在Swift中修改它:

def func1(n: Int): Double = {
   var a = 0.0D
   while (not terminated) {
       /// improve value of 'a' with algorithm 1 
   }
}

def func2(n: Int): Double = {
   var a = 0.0D
   while (not terminated) {
       /// improve value of 'a' with algorithm 2 
   }
}

答案 1 :(得分:0)

一种方法是使用withUnsafeMutablePointer(_:_:)

var a = CStruct(item: (0, 1, 2, 3, 4, 5, 6, 7, 8, 9))
var CStructPtr = withUnsafeMutablePointer(&a.item) { (b) -> UnsafeMutableBufferPointer<Int8> in
    // UnsafeMutableBufferPointer is used because it a little more safe.
    return UnsafeMutableBufferPointer(start: UnsafeMutablePointer<Int8>(b), count: 10)
}

//Modify the tuple however you want
CStructPtr[0] = 42
CStructPtr[1] = 30