This question already has answers here:
Printing optional variable
(15 answers)
Closed 7 years ago.
I am trying to print value on my table view row everything goes correct but value is printing like
println("\(cell?.textLabel?.text)")
Optional("Some text") instead of "Some text"
Is there any way in swift to print actual value?
It is printing an optional values so you need to unwrap it like:
if let yourCell = cell?.textLabel?.text {
println(yourCell)
}
Refer this Apple Document to know more about optionals.
print like this
println(cell.textLabel?.text)
Related
This question already has an answer here:
Swift variable decorations with "?" (question mark) and "!" (exclamation mark)
(1 answer)
Closed 2 years ago.
I have this doubt when I run this code both of them give me the same datatype as result. I know I am missing something really basic can someone please explain why ! and ? on a type gives the same datatype as result?
import UIKit
import Foundation
var unwrappedString: String!
var optionalString: String?
print("type of unwrappedString is:", type(of:unwrappedString))
print("type of optionalString is:", type(of:optionalString))
output is
type of unwrappedString is: Optional<String>
type of optionalString is: Optional<String>
Both symbols ! and ? used for optionals. We should have to use !, when we make sure option type variable has value at a time and we need to take action on it. Otherwise it will crash if variable value is nil nd if you are using ?, Application will not crash in nil case.
This question already has answers here:
How to capitalize each word in a string using Swift iOS
(8 answers)
Closed 5 years ago.
I want output like this:
var String1 = "stack over flow"
var desiredOutPut = "Stack Over Flow"
Also, I want this output in TextField event named "textFieldDidChange" so it must be efficient as well.
var desiredOutPut = String1.capitalized
This question already has answers here:
Finding out whether a string is numeric or not
(18 answers)
Closed 7 years ago.
How can I check, if searchView contains just numbers?
I found this:
if newText.isMatchedByRegex("^(?:|0|[1-9]\\d*)(?:\\.\\d*)?$") { ... }
but it checks if text contains any number. How can I do, that if all text contains just numbers in Swift?
Here is the solution you can get all digits from String.
Swift 3.0 :
let testString = "asdfsdsds12345gdssdsasdf"
let phone = testString.components(separatedBy: CharacterSet.decimalDigits.inverted).joined(separator: "")
print(phone)
you can use "^[0-9]+$" instade "^(?:|0|[1-9]\\d*)(?:\\.\\d*)?$"
This will accept one or more digits, if you want to accept only one digit then remove +
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
When I get the value from a Text Field and cast it to an integer, I get log output that says Optional(5) if I inputted 5, or Optional(1234) if I inputted 1234.
What does Optional() mean?
Here is the code to get and cast the value:
#IBOutlet weak var myInput: UITextField!
// When a button is clicked, I log the value in the UI text field.
#IBAction func someButton(sender: AnyObject) {
println(self.myInput.text.toInt())
}
Optional means the value is Optional type. It can be either nil or a value. When working with cocoa api most of the method parameters are optional type. To get actual value from optional value you either use if-let binding or force it to unwrap it with ! operator. Suppose ve have a value of a optional type of Int. Let's define it first.
let a: Int? = 5
The ? denotes it is an optional value. If you print this a it will write Optional(5). Let's get the actual value from it.
if let actualA = a {
println(actualA)
}
Now if a is not nil then the code inside if-let statement will be executed and it will print 5 on the console.
Optional types are for dealing with nil values in swift. They provide extra safety when working parameters and variables. If a value is not optional than it can never be nil. So we can safely do our work without worrying about nil values.
This question already has answers here:
How to modify user input?
(4 answers)
Closed 9 years ago.
I would like to replace text in an user input with an variable.
I wrote an little demo code, to show you my problem:
puts "Enter your feeling"
a = gets.chomp
#feel = "good"
puts a
SO when it comes to the input, i type in:
Actually i fell very #{#feel}
Then i hope to get this output:
Actually i fell very good
But instead i get this output:
Actually i fell very #{#feel}
What did i make wrong?
You can make use of Kernal#eval
eval ("a")
#{variable} works only double quotes (" "). So you try inside of "". if you use this ' ', it will consider as string