This code:
Text("Email us at mail#mail.com")
Gives me this result:
But this:
let str = "Email us at mail#mail.com"
Text(str)
Gives me:
I actually will have a string like:
let str = "Email us at \(email)"
Text(str)
How can I make the email a highlighted link?
Upd, wrote this code:
let mail = "mail#mail.com"
let str = "Email us at \(mail)"
Text( (try? AttributedString(markdown: str)) ?? AttributedString(str) )
Related
I want to receive some text that has been inputted by the user on an Apple Watch. This is what I have so far:
presentTextInputController(withSuggestions: ["Michael","David","John","Lisa","Mary","Susan","Matthew","James","Jessica","Jennifer","Amanda","Emily","Dylan","Ross","Rupert"], allowedInputMode: WKTextInputMode.plain) { (arr: [Any]?) in
playerNames.playerOne = String(describing: arr)
print(playerNames.playerOne)
}
This always returns a optional like Optional([Michael])
I want it to return Michael
I have looked around about optionals but can't seem to find an anwser.
You can try this code:
let suggestions = ["Michael", "David", "John", "Lisa", "Mary", "Susan", "Matthew", "James", "Jessica", "Jennifer", "Amanda", "Emily", "Dylan", "Ross", "Rupert"]
presentTextInputController(withSuggestions: suggestions, allowedInputMode: WKTextInputMode.plain) { (arr: [Any]?) in
guard let arr = arr, let firstElement = arr.first as? String else { return }
playerNames.playerOne = firstElement
print(playerNames.playerOne)
}
Here are some documents you can refer to: Optional Binding, Guard Statement.
I am writing a basic iOS app to test my Swift knowledge and keep on practicing. In my app the user types a name for a baby, then turns a switch either on or off to set the gender/sex and also change the system color.
After that, the name is used to fill in a UITextView, named "firstWords", in the following block of code:
// Save name entered into text field
#IBAction func saveSettings(_ sender: UIButton) {
nameLabel.text = nameTextField.text
if nameTextField.text == "" {
showMessage()
nameLabel.text = "Baby Name"
}
nameTextField.resignFirstResponder()
let nameHolder: String! = nameLabel.text
if boyGirlSwitch.isOn {
let sex = ("boy", "his", "he", "Boy", "His", "He")
} else {
let sex = ("girl", "her", "she", "Girl", "Her", "She")
}
firstWords.text = "Wow, " + nameHolder + " has so much to look forward to!" + (sex.5) + " will do so many great things!"
}
I keep getting an error at the tuple (sex.5) inside firstWords that says: "Use of unresolved identifier 'sex'"
As I understand it, the constant sex is declared within the if statement and the compiler does go through it either way, so it does get identified and declared.
QUESTION: Why am I getting the error?
Thanks in advance! Here's a screenshot of my code as well:
Screenshot of block of code as described above, including the compiler/build-error
This is a scope issue. sex is only available within the else clause.
You can fix it like this
// Save name entered into text field
#IBAction func saveSettings(_ sender: UIButton) {
nameLabel.text = nameTextField.text
if nameTextField.text == "" {
showMessage()
nameLabel.text = "Baby Name"
}
nameTextField.resignFirstResponder()
let nameHolder: String! = nameLabel.text
var sex : (String, String, String, String, String, String)
if boyGirlSwitch.isOn {
sex = ("boy", "his", "he", "Boy", "His", "He")
} else {
sex = ("girl", "her", "she", "Girl", "Her", "She")
}
firstWords.text = "Wow, " + nameHolder + " has so much to look forward to!" + (sex.5) + " will do so many great things!"
}
this way sex is defined within the scope of the whole IBAction and will be available in the end.
you could also skip one condition if you pre-declare it with a default:
// Save name entered into text field
#IBAction func saveSettings(_ sender: UIButton) {
nameLabel.text = nameTextField.text
if nameTextField.text == "" {
showMessage()
nameLabel.text = "Baby Name"
}
nameTextField.resignFirstResponder()
let nameHolder: String! = nameLabel.text
var sex = ("girl", "her", "she", "Girl", "Her", "She")
if boyGirlSwitch.isOn {
sex = ("boy", "his", "he", "Boy", "His", "He")
}
firstWords.text = "Wow, " + nameHolder + " has so much to look forward to!" + (sex.5) + " will do so many great things!"
}
There is actually a nice article about variable scope on Wikipedia: https://en.wikipedia.org/wiki/Scope_(computer_science)
So I have this in my code.
let hex:String = "#FFFFFF"
var returnValue = UInt()
var newString = String()
newString = hex.replacingOccurrences(of: "#", with: "0x")
returnValue = UInt(newString)! //This line gets an error
It gives me an unwrapping optional value error. How do I fix it?
You are using the wrong initializer and even if it was the right initializer the string format is wrong:
let hex = "#FFFFFF"
let newString = hex.replacingOccurrences(of: "#", with: "")
let returnValue = UInt(newString, radix:16) ?? 0
I am learning to use predicates for filtering. I found a tutorial, but one aspect is not working for me in Swift 3. Here is some specific code:
let ageIs33Predicate01 = NSPredicate(format: "age = 33") //THIS WORKS
let ageIs33Predicate02 = NSPredicate(format: "%K = 33", "age") //THIS WORKS
let ageIs33Predicate03 = NSPredicate(format: "%K = %#", "age","33") //THIS DOESN'T WORK
let ageIs33Predicate04 = NSPredicate(format: "age = %#","33") //THIS DOESN'T WORK
All 4 compile, but the last 2 produce no results even though I have a case where age = 33. Here is the test complete test code from the tutorial:
import Foundation
class Person: NSObject {
let firstName: String
let lastName: String
let age: Int
init(firstName: String, lastName: String, age: Int) {
self.firstName = firstName
self.lastName = lastName
self.age = age
}
override var description: String {
return "\(firstName) \(lastName)"
}
}
let alice = Person(firstName: "Alice", lastName: "Smith", age: 24)
let bob = Person(firstName: "Bob", lastName: "Jones", age: 27)
let charlie = Person(firstName: "Charlie", lastName: "Smith", age: 33)
let quentin = Person(firstName: "Quentin", lastName: "Alberts", age: 31)
let people = [alice, bob, charlie, quentin]
let ageIs33Predicate01 = NSPredicate(format: "age = 33")
let ageIs33Predicate02 = NSPredicate(format: "%K = 33", "age")
let ageIs33Predicate03 = NSPredicate(format: "%K = %#", "age","33")
let ageIs33Predicate04 = NSPredicate(format: "age = %#","33")
(people as NSArray).filtered(using: ageIs33Predicate01)
// ["Charlie Smith"]
(people as NSArray).filtered(using: ageIs33Predicate02)
// ["Charlie Smith"]
(people as NSArray).filtered(using: ageIs33Predicate03)
// []
(people as NSArray).filtered(using: ageIs33Predicate04)
// []
What am I doing wrong? Thanks.
Why would the last two work? You are passing a String in for an Int property. You need to pass in an Int to compare against the Int property.
Change the last two to:
let ageIs33Predicate03 = NSPredicate(format: "%K = %d", "age", 33)
let ageIs33Predicate04 = NSPredicate(format: "age = %d", 33)
Note the change in the format specifier from %# to %d.
I am trying to write a regex to get the last name of a person.
var name = "My Name";
var regExp = new RegExp("\s[a-z]||[A-Z]*");
var lastName = regExp(name);
Logger.log(lastName);
If I understand correctly \s should find the white space between My and Name, [a-z]||[A-Z] would get the next letter, then * would get the rest. I would appreciate a tip if anyone could help out.
You can use the following regex:
var name = "John Smith";
var regExp = new RegExp("(?:\\s)([a-z]+)", "gi"); // "i" is for case insensitive
var lastName = regExp.exec(name)[1];
Logger.log(lastName); // Smith
But, from your requirements, it is simpler to just use .split():
var name = "John Smith";
var lastName = name.split(" ")[1];
Logger.log(lastName); // Smith
Or .substring() (useful if there are more than one "last names"):
var name = "John Smith Smith";
var lastName = name.substring(name.indexOf(" ")+1, name.length);
Logger.log(lastName); // Smith Smith