initializer

Get class name in convenience init Swift 3

可紊 提交于 2019-12-01 18:43:06
问题 I'm trying to implement my own version of convenience init(context moc: NSManagedObjectContext) , the new convenience initialiser on NSManagedObject in iOS 10. Reason being I need to make it compatible with iOS 9. I've come up with this: convenience init(managedObjectContext moc: NSManagedObjectContext) { let name = "\(self)".components(separatedBy: ".").first ?? "" guard let entityDescription = NSEntityDescription.entity(forEntityName: name, in: moc) else { fatalError("Unable to create

Property initialization does not call set for List<T>

一个人想着一个人 提交于 2019-12-01 14:17:10
I've got a scenario I discovered when checking code coverage on a class's properties. When the property is of type List< T >, and an initializer is used, the set method doesn't appear to be called. This isn't true of other types, like strings and ints. Code coverage doesn't show the set call, nor does a breakpoint in set get hit. Example class: public class ContainerClass { public string Text { get; set; } public List<Item> Items { get; set; } } When using an initializer, like below, the set method on Text is called, and registers in code coverage, but the set method on Items does not, and I

Rails initializer that runs *after* routes are loaded?

こ雲淡風輕ζ 提交于 2019-12-01 13:49:20
问题 I want to set a class attribute when my Rails app starts up. It requires inspecting some routes, so the routes need to be loaded before my custom code runs. I am having trouble finding a reliable place to hook in. This works PERFECTLY in the "test" environment: config.after_initialize do Rails.logger.info "#{Rails.application.routes.routes.map(&:path)}" end But it doesn't work in the "development" environment (the routes are empty) For now I seem to have things working in development mode by

Property initialization does not call set for List<T>

蹲街弑〆低调 提交于 2019-12-01 12:14:33
问题 I've got a scenario I discovered when checking code coverage on a class's properties. When the property is of type List< T >, and an initializer is used, the set method doesn't appear to be called. This isn't true of other types, like strings and ints. Code coverage doesn't show the set call, nor does a breakpoint in set get hit. Example class: public class ContainerClass { public string Text { get; set; } public List<Item> Items { get; set; } } When using an initializer, like below, the set

C++ Array Initializers Warnings

北城以北 提交于 2019-12-01 09:33:52
I have declared and initialized a constant char array within a class: class grid { const char test[11] = {'s', 'e', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; My code works, but I keep getting compiler warnings: non static data member initializers only available with C++11 and extended initializer lists only available with C++11 I know that this isn't an issue because I'm compiling to C++11 standard, but I'm curious as to what is pre C++11 about my code. I am hoping someone can give me some insight and suggest what I can do to make this code C++98 "friendly". Also as requested, my compiler

C++11: string(50, 'x') versus string{50, 'x'}

别来无恙 提交于 2019-12-01 04:32:23
问题 As seen on ideone: cout << string(50, 'x'); // xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx cout << string{50, 'x'}; // 2x WAT?? I have figured out that 50 is ASCII '2', so: cout << static_cast<int>('2'); // 50 cout << static_cast<char>(50); // 2 But that's as far as I've got. Does this lead to a solid argument against C++11 initializers? 回答1: When you do string { 50, 'x' } you're essentially initializing the string with a list of characters. On the other hand, string(50, 'x') calls a 2

Is there a way to define variables of two different types in a for loop initializer?

淺唱寂寞╮ 提交于 2019-12-01 02:52:39
问题 You can define 2 variables of the same type in a for loop: int main() { for (int i = 0, j = 0; i < 10; i += 1, j = 2*i) { cout << j << endl; } } But it is illegal to define variables of different types: int main() { for (int i = 0, float j = 0.0; i < 10; i += 1, j = 2*i) { cout << j << endl; } } Is there a way to do this? (I don't need to use i inside the loop, just j .) If you have totally hacked and obscure solution, It's OK for me. In this contrived example I know you could just use double

Cannot access static field within enum initialiser

二次信任 提交于 2019-12-01 00:36:02
In this code I get a compiler error, see comment: public enum Type { CHANGESET("changeset"), NEW_TICKET("newticket"), TICKET_CHANGED("editedticket"), CLOSED_TICKET("closedticket"); private static final Map<String, Type> tracNameMap = new HashMap<String, Type>(); private Type(String name) { tracNameMap.put(name, this); // cannot refer to static field within an initializer } public static Type getByTracName(String tn) { return tracNameMap.get(tracNameMap); } } Is there a way to make this work, getting an enum value from a Map by one of its fields? The map is probably overkill here. Unless you

Cannot access static field within enum initialiser

♀尐吖头ヾ 提交于 2019-11-30 19:29:12
问题 In this code I get a compiler error, see comment: public enum Type { CHANGESET("changeset"), NEW_TICKET("newticket"), TICKET_CHANGED("editedticket"), CLOSED_TICKET("closedticket"); private static final Map<String, Type> tracNameMap = new HashMap<String, Type>(); private Type(String name) { tracNameMap.put(name, this); // cannot refer to static field within an initializer } public static Type getByTracName(String tn) { return tracNameMap.get(tracNameMap); } } Is there a way to make this work,

C# initialiser conditional assignment

孤者浪人 提交于 2019-11-30 17:34:01
In a c# initialiser, I want to not set a property if a condition is false. Something like this: ServerConnection serverConnection = new ServerConnection() { ServerInstance = server, LoginSecure = windowsAuthentication, if (!windowsAuthentication) { Login = user, Password = password } }; It can be done? How? You can't do this; C# initializers are a list of name = value pairs. See here for details: http://msdn.microsoft.com/en-us/library/ms364047(VS.80).aspx#cs3spec_topic5 You'll need to move the if block to the following line. This is not possible in an initializer; you need to make a separate