Complicated Swift Extension - ios

I want to create an extension for the String class in Swift that allows you to get a substring via the subscript operator like in Python. This can be accomplished with the Range class in the following way
extension String {
subscript (range: Range<Int>) -> String? {
if range.startIndex < 0 || range.endIndex > count(self) {
return nil
}
let range = Range(start: advance(startIndex, range.startIndex), end: advance(startIndex, range.endIndex))
return substringWithRange(range)
}
}
This enables the following usage
let string = "OptimusPrime"
string[0...6] // "Optimus"
However, I want to be able to index the string from the end as well as the beginning using negative integers.
string[7...(-1)] // "Prim"
Since the Range class doesn't allow startIndex to be greater than endIndex the extension above is not sufficient to achieve this behavior.
Since Range objects apparently can't be used for this extension, it makes sense to me to simply use the same syntax as in Python
string[0:2] // "Op"
string[0:-1] // "OptimusPrim"
Is this possible? How would I go about doing this?

i think you could get python-like syntax like this:
subscript (start:Int, end:Int) -> String?
which would let you go "bla bla"[0,3]
then if you take a negative number in, just make convert it to the string length minus the negative number instead so the range is valid

Related

Swift loses leading zero

I'm trying to pass an int to another class like 0902 and the sent result loses the first zero. This only happens when a zero is leading.
I also have a function which tries to add it in but that doesn't work either. Is there a reason in Swift for not keeping a leading zero Int?
If I send a value of 0902 to another class what gets shown afterwards is 902, totally confused.
func convertToTime(_ value: Int) -> String {
print(value) // 902
var text = String(format: "%02d", value)
text.insert(":", at: text.index(text.startIndex, offsetBy: +2))
return text
}
The problem here is how you define the type of that value "0902"?
Is it really a number?
Or is it actually a String that only contains numeric values?
Like a telephone number for instance. Is that a number? Or a string?
Would you want to add them together? Or multiply them? If not, make it a String.
Once you make it a string, the leading 0 is no longer an issue as it is just part of the string. As soon as you make it into a number then it makes no sense for the leading zero to be there as 0902 == 902.
Additional
Having had another look at this... Is it even a string? You are dealing with time here? So surely you should be using an NSDate object?
The problem is still the same though. Make sure you define your types correctly.
Determine what this "0902" actually is. Then make it the correct type for that.
Whether that be a number, a string, or a date.
The correct type will then ensure that you get the correct formats, and functions and properties of it.
Change your function argument to String:
func convertToTime(_ value: String) -> String {
print(value) // prints "0902"
var text = value
text.insert(":", at: text.index(text.startIndex, offsetBy: +2))
return text
}
convertToTime("0902") // prints "09:02"
func convert(integerToTimeString int: Int) -> String{
var string = "\(int)"
if string.count > 3{
string.insert(":", at: string.index(string.startIndex, offsetBy: String.IndexDistance(2)))
return string
}
string.insert("0", at: string.startIndex)
string.insert(":", at: string.index(string.startIndex, offsetBy: String.IndexDistance(2)))
return string
}
Try this code instead
It converts the Int to a String first and checks if its 4 characters before adding the ":"

How to get substring from user input?

