标签: struct

使用 Swift 4 中的 Encodable / Decodable 对结构进行编码 / 解码

我不太习惯结构体,我知道使用结构体的唯一方法就是这样......

struct UserDetails {
  let name: String
  let message: String

  init(name: String, message: String) {
    self.name = name
    self.message = message
  }
}
Run Code Online (Sandbox Code Playgroud)

name一旦我收到和的值message,我就会将它们添加到结构中,如下所示......

let userDetails = UserDetails(name: theName, message: theMessage)

然后将其添加到结构类型的数组中,如下所示......

self.userDetailsArray.append(userDetails) 
Run Code Online (Sandbox Code Playgroud)

现在终于,当我想获取个人姓名或消息时,我会这样得到......

let user = userDetailsArray[indexPath.row]
cell.userNameLabel.text = user.name
cell.messageLabel.text = user.message
Run Code Online (Sandbox Code Playgroud)

这也很好用。但我担心的是......我上面指定的是一种非常古老的方法,因为 swift 已经提出了NSCoding,然后Codable协议也提出了。但我不知道如何实施它们。

希望有人可以展示我如何将NSCoding/Codable协议应用到我上面提到的示例中......

struct ios swift

-3
推荐指数
1
解决办法
4036
查看次数

允许反射从 Go 中的结构中获取未导出的字段

我目前正在使用反射从结构中获取字段并将值作为接口值的一部分返回。我遇到了未导出字段的问题,我希望能够获取未导出的值并将它们与导出的字段一起返回。当我尝试从未导出的字段中获取值时,出现以下错误:

reflect.Value.Interface:无法返回从未导出的字段或方法中获得的值 [已恢复]

我一直在使用https://github.com/fatih/structs作为我的代码的基础,并希望它能够处理未导出的字段。

