I have two strings :
let input=Console.ReadLine()
let check=""
i am looking for a function that can concat this two strings. What should I do ?
Use the + operator:
let both = input + check
Related
I am stuck at a piece of code where I need to get the expression result based on the passed dictionary. For example:
expressionString = "(val1 + val2)"
How it is possible to pass these values from an dictionary or somewhere else. I searched many places but does't work for me.
let expressionString : String = "(val1 + val2)" as String
let expression = NSExpression(format: expressionString)
In above case I would like to know how I can map these val1 and val2 data by passing any argument.
Is it can be achieve by let ex = NSExpression(format: expressionString, argumentArray:[parameter])
?
i wrote code to get character when user enter in text field and do math with them
this :
#IBOutlet weak internal var textMeli: UITextField!
var myChar = textMeli.text
var numb = [myChar[0]*3 , myChar[1]*7]
but one is wrong
textMeli.text is a String.
myChar is a String.
You can't access a Character from a String using bracket notation.
Take a look at the documentation for the String structure.
You'll see that you can access the string's characters through the characters property. This will return a collection of Characters. Initalize a new array with the collection and you can then use bracket notation.
let string = "Foo"
let character = Array(string.characters)[0]
character will be of type Character.
You'll then need to convert the Character to some sort of number type (Float, Int, Double, etc.) to use multiplication.
Type is important in programming. Make sure you are keeping track so you know what function and properties you can use.
Off the soap box. It looks like your trying to take a string and convert it into a number. I would skip the steps of using characters. Have two text fields, one to accept the first number (as a String) and the other to accept the second number (as a String). Use a number formatter to convert your string to a number. A number formatter will return you an NSNumber. Checking out the documentation and you'll see that you can "convert" the NSNumber to any number type you want. Then you can use multiplication.
Something like this:
let firstNumberTextField: UITextField!
let secondNumberTextField: UITextField!
let numberFormatter = NumberFormatter()
let firstNumber = numberFormatter.number(from: firstNumberTextField.text!)
let secondNumber = numberFormatter.number(from: secondNumberTextField.text!)
let firstInt = firstNumber.integerValue //or whatever type of number you need
let secondInt = secondNumber.integerValue
let product = firstInt * secondInt
Dealing with Swift strings is kind of tricky because of the way they deal with Unicode and "grapheme clusters". You can't index into String objects using array syntax like that.
Swift also doesn't treat characters as interchangeable with 8 bit ints like C does, so you can't do math on characters like you're trying to do. You have to take a String and cast it to an Int type.
You could create an extension to the String class that WOULD let you use integer subscripts of strings:
extension String {
subscript (index: Int) -> String {
let first = self.startIndex
let startIndex = self.index(first, offsetBy: index)
let nextIndex = self.index(first, offsetBy: index + 1)
return self[startIndex ..< nextIndex]
}
}
And then:
let inputString = textMeli.text
let firstVal = Int(inputString[0])
let secondVal = Int(inputString[2])
and
let result = firstVal * 3 + secondVal * 7
Note that the subscript extension above is inefficient and would be a bad way to do any sort of "heavy lifting" string parsing. Each use of square bracket indexing has as bad as O(n) performance, meaning that traversing an entire string would give nearly O(n^2) performance, which is very bad.
The code above also lacks range checking or error handling. It will crash if you pass it a subscript out of range.
Note that its very strange to take multiple characters as input, then do math on the individual characters as if they are separate values. This seems like really bad user interface.
Why don't you step back from the details and tell us what you are trying to do at a higher level?
I saw a question: Swift: Split a String into an array
And there's some code I don't understand:
let fullName = "First Last"
let fullNameArr = split(fullName.characters){$0 == " "}.map{String($0)}
fullNameArr[0] // First
fullNameArr[1] // Last
How does split() and map{} work?
You're using a syntax that won't work in Xcode7. The correct syntax should be
let fullNameArr = fullName.characters.split{$0 == " "}.map(String.init)
Getting that out of the way let's break down that line into two pieces:
split takes
A collection of Characters representing the String's extended
grapheme clusters
-- From Xcode docs
and a closure taking a character and returning Bool - true if the character can be considered as a separator.
if this syntax is confusing try reading that:
fullNameArr = fullName.characters.split({
character in
return character == " "
})
Now, split returns an array of SubSequence objects. You want to convert them back to string to be able to print them nicely. So one way of doing it would be creating a for loop iterating over all the results of split and converting them to string, then appending to a result array, or using map method that does the same.
If you look closely at the first line, you execute map on the array and pass a closure that does something with every element of the array and writes it back.
A simple example how that works
let exampleArray = [1, 2, 3]
print(exampleArray.map {$0 * 3})
// prints [3, 6, 9]
Hope that helps!
For multiline button text I believe you add "\n" to the string. However I'm having trouble concatenating my function results and the newlinetext
setTitle:
HomeVC.getFriendCount("2",id:"friendid") + "\n newlinetext"
I need help getting my function results concatenated with "\n newlinetext"
You didn't specify an error, so I'm not sure, but I'm betting getFriendCount returns a number.
Try this:
let count = HomeVC.getFriendCount("2",id:"friendid")
let title = "\(count)\n newlinetext"
I've already encountered this. There must be some problem with (string + string) because it just ignores \n, though I never understood why this is. You can fix it by using join function:
let stringsToJoin = [getFriendCount("2",id:"friendid"), "newlinetext"]
let nString = join("\n", stringsToJoin)
Hope it helps!
You can use NSString's stringByAppendingString method
let aString = NSString(string: HomeVC.getFriendCount("2",id:"friendid"))
let concatenatedString = aString.stringByAppendingString("\n newlinetext")
If your method
HomeVC.getFriendCount("2",id:"friendid")
returns and optional string, then you need to unwrap it before concatenating.
Try
HomeVC.getFriendCount("2",id:"friendid")! + "\n newlinetext"
I’m trying to make a function that inputs an array of strings and outputs the strings separated by a semicolons.
My string array looks like:
let stringArray = ["dog", "cat", "bird", "cow"]
Any help is appreciated. Thanks!
Are you thinking of join?
let joinedString = join(";", stringArray)
Swift has join built into String and Array, but it works backwards from how you might think:
let stringArray = ["dog", "cat", "bird", "cow"]
let delimited = ";".join(stringArray)
// not stringArray.join(";")