i wrote code to get character when user enter in text field and do math with them
this :
#IBOutlet weak internal var textMeli: UITextField!
var myChar = textMeli.text
var numb = [myChar[0]*3 , myChar[1]*7]
but one is wrong
textMeli.text is a String.
myChar is a String.
You can't access a Character from a String using bracket notation.
Take a look at the documentation for the String structure.
You'll see that you can access the string's characters through the characters property. This will return a collection of Characters. Initalize a new array with the collection and you can then use bracket notation.
let string = "Foo"
let character = Array(string.characters)[0]
character will be of type Character.
You'll then need to convert the Character to some sort of number type (Float, Int, Double, etc.) to use multiplication.
Type is important in programming. Make sure you are keeping track so you know what function and properties you can use.
Off the soap box. It looks like your trying to take a string and convert it into a number. I would skip the steps of using characters. Have two text fields, one to accept the first number (as a String) and the other to accept the second number (as a String). Use a number formatter to convert your string to a number. A number formatter will return you an NSNumber. Checking out the documentation and you'll see that you can "convert" the NSNumber to any number type you want. Then you can use multiplication.
Something like this:
let firstNumberTextField: UITextField!
let secondNumberTextField: UITextField!
let numberFormatter = NumberFormatter()
let firstNumber = numberFormatter.number(from: firstNumberTextField.text!)
let secondNumber = numberFormatter.number(from: secondNumberTextField.text!)
let firstInt = firstNumber.integerValue //or whatever type of number you need
let secondInt = secondNumber.integerValue
let product = firstInt * secondInt
Dealing with Swift strings is kind of tricky because of the way they deal with Unicode and "grapheme clusters". You can't index into String objects using array syntax like that.
Swift also doesn't treat characters as interchangeable with 8 bit ints like C does, so you can't do math on characters like you're trying to do. You have to take a String and cast it to an Int type.
You could create an extension to the String class that WOULD let you use integer subscripts of strings:
extension String {
subscript (index: Int) -> String {
let first = self.startIndex
let startIndex = self.index(first, offsetBy: index)
let nextIndex = self.index(first, offsetBy: index + 1)
return self[startIndex ..< nextIndex]
}
}
And then:
let inputString = textMeli.text
let firstVal = Int(inputString[0])
let secondVal = Int(inputString[2])
and
let result = firstVal * 3 + secondVal * 7
Note that the subscript extension above is inefficient and would be a bad way to do any sort of "heavy lifting" string parsing. Each use of square bracket indexing has as bad as O(n) performance, meaning that traversing an entire string would give nearly O(n^2) performance, which is very bad.
The code above also lacks range checking or error handling. It will crash if you pass it a subscript out of range.
Note that its very strange to take multiple characters as input, then do math on the individual characters as if they are separate values. This seems like really bad user interface.
Why don't you step back from the details and tell us what you are trying to do at a higher level?

Swift: Precedence for custom operator in relation to dot (".") literal

In Swift 3, I have written a custom operator prefix operator § which I use in a method taking a String as value returning a LocalizedString struct (holding key and value).
public prefix func §(key: String) -> LocalizedString {
return LocalizedString(key: key)
}
public struct LocalizedString {
public var key: String
public var value: String
public init(key: String) {
let translated = translate(using: key) // assume we have this
self.key = key
self.value = translated ?? "!!\(key)!!"
}
}
(Yes I know about the awesome L10n enum in SwiftGen, but we are downloading our strings from our backend, and this question is more about how to work with custom operators)
But what if we wanna get the translated value from the result of the § operator (i.e. the property value from the resulting LocalizedString)
let translation = §"MyKey".value // Compile error "Value of type 'String' has no member 'value'"
We can of course easily fix this compile error by wraping it in parenthesis (§"MyKey").value. But if do not want to do that. Is it possible to set precedence for custom operators in relationship to the 'dot' literal?
Yes I know that only infix operators may declare precedence, but it would make sense to somehow work with precedence in order to achieve what I want:
precedencegroup Localization { higherThan: DotPrecedence } // There is no such group as "Dot"
prefix operator §: Localization
To mark that the Swift compiler first should evaluate §"MyKey" and understand that is not a string, but in fact an LocalizedString (struct).
Feels unlikely that this would be impossible? What am I missing?
The . is not an operator like all the other ones defined in the standard library, it is provided by the compiler instead. The grammar for it are Explicit Member Expressions.
Having a higher precedence than the . is nothing the compiler should enable you to do, as it's such a fundamental use case. Imagine what you could do if the compiler enabled such a thing:
-"Test".characters.count
If you could have a higher precedence than ., the compiler has to check all possibilities:
(-"Test").characters.count // func -(s: String) -> String
(-("Test".characters)).count // func -(s: String.CharacterView) -> String.CharacterView
-("Test".characters.count) // func -(s: Int) -> Int
Which would
Potentially increase the compile time a lot
Be ambiguous
Possibly change behaviour of existing code upon adding overloads
What I suggest you to do is abandon the idea with a new operator, it's only going to be adding more cognitive load by squashing some specific behaviour into a single obscure character. This is how I'd do it:
extension String {
var translatedString : String {
return translate(using: self)
}
}
"MyKey".localizedString
Or if you want to use your LocalizedString:
extension String {
var localized : LocalizedString {
return LocalizedString(key: self)
}
}
"MyKey".localized.value
These versions are much more comprehensive.

