swift 2.2 basic, if statement doesn't compile [duplicate] - swift

This question already has an answer here:
Error "{ expected after if statement"
(1 answer)
Closed 6 years ago.
Pardon me for beginner's question, Why this code got complained about { expected after if, the braces are already there
var years = Int(edtYears.text!)
if years !=nil {
//do something
}else {
//...
}
Thanks

You need to add space between both side of condition like if years != nil { or you can also write without space but the both side if years!=nil {
var years = Int("")
if years != nil {
//do something
}else {
//...
}

Never do like this. Make sure you do optional chaining otherwise you will surely get a crash.
if let text = edtYears.text, let convertToInt = Int(text){
print("Int \(convertToInt)")
}else{
print("Cannot convert")
}

Related

I want to pass some text into the textfield but something wrong....idk why print() is work [duplicate]

This question already has an answer here:
how can i put these print text into Textfield?
(1 answer)
Closed 5 years ago.
swift4 Xocode9
UNUserNotificationCenter.current().getPendingNotificationRequests {
DispatchQueue.main.async {
// Contextual closure type '() -> Void' expects 0 arguments,
// but 1 was used in closure body
let str:String = ""
self.finalresulter.text = str
self.finalresulter.text = "\($0.map{$0.content.title})"
}
print($0.map { $0.content.title},",",$0.map { $0.content.subtitle},","
,$0.map { $0.content.body},","
,$0.map { $0.trigger!})
}
please help...how to fix..
Try this:
notificatecontent.text = "\($0.map{$0.content.title})"
The issue appears to be identical to your other question. In order not to replicate existing answer I gave there, I would only mention the second part, which would look like:
UNUserNotificationCenter.current().getPendingNotificationRequests { requests in
// Textfield assignment here ...
print(requests.map { $0.content.title},",",requests.map { $0.content.subtitle},","
,requests.map { $0.content.body},","
,requests.map { $0.trigger!})
}
But, unless you explicitly want to print everything on one line, this print should be better replaced with regular for loop (or at least a forEach method):
// 1
for item in requests {
print(item.content.title, item.content.subtitle, item.content.body)
}
// 2
requests.forEach {
print($0.content.title, $0.content.subtitle, $0.content.body)
}

Removing "Optional" in Swift 4 [duplicate]

This question already has answers here:
How do I prevent my text from displaying Optional() in the Swift interpolation?
(3 answers)
Closed 5 years ago.
That's my code
let temperature = String(describing: Int(incomeTemp.text!))
celcjuszScore.text = "\(temperature)"
print(temperature)
Whent I am pushing a button, the result of print is "Optional(32)" (When I am writing 32 in incomeTemp). I would like to have "Optional" removed and only "32" should stay.
Just unwrap it.
if let temperature = Int(incomeTemp.text!) {
celcjuszScore.text = "\(temperature)"
print(temperature)
}
Remove the optional when converting text to number: Int(incomeTemp.text!) ?? 0.
Or solve the error explicitly:
if let temperature = Int(incomeTemp.text ?? "") {
celcjuszScore.text = "\(temperature)"
} else {
celcjuszScore.text = "Invalid temperature"
}

What's the difference between a comma separated conditional and one that uses a double ampersand [duplicate]

This question already has answers here:
Separating multiple if conditions with commas in Swift
(6 answers)
Closed 5 years ago.
I've recently come across this type of scenario using if/let and understand what it does thanks to this post. To my understanding, both conditions need to be met before the proceeding block is executed. I now have come to a point where I've seen it in a regular conditional statement:
if existingTextHasDecimalSeparator != nil, replacementTextHasDecimalSeparator != nil {
return false
} else {
return true
}
What's the difference between doing the above and simply using && as seen below?:
if existingTextHasDecimalSeparator != nil && replacementTextHasDecimalSeparator != nil {
return false
} else {
return true
}
There does not appear to be a difference between using && for grouping conditionals and using commas. I too have so far only seen it used with optional binding, but apparently it also works for regular conditionals, as the following snippet works fine.
let bool1 = true;
let bool2 = true;
if bool1 , bool2 {
print("true");
} else {
print("false")
}
The comma is used when optional binding with boolean conditions, for example if let a = a, a.isValid() whereas && is a typical AND operator

use of unresolved identifier 'println' in swift 3 [duplicate]

This question already has answers here:
How do you do println() in swift 2 [duplicate]
(4 answers)
Closed 6 years ago.
I'm just a beginner with Swift. I am trying a very basic code, but unable to print things using 'println'.
This is my code,
import Cocoa
var myString:String? = nil
if myString != nil {
println(myString)
}else {
println("myString has nil value")
}
println() does not work in xcode. Instead, print() function is used to print a statement with a newline.
If instead, you were to want to print without a newline, a terminator is used
Try this for your code:
import Cocoa
var myString:String? = nil
if myString != nil {
print(myString)
} else {
print("myString has nil value")
}
Example with terminator:
import Cocoa
var myString:String? = nil
if myString != nil {
print(myString, terminator:"")
} else {
print("myString has nil value", terminator:"")
}

In swift, how do I do this? [duplicate]

This question already has answers here:
Adding Thousand Separator to Int in Swift
(7 answers)
Closed 7 years ago.
My English is not enough to search the question. So, I have to write here. My integer is 600000000 for ex. I want to convert it like this: 600,000,000. How do I do this?
extension Int {
struct Number {
static let formatter = NSNumberFormatter()
}
var addThousandSeparator:String {
Number.formatter.groupingSeparator = "."
Number.formatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
return Number.formatter.stringFromNumber(self)!
}
}
let myInteger = 600000000
let myIntegerString = myInteger.addThousandSeparator // "600.000.000"