problem with UISegmentedControl - iphone

-(IBAction)changeSegmentDistance:(UISegmentedControl *)sender{
// refineDistance=sender;
switch ([refineDistance selectedSegmentIndex]) {
case 0:
valueString=5;
NSLog(#"value String %d",valueString);
break;
case 1:
valueString=10;
NSLog(#"value Stringaaaa %d",valueString);
case 2:
valueString=15;
NSLog(#"value String %d",valueString);
break;
case 3:
valueString=16;
NSLog(#"value String %d",valueString);
break;
default:
break;
}
}
When i running application and printing on Console
using NSLog...
when i select 0 its printing 5...
when i selected 1 its printing 10 and 15
when i selected 2 its printing 15
when i selected 3 its printed 16..
I dont know why its printing 10 and 15 when i selected 2nd.

You're missing a "break;" at the end of your "case 1" statement block. As such, execution is continuting into the next case statement.

You are missing a break for case 1.

Related

What's the best use case of enum value in dart?

I want to know the major use cases with example will be great.
Where to use enum -> common example suppose you want to a show week days name “MONDAY – SUNDAY” in a dropdown menu at that time you know week days data is constant and will never change.
example:
enum day {
sunday,
monday,
tuesday,
wednesday,
thursday,
friday,
saturday,
}
void main() {
// assume you app user will select from Menu Item day
var selectedEnumDay = day.friday;
// Switch-case
switch(selectedEnumDay) {
case day.sunday: print("You selected sunday");
break;
case day.monday: print("You selected monday");
break;
case day.tuesday: print("You selected tuesday");
break;
case day.wednesday: print("You selected wednesday");
break;
case day.thursday: print("You selected thursday");
break;
case day.friday: print("You selected friday");
break;
case day.saturday: print("You selected saturday");
break;
}
}
Enum are a special type of class that allow creating a set of constant values associated with a particular type. [ 1 ]
Enums are great for representing a discrete set of states. [ 2 ]
one of simple example using enum is :
enum RequestState {success, error, notAsked, loading }
...
RequestState apiState = RequestState.notAsked;
Widget buildContaier() {
switch(apiState){
case apiState.loading:
return CircularProgressIndicator();
case apiState.success:
return ListView();
case apiState.error:
return Text("error fetch api");
default:
return Container();
}

Custom Set of Steps in Slider

I have a #StateObject Slider. It is set to slide 1...5 step 1.
I need to have custom step [1,2,5,10,20] instead of step 1.
I have a setup where I have the slider work as an array index to my array of custom step. It is working. But It is jerky. Single slide registers slide value dozen times making my array to get picked up dozen times jerking the process. How can I have custom steps smooth.
I can adjust within the app but if Slider provides the exact number, logic of the app become much cleaner. Less translation, clear the logic.
Slider(value: $tSlider, in: 1...5, step: 1)
// How can I make above like below
Slider(value: $tSlider, in: 1...5, step: [1,2,5,10,20]
// What I have currently
#Published var tSteps:[Int] = [1,2,5,10,20]
#Published var tSlider: Double = 1
var tScale: Int { return tlSteps[Int(tlSlider)-1] }
Setting steps on Slider was a bad idea. Stepper was correct choice.
Stepper("Year Scale", onIncrement: {
switch tVM.tlScale {
case 1: self.tlScale = 2
case 2: self.tlScale = 5
case 5: self.tlScale = 10
case 10: self.tlScale = 20
default: self.tlScale = 20
},
onDecrement: {
switch tVM.tlScale {
case 20: self.tlScale = 10
case 10: self.tlScale = 5
case 5: self.tlScale = 2
case 2: self.tlScale = 1
default: self.tlScale = 1
}
})
referred to : post here and Skipjakk's answer. Strange Behavior of Stepper in SwiftUI

What is wrong with my switch case code on swift

As you see I am new at coding.
I am trying to print the correct week day on switch case.
But i can't.
What is wrong with my code?
var aNumber = Int.random(in: 0...10)
func dayOfTheWeek(day: Int) {
switch dayOfTheWeek {
case ..<2:
print ("Monday")
case ..<3:
print ("Tuesday")
case ..<4:
print ("Wednesday")
case ..<5:
print ("Thursday")
case ..<6:
print ("Friday")
case ..<7:
print ("Saturday")
case ..<8:
print ("Sunday")
default:
print("Error")
}
print(aNumber)
}
dayOfTheWeek(day: aNumber)
You need to switch on the parameter day (instead of the function name), and you don't need to match ranges like ..<2 only single numbers:
switch day {
case 2:
print ("Monday")
case 3:
print ("Tuesday")
// and so on...
dayOfTheWeek is the function, while day is the Int.
Therefore, you have to switch through the Integer.
Try switch day {...}.

Switch case with multiple values for the same case

I would like to know the syntax to set a multiple case statement in a switch / case.
For example :
String commentMark(int mark) {
switch (mark) {
case 0 : // Enter this block if mark == 0
return "Well that's bad" ;
case 1, 2, 3 : // Enter this block if mark == 1 or mark == 2 or mark == 3
return "Gods what happend" ;
// etc.
default :
return "At least you tried" ;
}
}
I cannot find the right syntax to set multiple case (the line case 1, 2, 3 :), is it even possible in Dart ?
I did not found any informations on pub.dev documentation, neither on dart.dev.
I tried :
case 1, 2, 3
case (1, 2, 3)
case (1 ; 2 ; 3)
case (1 : 2 : 3)
case 1 : 3
and more !
Execution continues until it reaches a break;. Therefore, you can list cases one after the other to get the following code execute on either one of those cases.
String commentMark(int mark) {
switch (mark) {
case 0 : // Enter this block if mark == 0
return "mark is 0" ;
case 1:
case 2:
case 3: // Enter this block if mark == 1 or mark == 2 or mark == 3
return "mark is either 1, 2 or 3" ;
// etc.
default :
return "mark is not 0, 1, 2 or 3" ;
}
}
The return statements above serve to get out of the function. If you do not want to return, you have to use break; after each block, of course. This code below is equivalent to the one above.
String commentMark(int mark) {
String msg;
switch (mark) {
case 0 : // Enter this block if mark == 0
msg = "mark is 0" ;
break;
case 1:
case 2:
case 3: // Enter this block if mark == 1 or mark == 2 or mark == 3
msg = "mark is either 1, 2 or 3" ;
break;
// etc.
default:
msg = "mark is not 0, 1, 2 or 3" ;
break; // this is a good habit, in case you change default to something else later.
}
return msg;
}
Instead of multiple case we can use or operator in single switch case it self.
switch (date) {
case 1 | 21 | 31:
return "st";
case 2 | 22:
return "nd";
case 3 | 23:
return "rd";
default:
return "th";
}

objective-c switch case with multiple arguments

Probably a basic question but I would like to reduce some code using multiple arguments on switch case statements. Possible? Correct syntax?
switch (myInteger){
case (1): //here I would like to apply multiple arguments as case (1 || 3 || 5)
<#statements#>
break;
case (2):
<#statements#>
break;
default:
break;
You can use multiple cases right below each other.
switch (myInteger) {
case 1:
case 3:
case 5:
// statements
break;
case 2:
// statements
break;
default:
// statements
break;
}
case 1:
case 3:
case 5:
statements;
break;
case 2:
statements;
break;
default:
break;
For Swift 3 there is a modification that I would like to mention
switch some value to consider {
case 1: //single argument
print("ABC")
case 2,3: // multiple arguments
print("KLM")
default:
print("XYZ")
}
Hope it helps you. Thanks
Switch case must declare inside the main method
SYNTAX
Switch (variable r expression)
{
Case 1 :
Body ;
Break
Case 2 :
Body;
Break;
Default :
Body ;
Break;
}