// Values returns us the structs values ready to be converted into our repeatable digest.
func (s *StructWrapper) Values() []interface{} {
    fields := s.structFields()

    var t []interface{}

    for _, field := range fields {
        val := s.value.FieldByName(field.Name)
        if IsStruct(val.Interface()) {
            // look out for embedded structs, and convert them to a
            // []interface{} to be added to the final values slice
            t = append(t, Values(val.Interface())...)
        } else {
            t = append(t, val.Interface())
        }
    } …
Run Code Online (Sandbox Code Playgroud)

struct go reflect

-3
推荐指数
1
解决办法
514
查看次数

如何将数据从一种类型复制到具有相同结构的另一种类型?

在下面的代码中:

type ProductEntity struct {
    ID          int     `json:"id"`
    Name        string  `json:"name"`
    Description string  `json:"description"`
    Price       float32 `json:"price"`
    SKU         string  `json:"sku"`
    CreatedOn   string  `json:"-"`
    UpdatedOn   string  `json:"-"`
    DeletedOn   string  `json:"-"`
}

type ProductEntityList []*ProductEntity

type PostRequestModel struct {
    ID          int     `json:"id"`
    Name        string  `json:"name"`
    Description string  `json:"description"`
    Price       float32 `json:"price"`
    SKU         string  `json:"sku"`
    CreatedOn   string  `json:"-"`
    UpdatedOn   string  `json:"-"`
    DeletedOn   string  `json:"-"`
}

type RequestBody []*PostRequestModel

func convertModelToEntity(modelList RequestBody) ProductEntityList {

    // return entity.ProductEntityList(modelList) // type conversion error 

}
Run Code Online (Sandbox Code Playgroud)

如何将数据从一种类型复制到另一种具有相同结构的类型?因为RequestBody …

struct pointers go slice

-3
推荐指数
1
解决办法
54
查看次数

为什么不应该在 C++ 中使用 char 和其他 C 函数?

有人可以就这个问题的答案给我启发吗?在结构中为 char* 分配一个值

在答案,他们告诉提问者不要混用C和C ++,即使用stringcin如果它的C ++或者 char如果它的C.这让我迷惑,因为我知道那是什么,我们可以用这样的charprintf所以在C ++通过包括适当的库. 如果char因为几个原因我们应该在 C++ 项目中工作怎么办?

答案之一还说typedef struct{...}x;在 C++ 中没有必要,而到目前为止我知道它用于防止重新键入struct x,例如x Name1; x Name2;代替struct x Name1; struct x Name2;. 对于像我这样的初学者来说,这已经足够令人困惑了。

c c++ arrays struct mixing

-3
推荐指数
1
解决办法
154
查看次数

程序接收信号SIGSEGV分段故障

我已经制作了MCVE例程,程序崩溃并在我添加到unpack_code函数后返回255(之前它正在工作):

int ch_bit; 
unsigned int *ch_word; 
ch_bit = *p_ch_bit; 
Run Code Online (Sandbox Code Playgroud)

波士顿代码

#include <stdio.h>
#include <stdlib.h>

struct melp_param *par;

void unpack_code( unsigned int **p_ch_beg, int *p_ch_bit, 
        int *p_code, int numbits, int wsize,unsigned int erasemaks)
{
      int ret_code;
      int ch_bit;
      unsigned int *ch_word;
      ch_bit = *(p_ch_bit);
      *p_code = 0;

}

void melp_chn_read(struct melp_param *par, 
                   struct melp_param *prev_par)
{



}

int main(void)
{
      int bit_buffer[42];

    unpack_code(NULL, NULL, bit_buffer, 0, 0, 0);

    return 0;

}
Run Code Online (Sandbox Code Playgroud)

经过一步一步的调试后:*p_ch_bit=Cannot access memory at address 0x0和消息错误出现'分段错误.....'

请帮我

c c++ struct pointers

-4
推荐指数
1
解决办法
265
查看次数

在c中声明结构内的变量

我想为我的编程语言创建一个面向对象的预处理器,将我的语言转换为C语言(如早期的C++).我想用结构模拟类.问题是:如何在结构中声明一个变量,如下所示:

typedef struct { //equivalent of class
   int a = 5;
   int (*sub)(int) = &int_sub; //function of a class, uses an external declared function
} class_name;
Run Code Online (Sandbox Code Playgroud)

我尝试了上面的代码,但编译器写了这个:

error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token 
       void (*sub)(int) = &int_sub;
Run Code Online (Sandbox Code Playgroud)

我有两个问题:

  1. 我可以在结构中声明一个变量吗?

  2. 如果有,怎么样?

c oop struct c-preprocessor

-4
推荐指数
1
解决办法
411
查看次数

struct pointer end和指针的含义是什么?C++

这个*头的含义是什么?

struct node{
  int data;
  struct node *next;
}*head;
Run Code Online (Sandbox Code Playgroud)

c++ struct

-4
推荐指数
1
解决办法
402
查看次数

struct和指向struct的指针之间的区别

假设我有一个如下所示的结构:

struct node{
  node *next;
  int data;
}
Run Code Online (Sandbox Code Playgroud)

我有一个C++函数,class Stack它定义了一个push类似的操作:

void Stack::push(int n){
    node *temp = new node;
    temp->data = n;
    temp->next = top;
    top = temp;

if(topMin == NULL) {
    temp = new node;
    temp->data = n;
    temp->next = topMin;
    topMin = temp;
    return;
}

    if(top->data < topMin->data) {
        temp = new node;
        temp->data = n;
        temp->next = topMin;
        topMin = temp;
    }
    return;
}
Run Code Online (Sandbox Code Playgroud)

使用之间有什么区别

node *temp = new node;
Run Code Online (Sandbox Code Playgroud)

temp = new …
Run Code Online (Sandbox Code Playgroud)

c++ struct pointers

-4
推荐指数
1
解决办法
143
查看次数

为什么`struct T {double x};`的输出在C和C++中有所不同?

struct T{ double x};
Run Code Online (Sandbox Code Playgroud)

在C中,它没有任何问题.

但在C++中,它给出了以下编译错误:

预期';' 在会员声明结束时.

c c++ struct language-lawyer

-4
推荐指数
1
解决办法
172
查看次数

为什么结构中的函数适用于C语言

如这个小脚本所示.

#include <stdio.h>

struct student{
    short count;
    void (*addCount)();
};

void student_addCount(struct student a){
    a.count++;
}

int main(){
    struct student student;
    student.addCount = student_addCount;
    student.count = 0;
    student.addCount();
    student.addCount();
    student.addCount();
    student.addCount();
    student.addCount();
    printf("%d\n",student.count);
}
Run Code Online (Sandbox Code Playgroud)

我已经添加了一个指向结构内部函数的指针,但我不知道为什么这会起作用,因为函数'addCount'没有接收任何参数,它实际上累计了指定的次数.

我正在使用GCC 6.3.0在不同的环境中编译此代码,例如ideone.com,wandbox.org和WSL中的编译器.

这是证明它与ideone一起工作. https://ideone.com/Cam4xY

c struct function-pointers gcc6

-4
推荐指数
1
解决办法
41
查看次数