literals

Type '[String, AnyObject?]' does not conform to protocol AnyObject?: why?

梦想的初衷 提交于 2020-01-01 02:27:05
问题 I'm trying to understand why I have a compilation error: Type '[String, AnyObject?]' does not conform to protocol AnyObject? var cars: [String: AnyObject?] = [ "model": "Ferrari"] var JSON: [String: AnyObject?] = [ "cars": cars ] // <-- error It seems like cars is not an AnyObject? which I don't understand. Thanks for your help! 回答1: AnyObject must be a class, but Dictionary is a structure. The Swift Programming Language: Type Casting AnyObject can represent an instance of any class type. The

In C# are the terms “Primitive” and “Literal” interchangeable?

时光毁灭记忆、已成空白 提交于 2020-01-01 02:15:13
问题 A discussion earlier today led me to question whether or not my understanding of primtives and literals is correct. My understanding is that a literal type is specifically a type which can have a value assigned using a notation that both human and compiler can understand without specific type declarations: var firstName = "John"; // "John" is literal var firstName = (string)"John"; // *if* the compiler didn't understand that "John" // was a literal representation of a string then I // would

What does “string literal in condition” mean?

独自空忆成欢 提交于 2019-12-30 20:52:33
问题 Whenever I try to run the program, an error pops up saying "string literal in condition (on line 10)". What am I doing wrong? puts "Welcome to the best calculator there is. Would you like to (a) calculate the area of a geometric shape or (b) calculate the equation of a parabola? Please enter an 'a' or a 'b' to get started." response = gets.chomp if response == "a" or "A" puts "ok." elsif response == "b" or "B" puts "awesome." else puts "I'm sorry. I did not get that. Please try again." end

Is '\u0B95' a multicharacter literal?

你离开我真会死。 提交于 2019-12-30 08:12:01
问题 In a previous answer I gave, I responded to the following warning being caused by the fact that '\u0B95' requires three bytes and so is a multicharacter literal : warning: multi-character character constant [-Wmultichar] But actually, I don't think I'm right and I don't think gcc is either. The standard states: An ordinary character literal that contains more than one c-char is a multicharacter literal . One production rule for c-char is a universal-character-name (i.e. \uXXXX or \UXXXXXXXX )

Why is it possible to assign a const char* to a char*?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-30 05:47:08
问题 I know that for example "hello" is of type const char* . So my questions are: How can we assign a literal string like "hello" to a non- const char* like this: char* s = "hello"; // "hello" is type of const char* and s is char* // and we know that conversion from const char* to // char* is invalid Is a literal string like "hello" , which will take memory in all my program, or it's just like temporary variable that will get destroyed when the statement ends? 回答1: In fact, "hello" is of type

In JavaScript, why is [ ] preferred over new Array();?

狂风中的少年 提交于 2019-12-29 06:39:45
问题 I remember reading somewhere (I think it was in one of Crockford's papers) that using an array literal [] is better than using the new Array(); notation. But I can't really remember any advantages of one over the other. Can anyone please explain to me on why the former is preferred over the latter? Here is one reason I can think of on why [] is better than new Array(); : var Array = function () { }; Overriding the Array object will break code...! Any more reasons? 回答1: Brevity It has less

Why int j = 012 giving output 10?

 ̄綄美尐妖づ 提交于 2019-12-28 04:24:05
问题 In my actual project It happened accidentally here is my modified small program. I can't figure out why it is giving output 10 ? public class Int { public static void main(String args[]) { int j=012;//accidentaly i put zero System.out.println(j);// prints 10?? } } After that, I put two zeros still giving output 10. Then I change 012 to 0123 and now it is giving output 83? Can anyone explain why? 回答1: Than I change 012 to 0123 and now it is giving output 83? Because, it's taken as octal base

Should we generally use float literals for floats instead of the simpler double literals?

≯℡__Kan透↙ 提交于 2019-12-28 02:44:29
问题 In C++ (or maybe only our compilers VC8 and VC10) 3.14 is a double literal and 3.14f is a float literal. Now I have a colleague that stated: We should use float-literals for float calculations and double-literals for double calculations as this could have an impact on the precision of a calculation when constants are used in a calcualtion. Specifically, I think he meant: double d1, d2; float f1, f2; ... init and stuff ... f1 = 3.1415 * f2; f1 = 3.1415f * f2; // any difference? d1 = 3.1415 *

Why are string literals const?

烈酒焚心 提交于 2019-12-28 00:50:34
问题 It is known that in C++ string literals are immutable and the result of modifying a string literal is undefined. For example char * str = "Hello!"; str[1] = 'a'; This will bring to an undefined behavior. Besides that string literals are placed in static memory. So they exists during whole program. I would like to know why do string literals have such properties. 回答1: There are a couple of different reasons. One is to allow storing string literals in read-only memory (as others have already

Best way to convert string to array of object in javascript?

半城伤御伤魂 提交于 2019-12-27 14:42:25
问题 I want to convert below string to an array in javascript. {a:12, b:c, foo:bar} How do I convert this string into array of objects? Any cool idea? 回答1: I think that the best way of doing this, as Douglas Crockford (one of the biggests gurus of JavaScript) suggests in here is using the JSON native parser, as it is not only faster than the eval(), it's also more secure. Native JSON parser is already available in: Firefox 3.5+ IE 8+ Opera 10.5+ Safari Safari 4.0.3+ Chrome (don't know which