designated-initializer

Is there a Way to Get Warned about Misbehaving Designated Initializers?

落花浮王杯 提交于 2019-12-01 08:38:19
C99 introduced the concept of designated intializers for structs. So for example, given: typedef struct { int c; char a; float b; } X; I could initialize like: X foo = {.a = '\1', .b = 2.0F, .c = 4}; and calling: printf("c = %d\na = %hhu\nb = %f", foo.c, foo.a, foo.b); would output: c = 4 a = 1 b = 2.000000 As mentioned here this has the "surprising behavior" of assigning to c then a then b , independent of the order of my designated initializers. This becomes a real issue if I have functions like this: int i = 0; int f() { return ++i; } int g() { i += 2; return i; } int h() { i += 4; return i

Which initializer(s) to override for UITableViewController subclass

百般思念 提交于 2019-11-30 03:54:54
I have a UITableViewController subclass that's instantiated, depending on where it's used, in a NIB or via code. In both cases I want to do customization in the initializer method. Does that mean I need to implement both initWithNibName:bundle: and initWithCoder: , and would each method call its respective super initializer? While I don't need this right now, what if I also want to be able to instantiate the view controller with initWithStyle: ? Would I then need 3 different init methods that replicate the same behavior? It seems like this violates the whole designated initializer convention,

-Wmissing-field-initializer when using designated initializers

时光毁灭记忆、已成空白 提交于 2019-11-29 14:46:41
I'm using GCC 4.6.2 (Mingw) and compiling with -Wextra . I'm getting strange warnings whenever I use designated initializers. For the following code typedef struct { int x; int y; } struct1; typedef struct { int x; int y; } struct2; typedef struct { struct1 s1; struct2 s2[4]; } bug_struct; bug_struct bug_struct1 = { .s1.x = 1, .s1.y = 2, .s2[0].x = 1, .s2[0].y = 2, .s2[1].x = 1, .s2[1].y = 2, .s2[2].x = 1, .s2[2].y = 2, .s2[3].x = 1, .s2[3].y = 2, }; I get warnings bug.c:24:3: warning: missing initializer [-Wmissing-field-initializers] bug.c:24:3: warning: (near initialization for 'bug_struct1

Possible compiler bug in MSVC12 (VS2013) with designated initializer

自古美人都是妖i 提交于 2019-11-29 09:34:21
Using VS2013 Update 2, I've stumbled on some strange error message : // test.c int main(void) { struct foo { int i; float f; }; struct bar { unsigned u; struct foo foo; double d; }; struct foo some_foo = { .i = 1, .f = 2.0 }; struct bar some_bar = { .u = 3, // error C2440 : 'initializing' : cannot convert from 'foo' to 'int' .foo = some_foo, .d = 4.0 }; // Works fine some_bar.foo = some_foo; return 0; } Both GCC and Clang accept it. Am I missing something or does this piece of code exposes a compiler bug ? EDIT : Duplicate: Initializing struct within another struct using designated initializer

Turn off designated initializer checking in Xcode 6

跟風遠走 提交于 2019-11-29 02:27:18
问题 I'm getting the compile error: error: convenience initializer missing a 'self' call to another initializer [-Werror,-Wobjc-designated-initializers] Compile-checked designated initializers might be a good thing, but if I don't want deal with that right now, how can I turn this off? 回答1: Following on from Clay's answer.. Method 3 You might want to suppress the warning on one occurrence, not all of them: #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wobjc-designated

Which initializer(s) to override for UITableViewController subclass

六眼飞鱼酱① 提交于 2019-11-29 01:04:27
问题 I have a UITableViewController subclass that's instantiated, depending on where it's used, in a NIB or via code. In both cases I want to do customization in the initializer method. Does that mean I need to implement both initWithNibName:bundle: and initWithCoder: , and would each method call its respective super initializer? While I don't need this right now, what if I also want to be able to instantiate the view controller with initWithStyle: ? Would I then need 3 different init methods that

What happens to fields not named by a designated initializer?

回眸只為那壹抹淺笑 提交于 2019-11-28 23:27:11
In C99 (and not in C++), it's possible to initialize structs using this syntax: struct info { char name[8+1]; int sz; int typ; }; struct info arr[] = { [0] = { .sz = 20, .name = "abc" }, [9] = { .sz = -1, .name = "" } }; What happens to the unspecified fields? They are zeroed. From the C99 standard §6.7.8 (Initialization)/21, If there are fewer initializers in a brace-enclosed list than there are elements or members of an aggregate, or fewer characters in a string literal used to initialize an array of known size than there are elements in the array, the remainder of the aggregate shall be

How to write init methods of a UIViewController in Swift

浪尽此生 提交于 2019-11-28 16:38:15
问题 I am unable to add init method to the following UIViewController class. I need to write some code in the init method. Do i have to write init(coder) method? Even when I add the coder and decoder methods I still get errors. I also tried using the init method without any parameters but that also does not seem to work. class ViewController: UIViewController { var tap: UITapGestureRecognizer? override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: NSBundle?) { super.init(nibName: nil,

Why are designated initializers not implemented in g++

让人想犯罪 __ 提交于 2019-11-28 09:53:42
Is there any specific reason why has support for designated initializers not been added to g++? Is the reason that C99 standards came late and g++ was developed earlier and later people didn't care about this issue, or there is some inherent difficulty in implementing designated initializers in the grammar of C++? As I noted in a comment, G++ doesn't support C99 standard designated initialisers, but it does support the GNU extension to C90 which allows designated initialisers. So this doesn't work: union value_t { char * v_cp; float v_f; }; union value_t my_val = { .v_f = 3.5f }; But this does

Swift - Must call a designated initializer of the superclass SKSpriteNode error

江枫思渺然 提交于 2019-11-28 06:09:15
This code worked on first XCode 6 Beta, but on latest Beta it's not working and gives such errors Must call a designated initializer of the superclass SKSpriteNode : import SpriteKit class Creature: SKSpriteNode { var isAlive:Bool = false { didSet { self.hidden = !isAlive } } var livingNeighbours:Int = 0 init() { // throws: must call a designated initializer of the superclass SKSpriteNode super.init(imageNamed:"bubble") self.hidden = true } init(texture: SKTexture!) { // throws: must call a designated initializer of the superclass SKSpriteNode super.init(texture: texture) } init(texture: