The Swift documentation at page 61 of the Swift manual hints to the possibility of using where to join an optional binding with a regular condition. Yet when I do it I have a warning suggesting me to substitute the where with a comma like in the following piece of code:
if let geocodingError = error as? NSError where geocodingError.code == 2
In Swift 3 this syntax has changed.
What was
if let x = y, a = b where a == x {
Is now
if let x = y, let a = b, a == x {
The justification is that each sub-clause of the if ... { is now an independent boolean test.
See the Xcode Release notes & the Swift Evolution proposal for more info about this change.
Example with two conditions
if let x = y, let a = b, a == x && !x.isEmpty {
In xcode 9
if let str = textField.text as String!, !str.isEmpty
{
params[key] = str
TextFieldHelper.setup(textField: textField)
}
else
{
TextFieldHelper.error(textField: textField)
}
Related
This question already has answers here:
Cocoa method to return a list of all letters in the current locale's alphabet?
(3 answers)
Closed 1 year ago.
I’m translating my app to different languages, how could I get the alphabet based on the localization? Could be Latin, Cyrillic, etc.
Try this:
import UIKit // Or Foundation
if let alphabetCharacterSet = Locale(identifier: "ru").exemplarCharacterSet?.intersection(CharacterSet.lowercaseLetters) {
print(alphabetCharacterSet.characters().sorted(by: {String($0).localizedCompare(String($1)) == .orderedAscending }))
}
// If you don't sort alphabetical order is not guaranteed.
extension CharacterSet {
func characters() -> [Character] {
// A Unicode scalar is any Unicode code point in the range U+0000 to U+D7FF inclusive or U+E000 to U+10FFFF inclusive.
return codePoints().compactMap { UnicodeScalar($0) }.map { Character($0) }
}
func codePoints() -> [Int] {
var result: [Int] = []
var plane = 0
// following documentation at https://developer.apple.com/documentation/foundation/nscharacterset/1417719-bitmaprepresentation
for (i, w) in bitmapRepresentation.enumerated() {
let k = i % 0x2001
if k == 0x2000 {
// plane index byte
plane = Int(w) << 13
continue
}
let base = (plane + k) << 3
for j in 0 ..< 8 where w & 1 << j != 0 {
result.append(base + j)
}
}
return result
}
}
You have to provide Localizable.strings base file to you project, which consist of your different text for different origins:
Like
for default language:
"Hello World!" = "Hello World!";
and for like in Latin language:
"Hello World!" = "salve mundi!";
I have an app that solves physics problems based on given knowns and unknowns .. but it does it the wrong way..
let's say we have this equation ( d = v * t )
We have here three variables .. the way I did it is like this
let v = 10
let t = 5
let d = nil
let result = Double()
if d == nil && v != nil && t != nil {
result = v * t
}if v == nil && d != nil && t != nil {
result = d / t
}if t == nil && v != nil && d != nil {
result = d / v
}else{
result = nil
}
print(result) // "50"
obviously it's a headache .. this is only a small equation with three vars.. what if I had 20 equations each has 5 vars.. it would be very exhausting to type.. So I'm wondering if there is a way to just type the equation and call .solve or something .. something like this
let equation = "d = v * t"
let knowns = [d:20,v:50]
let result = equation.solve(usingKnowns: knowns)
print(result) // "Optional(t = 0.4)"
Thanks in advance,
Any help would be really great
The problem here is to see what variable is unknown, supose you have X variables and you always get X-1 variables known and 1 unknown you don't really need to check if the knowns variables are != nil.
Other possibility is the one I recommend you:
func resolve(a: Double?, b: Double?, c: Double?)-> Double{
//Default value
var result: Double = 0.0
//a missing
if let b = b, let c = c{
result = b*c
}
//b missing
if let a = a, let b = b{
result = a/b
}
//c missing
if let a = a, let c = c{
result = a/c
}
return result
}
In this case you have a single call with the 3 parameters, all of them optionals, so if you try a "if let" with 2 o them simultaneously and it works, it means the other one is the unknow variable.
This is the "cleanest" solution I can think of.
This question already has answers here:
Swift - Resolving a math operation in a string
(4 answers)
Closed 6 years ago.
let randsign = Int(arc4random_uniform(2) + 1)
//This function returns a random operator
func whatSign(par1:Int)-> String {
if (par1 == 1){
return "+"
}
else {
return "-"
}
}
var sigh = whatSign(par1: randsign)
let randnum1:Any = Int (arc4random_uniform(10) + 1)
let randnum2:Int = Int (arc4random_uniform(10) + 1)
//I want the variable "finVal" to perform mathematical operation, but it does not because the variable "sign" is String
let finVal = "\(randnum1)\(sigh)\(randnum2)"
print(finVal)
//When I print I get for example 1-3, 9-4, 8+2 .But I wanted them to do arithmetic
NSExpression is one the good way to evaluate the Math
let finVal = "(randnum1)(sigh)(randnum2)"
let result = NSExpression(format: finVal).expressionValue(with: nil, context: nil) as! Int
let randsign = Int(arc4random_uniform(2) + 1)
let randnum1:Any = Int (arc4random_uniform(10) + 1)
let randnum2:Int = Int (arc4random_uniform(10) + 1)
var finVal:Int
//This function returns a random operator
func whatSign(par1:Int)-> String {
//I want to compute the variables and get final result from adding or subtracting them
if (par1 == 1){
finVal = randnum1 + randnum2
return "+"
}
else {
finVal = randnum1 - randnum2
return "-"
}
}
whatSign(par1: randsign)
print(finVal)
//When I print I get for example 1-3, 9-4, 8+2 .But I wanted them to do arithmetic
Maybe it will helps you
This question already has answers here:
Refactored Solution In Swift
(2 answers)
Closed 6 years ago.
I'm attempting to solve HackerRank's Hash Table Ransom Note challenge. There are 19 test cases and I'm passing all but two of time due to timeout on larger data sets (10,000-30,000 entries).
I'm given:
1) an array of words contained in a magazine and
2) an array of words for a ransom note. My objective is to determine if the words in the magazine can be used to construct a ransom note.
I need to have enough unique elements in the magazineWords to satisfy the quantity needed by noteWords.
I'm using this code to make that determination...and it takes FOREVER...
for word in noteWordsSet {
// check if there are enough unique words in magazineWords to put in the note
if magazineWords.filter({$0==word}).count < noteWords.filter({$0==word}).count {
return "No"
}
}
What is a faster way to accomplish this task?
Below is my complete code for the challenge:
import Foundation
var magazineWords = // Array of 1 to 30,000 strings
var noteWords = // Array of 1 to 30,000 strings
enum RegexString: String {
// Letters a to z, A to Z, 1 to 5 characters long
case wordCanBeUsed = "([a-zA-Z]{1,5})"
}
func matches(for regexString: String, in text: String) -> [String] {
// Hat tip MartinR for this
do {
let regex = try NSRegularExpression(pattern: regexString)
let nsString = text as NSString
let results = regex.matches(in: text, range: NSRange(location: 0, length: nsString.length))
return results.map { nsString.substring(with: $0.range)}
} catch let error {
print("invalid regex: \(error.localizedDescription)")
return []
}
}
func canCreateRansomNote(from magazineWords: [String], for noteWords: [String]) -> String {
// figure out what's unique
let magazineWordsSet = Set(magazineWords)
let noteWordsSet = Set(noteWords)
let intersectingValuesSet = magazineWordsSet.intersection(noteWordsSet)
// constraints specified in challenge
guard magazineWords.count >= 1, noteWords.count >= 1 else { return "No" }
guard magazineWords.count <= 30000, noteWords.count <= 30000 else { return "No" }
// make sure there are enough individual words to work with
guard magazineWordsSet.count >= noteWordsSet.count else { return "No" }
guard intersectingValuesSet.count == noteWordsSet.count else { return "No" }
// check if all the words can be used. assume the regex method works perfectly
guard noteWords.count == matches(for: RegexString.wordCanBeUsed.rawValue, in: noteWords.joined(separator: " ")).count else { return "No" }
// FIXME: this is a processor hog. I'm timing out when I get to this point
// need to make sure there are enough magazine words to write the note
// compare quantity of word in magazine with quantity of word in note
for word in noteWordsSet {
// check if there are enough unique words in magazineWords to put in the note
if magazineWords.filter({$0==word}).count < noteWords.filter({$0==word}).count {
return "No"
}
}
return "Yes"
}
print(canCreateRansomNote(from: magazineWords, for: noteWords))
I don't know how to read from the test case on the contest website or what frameworks you are allowed. If Foundation is allowed, you can use NSCountedSet
import Foundation
let fileContent = try! String(contentsOf: URL(fileURLWithPath: "/path/to/file.txt"))
let scanner = Scanner(string: fileContent)
var m = 0
var n = 0
scanner.scanInt(&m)
scanner.scanInt(&n)
var magazineWords = NSCountedSet(capacity: m)
var ransomWords = NSCountedSet(capacity: n)
for i in 0..<(m+n) {
var word: NSString? = nil
scanner.scanUpToCharacters(from: .whitespacesAndNewlines, into: &word)
if i < m {
magazineWords.add(word!)
} else {
ransomWords.add(word!)
}
}
var canCreate = true
for w in ransomWords {
if ransomWords.count(for: w) > magazineWords.count(for: w) {
canCreate = false
break
}
}
print(canCreate ? "Yes" : "No")
It works by going through the input file one word at a time, counting how many times that word appears in the magazine and in the ransom note. Then if any word appear more frequently in the ransom note than in the magazine, it fails the test immediately. Run the 30,000 words test case in less than 1 second on my iMac 2012.
I have an app that converts textField input into an array of Int's by using componentSeparatedByString(",") but when i enter more than one comma in the textfield the app crashes, been trying to find a solution online but no luck, how can i fix this? I can keep it from crashing by checking for characters.first == "," ||characters.last == ",", but not consecutive commas.
enterValueLabel.text = ""
let circuits = circuitNumbersTextField.text!.componentsSeparatedByString(",")
let circuitNumbers = circuits.map { Int($0)!}
CircuitColors(circuitNumber: circuitNumbers, phaseColors: circuitPhaseColors )
if /*circuitNumbersTextField.text!.characters.first != "," || */circuitNumbersTextField.text!.characters.last != "," || (circuitNumbersTextField.text!.characters.first != "," && circuitNumbersTextField.text!.characters.last != ",")
Here's what I would do to make your code work. What is important here is the general idea, not the specific example I'm using (although it should work for you).
First, let's safely unwrap the text label:
if let text = circuitNumbersTextField.text {
}
Now that we avoid using circuitNumbersTextField.text! we know that an error wouldn't come from there.
Then we cut the sentence in components:
if let text = circuitNumbersTextField.text {
let circuits = text.componentsSeparatedByString(",")
}
We use flatMap to safely unwrap the Optionals returned by Int():
if let text = circuitNumbersTextField.text {
let circuits = text.componentsSeparatedByString(",")
let circuitNumbers = circuits.flatMap { Int($0) }
// circuitNumbers will only contain the successfully unwrapped values
}
Your code snippet:
if let text = circuitNumbersTextField.text {
let circuits = text.componentsSeparatedByString(",")
let circuitNumbers = circuits.flatMap { Int($0) }
if (circuits.first != "," && circuits.last != ",") || circuits.first != "," || circuits.last != "," {
// condition is met
} else {
// condition is not met
}
}
You can now safely use circuitNumbers in this code block without crashing.