Swift Iteration Character Length [duplicate] - ios

This question already has answers here:
Generate random alphanumeric string in Swift
(24 answers)
Closed 6 years ago.
I am in the process of making one of my first apps, the aim of it is as a password generator and telling people on a scale of 1-1000 how hard it is to guess, and how hard it is to remember based on how the letters are formatted and what it looks like and how the brain remembers patterns. So far i have all the characters I want to use in an array, and I then have a for in loop that iterates through the characters, but I can't figure out how to specify the length of the password to generate, as currently it just prints each character. So, I am asking how can I make an 8 character long password generator as simply as possible, what i have so far is:
import Foundation
let chars = ["a","b","c","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u" ,"v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","0","1","2","3","4","5","6","7","8","9"]
Thank you!
var generate: String
for generate in chars {
print(generate)
}

As simply as possible use a loop and and a random number to get the character at given index:
let length = 8
var pass = ""
for _ in 0..<length {
let random = arc4random_uniform(UInt32(chars.count))
pass += chars[Int(random)]
}
print(pass)

Related

Localize file with property in swift [duplicate]

This question already has answers here:
Precision String Format Specifier In Swift
(31 answers)
Closed 2 years ago.
I need to transfer the double variable to the localization file, if I write %d I can only pass int, but I need to pass the double
You can pass the double with format string %f. To restrict the number of digits after the decimal point you can format is like this %0.2f. This will allow only 2 digits after the decimal point.
Sample Code:
let num = 10.5
let outputStr = String(format:"I am printing %0.1f", arguments:[num])
print(outputStr)

iOS String: remove prefix and suffix by CharacterSet [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
For a given String in Swift I need to remove prefix and suffix characters that belong to a predefined character set.
I can use components(separatedBy:) with the character set and get rid of the empty strings at the beginning and end of the components array. Just wondering if there's a better approach?
Thanks!
You can use .trimmingCharacters(in: CharacterSet) on the string. From the 'help' text in Xcode on this function: "Returns a new string made by removing from both ends of the String characters contained in a given character set."
Here is a unit test example using a custom character set. Notice it only removes characters from the start and end, not the middle (the comma stays):
import XCTest
class TrimCharacters: XCTestCase {
func testExample() throws {
let string = "abcbaHello, World!ccbbaa"
let charactersToTrim = CharacterSet(charactersIn: "abc,")
XCTAssertEqual(string.trimmingCharacters(in: charactersToTrim), "Hello, World!")
}
}
There are also predefined character sets that can be useful. You can also invert a character set. See CharacterSet

Invalid escape sequence in literal with regex [duplicate]

This question already has an answer here:
Ignore escaped double quote characters swift
(1 answer)
Closed 6 years ago.
I define a string with:
static let Regex_studio_tel = "^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,4})?$"
But there comes an issue:
Invalid escape sequence in literal
The picture I token:
Edit -1
My requirement is match special plane numbers use Regex, such as:
My company have a special plane number:
028-65636688 or 85317778-8007
// aaa-bbbbbbbb-ccc we know the aaa is the prefix, and it means City Dialing Code, and bbbbbbbb is the main tel number, cccc is the landline telephone's extension number,
such as my company's landline telephone is 028-65636688, maybe our company have 10 extension number: 028-65636688-8007 ,028-65636688-8006,028-65636688-8005 and so on.
Of course, it maybe have a ext-number at the end.
028-65636688-2559
Two character sequence \ - is not a valid escape sequence in Swift String. When you need to pass \ - to NSRegularExpression as pattern, you need to write \\- in Swift String literal.
So, your line should be something like this:
static let Regex_studio_tel = "^(0[0-9]{2,3}\\-)?([2-9][0-9]{6,7})+(\\-[0-9]{1,4})?$"
ADDITION
As Rob commented, minus sign is not a special character in regex when appearing outside of [ ], so you can write it as:
static let Regex_studio_tel = "^(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$"
I'm guessing that your intent was to escape the - characters. But that's not necessary (and is incorrect). If your intent was to match just dashes, you should remove those backslashes entirely:
let pattern = "^(0[0-9]{2,3}-)?([2-9][0-9]{6,7})+(-[0-9]{1,4})?$"
Unrelated, but I'm suspicious of that + character. Did you really mean that you wanted to match one or more occurrences of [2-9][0-9]{6,7}? Or did you want to match exactly one occurrence?

How to add percent sign in string format and using swift language? [duplicate]

This question already has answers here:
How to add percent sign to NSString
(7 answers)
Closed 6 years ago.
This post was edited and submitted for review 2 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I want to get string like "99.99%" from Double,and I used format to get this:
let rate = 99.99999
let str = String(format: "%.2f%", rate)
// output 99.99
And \% is not allowed. So how to add percent sign in string format, please help me!
write % twice:
rate = 99.99
let str = String(format: "%.2f%%", rate)
The % symbol has a special use in a printf statement. How would you
place this character as part of the output on the screen?
You can do this by using %% in the printf statement. For example, you can write printf(“10%%”) to have the output appear as 10% on the screen.
Hope it help you :)

How do you confirm a string only contains numbers in Swift? [duplicate]

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 +

Resources