Cannot find char at position and index of char of a string var in Swift 2

Equivalent functions in Swift 2 of Java's charAt() and indexOf() ?
Firstly read this article about Swift strings and think about exactly what you mean by characters.
You can use the character view (or the utf16 view if that is the sort of characters that you want) of the string to see it as a collection and if you really need to get characters by index (rather than by iteration) you may want to convert it to an array but normally you just need to advance the index.
let myString = "Hello, Stack overflow"
// Note this index is not an integer but an index into a character view (String.CharacterView.Index)
let index = myString.characters.indexOf( "," )
let character = myString[myString.startIndex.advancedBy(4)] // "o"
This is O(n) (where n is the number of characters into the String) as it needs to iterate over the array as Characters may vary in length in the encoding)
Old answer below. The character array may be quicker for repeat access still as the array accesses are O(1) following the one off O(n) conversion to array (n is the array length).
let cIndex = 5
// This initialises a new array from the characters collection
let characters = [Character](myString.characters)
if cIndex < characters.count {
let character = characters[cIndex]
// Use the character here
}
Obviously some simplification is possible if the index is guaranteed to be within the length of the characters but I prefer to demonstrate with some safety on SO.
You can extend the String class with the missing charAt(index: Int) function:
extension String {
func charAt(index: Int) -> Character {
return [Character](characters)[index]
}
}
You have to convert the input string to an Array then return the character from the specific index here is the code
extension String {
func charAt(_ index : Int) -> Character {
let arr = Array(self.characters);
return (arr[index]);
}
}
This will work same as Java's charAt() method you can use it as
var str:String = "Alex"
print(str.charAt(1))
Did you read the NSString documentation? String and NSString are not identical, but as mentioned by this answer most functions are more or less the same. Anyway, the two exact functions you ask for are right there:
Java's charAt:
func characterAtIndex(_ index: Int) -> unichar
Java's indexOf:
func rangeOfString(_ searchString: String) -> NSRange
The NSRange data type contains just 2 variables: location and length that tell you where the substring is in the original string and how long it is. See the documentation.
//Java
"abc".indexOf("b") => 1
//Swift
"abc".rangeOfString("b").location #=> 1
you can used it instead of CharAt() in swift 3:
func charAt(str: String , int :Int)->Character{
str[str.startIndex]
let index = str.index(str.startIndex, offsetBy: int)
return str[index]
}

Check if string is already a currency string?

I would like to create a function that looks at a string, and if it's a decimal string, returns it as a currency-formatted string. The function below does that, however if I pass in a string that is already formatted, it will fail of course (it expects to see a string like '25' or '25.55' but not '$15.25'
Is there a way to modify my function below to add another if condition that says "if you've already been formatted as a currency string, or your string is not in the right format, return X" (maybe X will be 0, or maybe it will be self (the same string) i'm not sure yet).
func toCurrencyStringFromDecimalString() -> String
{
var numberFormatter = NSNumberFormatter()
numberFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
if (self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()).utf16Count == 0)
{
//If whitespace is passed in, just return 0.0 as default
return numberFormatter.stringFromNumber(NSDecimalNumber(string: "0.0"))!
}
else if (IS_NOT_A_DECIMAL_OR_ALREADY_A_CURRENCY_STRING)
{
//So obviously this would go here to see if it's not a decimal (or already contains a current placeholder etc)
}
else
{
return numberFormatter.stringFromNumber(NSDecimalNumber(string: self))!
}
}
Thank you for your help!
Sounds like you need to use NSScanner.
According to the docs, the scanDecimal function of NSScanner:
Skips past excess digits in the case of overflow, so the receiver’s
position is past the entire integer representation.
Invoke this method with NULL as value to simply scan past a decimal integer representation.
I've been mostly programming in Obj-C so my Swift is rubbish, but here's my attempt at translating the appropriate code for detecting numeric strings (as also demonstrated in this answer):
let scanner: NSScanner = NSScanner(string:self)
let isNumeric = scanner.scanDecimal(nil) && scanner.atEnd
If the string is not a decimal representation, isNumeric should return false.

Resources