Syntax for Swift 2 for directions step loop - swift

Can anyone help with the syntax for this loop in Swift 2. The code is from a very useful tutorial ( http://www.devfright.com/category/map-kit-framework/mkdirectionsrequest/) which covers the route directions but it's in Objective C and I know that a number of elements are now deprecated, such as ++. I'm having problems converting the first two lines of code to Swift 2. Any help much appreciated.
for (int i = 0; i < routeDetails.steps.count; i++) {
MKRouteStep *step = [routeDetails.steps objectAtIndex:i];
NSString *newStep = step.instructions;
self.allSteps = [self.allSteps stringByAppendingString:newStep];
self.allSteps = [self.allSteps stringByAppendingString:#"\n\n"];
self.steps.text = self.allSteps;
}

If you don't need the index explicitly, forget index loops.
for step in routeDetails.steps {
let newStep = step.instructions
allSteps += "\(newStep)\n\n"
steps.text = allSteps
}
or still swiftier
allSteps += routeDetails.steps.map({$0.instructions}).joinWithSeparator("\n\n")
steps.text = allSteps

In Swift 2:
for i in 0..< routeDetails.steps.count {
var step: MKRouteStep = routeDetails.steps[i]
var newStep: String = step.instructions
self.allSteps = self.allSteps.stringByAppendingString(newStep)
self.allSteps = self.allSteps.stringByAppendingString("\n\n")
self.steps.text = self.allSteps
}

For anyone else using the route.step instructions this is how I placed it in a text field on a map. Many thanks to Vardian for solving the problem.
self.allSteps += route.steps.map({$0.instructions}).joinWithSeparator("\n\n")
self.directionsText.text = self.allSteps

Related

Dart remove first comma from string if exists

I have a string of numbers separated by commas. I would like to remove the first and last comma if it exists.
//qlist = ',6063,6077,6051,5925,6069,6054,6071,6049,5903,5858,5760,';
if (qlist.substring(qlist.length - 1) == ','){
qlist= qlist.substring(0,qlist.length - 1);
}
if (qlist.substring(0) == ','){
qlist= qlist.substring(1,qlist.length);
}
Please suggest the right approach.
You could do the following:
int startSlice = 0;
int endSlice = myString.length;
if (myString.startsWith(‘,’))
startSlice = 1;
if (myString.endsWith(‘,’))
endSlice -= 1;
myString = myString.subString(startSlice, endSlice);

how to concat each element in array by another single string value in swift

I am trying to concatenate multiple element in array with another string using swift 5:
ex:
MyArray = ["Bat","Foot","Basket"]
MyString = "Ball"
output = ["BatBall","FootBall","BasketBall"]
Or
output = ["BallBat","BallFoot","BallBasket"]
How should I do this? Please help.
you can achieve this by map functionality :
var myArray = ["Bat","Foot","Basket"]
var output = myArray.map { $0 + "ball" }
print(output) // ["Batball", "Football", "Basketball"]
if you want to do this in a complete simple way, you can do this:
var myArray = ["Bat","Foot","Basket"]
var myString = "Ball"
for i in myArray.indices {
myArray[i] = "\(myString)\(myArray[i])"
print(myArray[i])
}
This way you can add any kind of data, every where you want.

How to identify the type of an object?

Here is my JSON response for a particular API.
Case 1
ChallengeConfiguration = {
AnswerAttemptsAllowed = 0;
ApplicantChallengeId = 872934636;
ApplicantId = 30320480;
CorrectAnswersNeeded = 0;
MultiChoiceQuestion = (
{
FullQuestionText = "From the following list, select one of your current or previous employers.";
QuestionId = 35666244;
SequenceNumber = 1;
},
{
FullQuestionText = "What color is/was your 2010 Pontiac Grand Prix?";
QuestionId = 35666246;
SequenceNumber = 2;
}
)
}
The key "MultiChoiceQuestion" returns an array with two questions. So here is my code.
let QuestionArray:NSArray = dict1.objectForKey("ChallengeConfiguration")?.objectForKey("MultiChoiceQuestion") as! NSArray
Case 2
ChallengeConfiguration =
{
AnswerAttemptsAllowed = 0;
ApplicantChallengeId = 872934636;
ApplicantId = 30320480;
CorrectAnswersNeeded = 0;
MultiChoiceQuestion = {
FullQuestionText = "From the following list, select one of your
current or previous employers.";
QuestionId = 35666244;
SequenceNumber = 1;
}
}
For Case 2 my code does not work and app crashes because it returns a dictionary for that specific Key. So how could I write a generic code that would work for all objects?
It looks like the key can contain either an array of dictionary values or a dictionary, so you just need to try casting to see which one you have.
so I would likely do it like this:
if let arr = dict1.objectForKey("ChallengeConfiguration")?.objectForKey("MultiChoiceQuestion") as? Array {
// parse multiple items as an array
} else if let arr = dict1.objectForKey("ChallengeConfiguration")?.objectForKey("MultiChoiceQuestion") as? [String:AnyObject] {
// parse single item from dictionary
}
You should never really use ! to force unwrap something unless you are completely certain that the value exists and is of the type you are expecting.
Use conditional logic here to test the response and parse it safely so that your app doesn't crash, even in failure.

Using multiple outlets in swift

func lampPressed(Rij: Int, Kolom: Int){
for var i = 1; i < 3; i++ {
if status[Rij][i] == "aan" {
rij\(Rij)kolom\(i)LBL.text = "Uit"
}else{
}
}
}
I want a easy way to switch between 9 different labelOutlets.
Is there a way to make this happen.
Or else an other way to make this faster
Update:
I now have put all my button in a collections:
collectionOfButtons[(Rij-1)*3+i].titleLabel?.text = "uit"
and made the fund got from 1 to 3
SaiCyli had the right answer.
I have just made a 3x3 array.
For the index:
index = row * 3 + column

Weird value from NSDictionary

I got a NSDictionary that when I do a [currentOrder debugDescription] call on it the layout is below, when I then do a:
[currentOrder valueForKey: #"itemOrder"]
It return it funny with the brackets as:
(
"4 X ESPRESSO"
)
where it should only be (without brackets):
4 X EXPRESSO
Any idea why?
Content of currentOrder:
currentOrder: <__NSArrayM 0x68426c0>(
{
extra1Select = 0;
extra2Select = 0;
extra3Select = 0;
itemCost = 58;
itemOrder = "4 X ESPRESSO";
itemOrderDescription = "Cookie: YES, Sugar: YES";
itemQuantity = 4;
itemRestaurant = VidaECaffe;
plistItem = {
cost = "11.5";
description = "R11.50";
extra1 = {
desc = Cookie;
details = (
{
cost = "3.00";
option1 = YES;
},
{
cost = "0.00";
option2 = NO;
}
);
};
extra2 = {
desc = Sugar;
details = (
{
cost = "0.00";
option1 = YES;
},
{
cost = "0.00";
option2 = NO;
}
);
};
itemRestaurant = VidaECaffe;
level = 1;
title = ESPRESSO;
};
}
)
The problem is that currentOrder is not a dictionary but a NSArray containing a dictionary. The failure you are making next is that you use valueForKey: which is part of the key value coding family and not the designated access method for dictionaries (which is objectForKey:), and the array returns you a filtered array as a result...
When you print out the value of an array or dictionary in the debugger, the debugger includes the extra brackets, braces, and whatnot so you can see the layout of the structure you are asking about.
If the debugger shows the contents of an array something like
(
"Foobar"
)
it's telling you that the array has one element, a string with the value Foobar.
Put entirely another way: Read up on what the debugger does when it prints out values. Lots of resources here and on the web for this. For example: Debugging with GDB: Introduction to Commands, Print and Print-Object