English 中文(简体)
为何为C Structs 工作,
原标题:Why {} works for C Structs but not for properties

前几天我遇到一些东西, 用来用卷曲牙套来确定C Structs的价值, 效果很好, 但我想知道为什么我不能用它来设定属性的价值?

//This works:
CGPoint pt = {10, 20};
CGRect rct = {0, 5, 50, 50};

//But this doesn t:
self.frame = {0, 5, 50, 50};
imageview.center = {100, 120};

编译器错误为“ 期望表达式”

我想我知道为什么这不起作用,但我希望得到一个更彻底的解释。

最佳回答

第一行将变量的宣布和初始化结合起来。汇编者从左表达式的类型中推断出右表达式的类型,并按预期在阵列槽中分配数字。

最后几行不是初始化。 现在编译者会做任何推论。 因此, 编译者不知道该如何解释右边的东西。 它是什么类型, 如何在记忆中排版这些数字? 编译者不会假设它的 CGrect 或 CGPoint, 您必须帮助他用一个清晰的投影( CGRect) { / 5, 50, 50} 给他缺少的信息 。

问题回答

这项工作应:

self.frame = (CGRect){0, 5, 50, 50};
imageview.center = (CGPoint){100, 120};




相关问题
Nested struct variable initialization

How can I initialize this nested struct in C? typedef struct _s0 { int size; double * elems; }StructInner ; typedef struct _s1 { StructInner a, b, c, d, e; long f; char[16] s; }...

passing primitive or struct type as function argument

I m trying to write some reasonably generic networking code. I have several kinds of packets, each represented by a different struct. The function where all my sending occurs looks like: - (void)...

Error: "Cannot modify the return value" c#

I m using auto-implemented properties. I guess the fastest way to fix following is to declare my own backing variable? public Point Origin { get; set; } Origin.X = 10; // fails with CS1612 Error ...

Sending struct over TCP (C-programming)

I have a client and server program where I want to send an entire struct from the client and then output the struct member "ID" on the server. I have done all the connecting etc and already managed ...

Scheme, getting the pointer from pointed struct

Assume I have a such struct: (define-struct node (value next)) ;and making 2 nodes, parent pointing to child as next. (define child (make-node 2 null)) (define parent (make-node 1 child)) Under ...

Changing a struct inside another struct in a foreach loop

The following code prints (when invoking MyMethod): 0 0 0 1 I would expect it to print: 0 0 1 1 Why is this? Code: private struct MyStruct { public MyInnerStruct innerStruct; } private ...

热门标签