struct

Passing a struct pointer as a parameter in C#

喜夏-厌秋 提交于 2020-08-24 21:52:05
问题 I have a function in C++ that I exported to a DLL. I contains a struct pointer as one of the parameters. I need to use this function in C#, so I used DLLImport for the function and recreated the struct in C# using StructLayout. I've tried passing in the parameter using ref as well as tried Marshaling it in using MarshalAs(UnmangedType.Struct) and Marshal.PtrToStructure. The parameter still isn't passing correctly. Example: [DllImport("testdll.dll")] public static extern int getProduct(int

JSON unmarshal integer field into a string

梦想的初衷 提交于 2020-08-21 06:47:29
问题 I am struggling with deserializing a integer into a string struct field. The struct field is a string and is expected to be assignable from users of my library. That's why I want it to be a string, since for the purpose of writing it to the database I actually don't care about the value inside. The users can supply text, but some just assign integers. Consider this struct: type Test struct { Foo string } Sometimes I end up with a JSON value that is valid but won't deserialize into the struct

JSON unmarshal integer field into a string

对着背影说爱祢 提交于 2020-08-21 06:46:34
问题 I am struggling with deserializing a integer into a string struct field. The struct field is a string and is expected to be assignable from users of my library. That's why I want it to be a string, since for the purpose of writing it to the database I actually don't care about the value inside. The users can supply text, but some just assign integers. Consider this struct: type Test struct { Foo string } Sometimes I end up with a JSON value that is valid but won't deserialize into the struct

Is the omission of a forward declaration for a pointer to a structure valid? [duplicate]

喜夏-厌秋 提交于 2020-08-20 12:13:10
问题 This question already has answers here : How is it legal to reference an undefined type inside a structure? (7 answers) Closed last month . I recently came across this comment by @Paul Ogilvie: "You say "To define a pointer to a structure you only need to know the structure tag". In my experience that is unnecessary. Just declare a pointer_to_some_type and the compiler will reserve space for a pointer and does type checking on assignment. Only once you want dereference the pointer and access

Is the omission of a forward declaration for a pointer to a structure valid? [duplicate]

不羁岁月 提交于 2020-08-20 12:09:49
问题 This question already has answers here : How is it legal to reference an undefined type inside a structure? (7 answers) Closed last month . I recently came across this comment by @Paul Ogilvie: "You say "To define a pointer to a structure you only need to know the structure tag". In my experience that is unnecessary. Just declare a pointer_to_some_type and the compiler will reserve space for a pointer and does type checking on assignment. Only once you want dereference the pointer and access

Is the omission of a forward declaration for a pointer to a structure valid? [duplicate]

十年热恋 提交于 2020-08-20 12:09:28
问题 This question already has answers here : How is it legal to reference an undefined type inside a structure? (7 answers) Closed last month . I recently came across this comment by @Paul Ogilvie: "You say "To define a pointer to a structure you only need to know the structure tag". In my experience that is unnecessary. Just declare a pointer_to_some_type and the compiler will reserve space for a pointer and does type checking on assignment. Only once you want dereference the pointer and access

Sort the fields in a struct based on value

萝らか妹 提交于 2020-08-20 06:16:18
问题 Say I have a struct: MyStruct.a = 12; MyStruct.b = 22; MyStruct.c = 32; Can I modify it so that the fields are ordered based on their value: MyStruct c: 32 b: 22 a: 12 The orderfields methods allow ordering of the struct based on the field name or other structures/cell arrays, but not by value. 回答1: % Define initial structure: myStruct.a = 12; myStruct.b = 22; myStruct.c = 32; % Find desired order of values, rather than fieldnames: [ ~,sortIdx ] = sort( structfun( @(x) x, myStruct ), 'descend

Sort the fields in a struct based on value

狂风中的少年 提交于 2020-08-20 06:16:07
问题 Say I have a struct: MyStruct.a = 12; MyStruct.b = 22; MyStruct.c = 32; Can I modify it so that the fields are ordered based on their value: MyStruct c: 32 b: 22 a: 12 The orderfields methods allow ordering of the struct based on the field name or other structures/cell arrays, but not by value. 回答1: % Define initial structure: myStruct.a = 12; myStruct.b = 22; myStruct.c = 32; % Find desired order of values, rather than fieldnames: [ ~,sortIdx ] = sort( structfun( @(x) x, myStruct ), 'descend

Empty output while trying to convert a yaml data into a struct [duplicate]

这一生的挚爱 提交于 2020-08-15 18:12:16
问题 This question already has answers here : Go Unmarshaling YAML into struct (2 answers) Closed 2 years ago . Im trying to convert a yaml data into a struct and print it. The output I get for this program is empty. package main import ( "fmt" "gopkg.in/yaml.v2" ) type example struct { variable1 string variable2 string } func main() { var a example yaml.Unmarshal([]byte("variable1: asd\nvariable2: sdcs"), &a) fmt.Println(a.variable1) } 回答1: The documentation for Unmarshal states that Struct

Custom Struct: Type does not conform to protocol 'Decodable'

|▌冷眼眸甩不掉的悲伤 提交于 2020-08-10 23:16:35
问题 I would like to be able to save a Custom-struct to UserDefaults but for that I need it to be Codable .. I tried it like this: struct Wishlist: Codable { var name: String var image: UIImage var wishData: [Wish] var color: UIColor var textColor: UIColor var index: Int } But that gives me this error : Type 'Wishlist' does not conform to protocol 'Decodable' Here is my Class Wish , maybe that's where the problem is: class Wish: NSObject { public var wishName : String? public var checkedStatus :