casting

Float divison and casting in Swift

左心房为你撑大大i 提交于 2019-12-22 03:36:46
问题 I'm trying to learn Swift and I made a simple average function: func average(numbers: Int...) -> Float { var sum = 0 for number in numbers { sum += number } return Float(sum)/Float(numbers.count) } average(1,2,3,4,5,6) This gives me the correct result: 3.5 However, I am wondering why I have to cast both sum and numbers.count to floats. I tried casting this way: return Float(sum/numbers.count) but it gives me just 3.0 回答1: First, you should use Double and not Float. Float gives you very

Objective C - Type Casting From NSString to Int

China☆狼群 提交于 2019-12-22 02:05:13
问题 I have this bit of Objective C code, where I am casting a NSString to an int : NSString *a=@"123abc"; NSInteger b=(int) a; NSLog(@"b: %d",b); And the NSLog produces this output: b: 18396 Can anyone explain to me why this is happening? I was under the impression type casting a string to an integer would get the numerical value from the string. 回答1: You've got integer value of pointer to NSString object there. To parse string to integer you should do: NSString *a = @"123abc"; NSInteger b = [a

Type-casting to boolean

*爱你&永不变心* 提交于 2019-12-22 01:55:11
问题 Can someone explain me why this: var_dump((bool) 1==2); returns bool(true) but var_dump(1==2); returns bool(false) Of course the second return is correct, but why in the first occasion php returns an unexpected value? 回答1: It's actually not as strange it seems. (bool) has higher precedence than ==, so this: var_dump((bool) 1==2); is equivalent to this: var_dump( ((bool) 1) == 2); or this: var_dump(true == 2); Due to type juggling, the 2 also essentially gets cast to bool (since this is a

Is static_cast misused?

纵然是瞬间 提交于 2019-12-22 01:53:54
问题 I have mixed feelings about static_cast , as it is the safest C++ cast available, but allows both safe and unsafe conversions at the same time, so you have to know the context to say if it is actually safe or might lead to UB (e.g. when casting to a sub-class). So why isn't there a safer explicit cast? Here is an example, where it could be useful. In COM, they have to return the interface pointer as void** ppv , so "have to" cast explicitely *ppv = (IInterface*) this; which was then suggested

Convert linq query to string array - C#

﹥>﹥吖頭↗ 提交于 2019-12-22 01:53:23
问题 What is the most efficient way of converting a single column linq query to a string array? private string[] WordList() { DataContext db = new DataContext(); var list = from x in db.Words orderby x.Word ascending select new { x.Word }; // return string array here } Note - x.Word is a string 回答1: I prefer the lambda style, and you really ought to be disposing your data context. private string[] WordList() { using (DataContext db = new DataContext()) { return db.Words.Select( x => x.Word )

Unexpected type safety violation

醉酒当歌 提交于 2019-12-22 01:50:27
问题 In the following code, a dowcast to an apparently incompatible type passes compilation: public class Item { List<Item> items() { return asList(new Item()); } Item m = (Item) items(); } Item and List<Item> are disparate types so the cast can never succeed. Why did the compiler allow this? 回答1: A List<Item> could very well be an Item. See for example: public class Foo extends Item implements List<Item> { // implement required methods } A cast tells the compiler: "I know you can't be sure that

How do I convert a single char to a string?

荒凉一梦 提交于 2019-12-22 01:49:41
问题 I'd like to enumerate a string and instead of it returning chars I'd like to have the iterative variable be of type string . This probably isn't possible to have the iterative type be a string so what is the most efficient way to iterate through this string? Do I need to create a new string object with each iteration of the loop or can I perform a cast somehow? String myString = "Hello, World"; foreach (Char c in myString) { // what I want to do in here is get a string representation of c //

Unexpected type safety violation

夙愿已清 提交于 2019-12-22 01:49:34
问题 In the following code, a dowcast to an apparently incompatible type passes compilation: public class Item { List<Item> items() { return asList(new Item()); } Item m = (Item) items(); } Item and List<Item> are disparate types so the cast can never succeed. Why did the compiler allow this? 回答1: A List<Item> could very well be an Item. See for example: public class Foo extends Item implements List<Item> { // implement required methods } A cast tells the compiler: "I know you can't be sure that

casting new System.Windows.Forms.Control object to System.Windows.Forms.Textbox

和自甴很熟 提交于 2019-12-22 01:26:11
问题 i get an InvalidArgumentException while casting Control to System.Windows.Forms.Textbox: Unable to cast object of type 'System.Windows.Forms.Control' to type 'System.Windows.Forms.TextBox'. System.Windows.Forms.Control control = new System.Windows.Forms.Control(); control.Width = currentField.Width; //here comes the error ((System.Windows.Forms.TextBox)control).Text = currentField.Name; I am doing this, because I have different Controls (Textbox, MaskedTextbox, Datetimepicker...), which will

Android postDelayed works but can't put it in a loop?

匆匆过客 提交于 2019-12-22 00:13:22
问题 Sorry I am a noob I've read countless tutorials about making a simple timer and was wondering why it doesn't work until I noticed it is the while-loop causing the issue o.O I have removed it and then it works but only 1 time I need to use the loop though so the movement finishes :C Heres the code: old_x is the coordinate from an ImageView and new_x from the onTouch Event, maybe the problem because I am casting them as an int? I don't know what I need to do so it works please help O: while(old