I found I can compare two numbers in String format directly in Swift.
Initially, I was trying to cast my String format number to double or Int, and then do the comparison. When I accidentally found I can compare them directly, I have done some tests in my playground. It seems the result are all correct, even with the numbers are empty string or "(Double)" vs "(Int)".
(Exceptions): The test cases with negative numbers in the String format will fail the comparing.
Does anyone know if this is a default behavior in Swift String? I am not sure if I can utilize this in my program.
Example:
var numStrs1 = ["15", "12.2", "15", ""]
var numStrs2 = ["13", "12", "", "23.0"]
func compareNumStr(numStr1:String, numStr2:String) -> Bool {
return numStr1 > numStr2
}
for var i = 0; i < numStrs1.count; ++i{
compareNumStr(numStrs1[i], numStrs2[i])
}
What the default implementation of string comparisons rely on is the Unicode collation algorithm source: http://oleb.net/blog/2014/07/swift-strings/
The comparison you're making right now is not a canonical Int/Double comparison, but rather how it translates to unicode. Depending on how you wish to use the information, it may not be suitable for all scenarios. Like you found out with negative numbers, you should use to .toInt method to get the number you want, and compare the integers instead of comparing the strings.
Related
I'm currently working on making a savings app in Xcode 10. I'm working on a feature that lets users add the amount of money they have saved for something into the app through a UI text field. I can't find a way to return the text from the text field to an Integer and add that to the total sum of money that has been saved. Also whenever I tried to add a test variable I got plenty of errors.
var amountSavedSoFar += amountOfMoneySaved
Both I have set to be integers. I'm trying to set amountOfMoneySaved equal to the numbers in the text field, but it doesn't seem to work.
'+=' is not a prefix unary operator
Consecutive statements on a line must be separated by ';'
Type annotation missing in pattern
Unary operator cannot be separated from its operand
You've got a few issues as you mentioned:
amountSavedSoFar is declared in the saveAmount function and will not be persisted if you call that function more than once.
amountSaved.text is not being converted from String to the appropriate type (Int, Double, etc.)
amountSavedSoFar isn't typed or initialized.
Try something like:
var amountSavedSoFar: Int = 0
#IBAction func saveAmount(_ sender: Any) {
//Convert the text and default to zero if conversion fails
amountSavedSoFar += Int(amountSaved.text) ?? 0
}
I'm working on a piece of code that use generics. This is an example of what I'm trying to achieve :
var strings: Array<Array<String>> = [["1", "2", "3"], ["4", "5", "6"]]
var array: Array<Array<AnyObject>> = strings
But the compiler says "'String' is not identical to 'AnyObject'".
I have no idea why the compiler complains and how to achieve what I need.
I already tried casting it like this :
var array: Array<Array<AnyObject>> = strings as Array<Array<AnyObject>>
Without any success.
Do you guys have any idea ?
Thanks.
That doesn't work because, as the compiler says, AnyObject is not String, although you can cast AnyObject to String and vice versa.
It doesn't even work using different value types that may seem "compatible":
var array1: Array<Array<UInt>> = []
var array2: Array<Array<Int>> = array1
The only way to do what you need is to write a converter that given an array containing object of type String returns an array of AnyObject.
The reason is that even if 2 data types are compatible, they do not use the same amount of memory and can have different data members and initialization constraints.
Taking into account that arrays are value types, the assignment is not done by reference - instead a copy of the array is created and assigned to the destination variable. If the underlying data type is the same for left and right side of the assignment, each item in the array can just be copied byte by byte to create a copy of it. If the left and right side have different types, that is not possible because most likely they use memory in a different way (i.e. they may have different data members), so in that case the object should be instantiated via an initializer, but which one and using which parameters?
I am receiving a creation date for an object in a database as milliseconds (# of milliseconds since epoch or whatever) and would like to convert it to/from a string in Swift!
I think I'd need a data type of CUnsignedLong?
I am trying something like this but it outputs the wrong number:
var trial: CUnsignedLong = 1397016000000
println(trial) //outputs 1151628800 instead!
I'm guess this is the wrong data type so what would you all advise in a situation like this?
In Java I was using long which worked.
Thanks!
func currentTimeMillis() -> Int64{
let nowDouble = NSDate().timeIntervalSince1970
return Int64(nowDouble*1000)
}
Working fine
On 32-bit platforms, CUnsignedLong is a 32-bit integer, which is not large
enough to hold the number 1397016000000. (This is different from Java, where
long is generally a 64-bit integer.)
You can use UInt64 or NSTimeInterval (a type alias for Double), which is what the
NSDate methods use.
Easy question here, probably, but searching did not find a similar question.
The # operator finds the length of a string, among other things, great. But with Lua being dynamically typed, thus no conversion operators, how does one type a number as a string in order to determine its length?
For example suppose I want to print the factorials from 1 to 9 in a formatted table.
i,F = 1,1
while i<10 do
print(i.."! == "..string.rep("0",10-#F)..F)
i=i+1
F=F*i
end
error: attempt to get length of global 'F' (a number value)
why not use tostring(F) to convert F to a string?
Alternatively,
length = math.floor(math.log10(number)+1)
Careful though, this will only work where n > 0!
There are probably a dozen ways to do this. The easy way is to use tostring as Dan mentions. You could also concatenate an empty string, e.g. F_str=""..F to get F_str as a string representation. But since you are trying to output a formatted string, use the string.format method to do all the hard work for you:
i,F = 1,1
while i<10 do
print(string.format("%01d! == %010d", i, F))
i=i+1
F=F*i
end
Isn't while tostring(F).len < 10 do useful?
I just want to know the difference between CompareStr and = for comparing strings in Delphi. Both yield the same result.
if(str2[i] = str1[i]) then
ShowMessage('Palindrome')
if(CompareStr(str2[i], str1[i]) = 0) then
ShowMessage('Palindrome')
Both show message Palindrome.
Use CompareStr not when you just want to see whether two strings are equal, but when you want to know how one string compares relative to another. It will return a value less than 0 if the first argument appears first, asciibetically, and it will return a value greater than zero if the first argument belongs after the second.
Without CompareStr, you might have code like this:
if str1[i] = str2[i] then begin
// They're equal
end else if str1[i] < str2[i] then begin
// str1 comes first
end else begin
// str2 comes first
end;
That compares str1 and str2 twice. With CompareStr, you can cut out one of the string comparisons and replace it with a cheaper integer comparison:
x := CompareStr(str1[i], str2[i]);
if x = 0 then begin
// They're equal
end else if x < 0 then begin
// str1 comes first
end else begin
// str2 comes first
end;
As Gerry's answer explains, the function is particularly useful in sorting functions, especially since it has the same interface as other comparison functions like CompareText and AnsiCompareStr. The sorting function is a template method, and each of the functions serves as a comparison strategy.
If all you want to do is test for equality, use the = operator — it's easier to read. Use CompareStr when you need the extra functionality it provides.
Assuming Str1 and Str2 are strings, rather than arrays (or lists) or string, the first version will be more efficient, as the second version will first copy str1[i] and str2[i] to two new strings, then call a function, with the associated overhead.
The first version will simply compare the single characters referred to by str1[i] and str2[i]
If you are only interested if strings are the same, use =. If you need to know if strings are the same, OR which string is greater, then use CompareStr.
CompareStr is particularly useful when sorting lists, e.g, with TList.Sort(CompareFunc) or TStringList.Sort(CompareFunc)
If you want case-insensitive comparisons, use CompareText.
The result is not the same when compared strings are not equal. The result of CompareStr or AnsiCompareStr is of Integer type showing some more information literally how those strings compare. Take a look at http://www.delphibasics.co.uk/RTL.asp?Name=AnsiCompareStr
Apart from the return value (integer versus boolean), from the code it says that for CompareStr "the compare operation is based on the 8-bit ordinal value of each character and is not affected by the current user locale". So it looks like CompareStr was originally part of the FastCode routines and is in essence an optimised Ansi version developed for performance reasons. I have always tended to go with "=", "<", ">", etc.