designated-initializer

Non-designated initialiser inheritance from Objective C classes

让人想犯罪 __ 提交于 2019-12-06 11:59:16
Having come across problems when sub-classing UIKit classes and adding immutable variables to them, I made a test project to figure out what was going on. My conclusion is that if: we have an Objective C class, which inherits from another class, with its own designated initialiser (implicit or explicitly annotated) in its initialiser , it calls [self initWithXX] where initWithXX is an init method on the superclass we subclass this class in Swift , adding an immutable property (which obviously must be initialised on instantiation) we implement a single designated initialiser for this Swift

CTAD and designated initializers in C++20

回眸只為那壹抹淺笑 提交于 2019-12-05 05:36:25
I have already stated confusion about CTAD with designated initializers in this question , but i have another confusion with a very similar code snippet template <typename int_t=int, typename float_t=float> struct my_pair { int_t first; float_t second; }; template<typename ... ts> my_pair(ts...) -> my_pair<ts...>; int main() { my_pair x{.second = 20.f}; static_assert( std::is_same_v<decltype(x.first), int> ); //FAILS <- its deduced to float static_assert( std::is_same_v<decltype(x.second), float> ); } It seems like the deduction guide causes the type of first to be deduced to float , even

Cryptic struct definition in C

人盡茶涼 提交于 2019-12-05 01:20:59
I came across the following maze definition code: typedef struct mazeNode { int hasCheese; int tag; struct mazeNode *left; struct mazeNode *right; } maze_t; maze_t maze = { .tag = 1, .left = &(maze_t) { .left = &(maze_t) { .left = &(maze_t) {}, .right = &(maze_t) {} }, .right = &(maze_t) { .right = &(maze_t) {} } }, .right = &(maze_t) { .tag = 8, .left = &(maze_t) {}, .right = &(maze_t) { .tag = 10, .left = &(maze_t) { .tag = 11, .left = &(maze_t) { .hasCheese = 1, .tag = 12 } }, .right = &(maze_t) {} } } }; From the linked blog post I understand that they are trying to define the binary tree

Adding NSCoding as an Extension

房东的猫 提交于 2019-12-03 16:54:20
问题 I'd like to extend a framework class ( I don't want to edit the source code directly ), and make it conform to NSCoding . Basically, here's a simplification of the situation I'm in : /* Can't be edited. */ class Car: NSObject { var color: String? } /* Can be edited */ extension Car: NSCoding { init(coder aDecoder: NSCoder) { } func encodeWithCoder(aCoder: NSCoder) { } } The issue is init(coder aDecoder: NSCoder) is, as per the header file, a designated initializer ( isn't this weird though ?

Strange values while initializing array using designated initializers

自古美人都是妖i 提交于 2019-12-03 16:30:07
问题 When I initialize the array below all the output looks ok except for values[3] . For some reason values[3] initialized as values[0]+values[5] is outputting a very large number. My guess is that I am trying to assign values[0]+values[5] before they are properly stored in memory but if someone could explain that would be great. int main (void) { int values[10] = { [0]=197,[2]=-100,[5]=350, [3]=values[0] + values[5], [9]= values[5]/10 }; int index; for (index=0; index<10; index++) printf("values

Override designated initializer of superclass

不想你离开。 提交于 2019-12-03 13:16:11
I am reading a book which has a guideline: "If a class declares a designated initializer that is different from its superclass, the superclass’s designated initializer must be overridden to call the new designated initializer" As I understand this guideline in other words is that, if I am subclassing my class form its superclass, and my subclass has a designated initializer which is different from des. initializer of its superclass, then in my subclass I must override the designated initializer of my superclass and inside it call the designated initializer of my subclass. Is this true? Do we

What's the difference between a required initializer and a designated initializer?

℡╲_俬逩灬. 提交于 2019-12-03 12:13:19
问题 I was creating my own custom tableViewCell and then I got an error saying: 'required' initializer 'init(coder:)' must be provided by subclass of 'UITableViewCell' I looked it up and obviously it's a must to implement that as well. But this led to my confusion about required vs. designated initializers Apple Docs says: Required Initializers: Write the required modifier before the definition of a class initializer to indicate that every subclass of the class must implement that initializer:

Using Designated Initializers with the Heap

穿精又带淫゛_ 提交于 2019-12-02 01:10:22
One can use designated initializers as shown below (for "billy") without issue, but when the same initialization approach is used on dynamic memory things will break at compile-time. What are the restrictions for using designated initializers? Aside from where (i.e. the address) to which we are writing, what makes these two initializations different? Why can we not use designated initializers with dynamic memory? struct student{ char *name; int age; }; void print_student(struct student* st){ printf("Student: %s is %d years old\n", st->name, st->age); } int main(void) { srand(time(NULL));

C99 Structure Designated Initialisers and other value

给你一囗甜甜゛ 提交于 2019-12-01 15:08:42
I am aware that in C99 you can initialize members of the structure using member name as follows : struct myStruct { int i; char c; float f; }; So following is valid : struct myStruct m = {.f = 10.11, .i = 5, .c = 'a'}; Also it is said that uninitialised members will be set to 0 . So struct myStruct m = {.f = 10.11, .c = 'a'}; here i will be set to 0 But, for the following : struct myStruct m = {.f = 10.11, .c = 'a', 6}; i is still initialized to 0. What is the reason if we do such compound initialization. This is covered in the draft C99 standard section 6.7.8 Initialization , basically if the

C99 Structure Designated Initialisers and other value

匆匆过客 提交于 2019-12-01 14:00:03
问题 I am aware that in C99 you can initialize members of the structure using member name as follows : struct myStruct { int i; char c; float f; }; So following is valid : struct myStruct m = {.f = 10.11, .i = 5, .c = 'a'}; Also it is said that uninitialised members will be set to 0 . So struct myStruct m = {.f = 10.11, .c = 'a'}; here i will be set to 0 But, for the following : struct myStruct m = {.f = 10.11, .c = 'a', 6}; i is still initialized to 0. What is the reason if we do such compound