substring(from:)' is deprecated. Use string slicing subscript with a 'partial range from' operator - swift4.2

I have just coverted an app from swift3 to swift4.2 on Xcode 10.1
I am in the process of fixing some of the many errors that have appeared.
Apparently substring(from:)' is deprecated. Use string slicing subscript with a 'partial range from' operator
t_prefix_phone = contact_phone.substring(to:contact_phone.index(contact_phone.startIndex, offsetBy: 3))
t_phone = contact_phone.substring(from:contact_phone.index(contact_phone.endIndex, offsetBy: -7))
Could you please help me translate the code above to 4.2 in such a way that the results are still strings.
Thanks

swift 4 has prefix and suffix just for this:
let contact_phone = "0123456789"
let t_prefix_phone = String(contact_phone.prefix(3))
let t_phone = String(contact_phone.suffix(7))

Related

Too many arguments have been given to this function

I am getting the error, stated in the title, with the following code:
ToText (Split ({?CutOffDate},"-") [2]),ToText (Split ({?CutOffDate},"-") [1]))
Please help fresh programmer find the problem!
Check first if this is a conversion issue, using the CDate function, as in here:
ToText(CDate({aString}), "dd-MMM-yy")
Don't forget the := assignment operator
EffectiveDateTimeString := ToText(CurrentDate + CurrentTime, "dd-MM-yyyy hh:mm:ss");

statement cannot begin with a closure statement on for loop

I have strange issue in Xcode Playground, I have no idea if I had same problem before Xcode 7.
You can see left arrow (less than) position on for loop conditional section, 1st and 3rd syntax has problem and 2nd and 4th have correct syntax...
I am using Xcode 7 and same issue is on Project (not Playground).
Swift beginner mistake: Space around operators is significant. It's
a<b or a < b but not a <b or a< b.
One solution to this problem of space around operators would be to use the "swiftier" way of looping:
for item in array {
// do something with item
}
Same with an index:
for (index, item) in array.enumerate() {
// do something with index and item
}
There's also map to get a modified array from another array:
let result = array.map { item in
// apply transformation to `item`
}
There's no need to continue using the old for var i = 0; ... mechanism anymore in Swift.
This thread seems to indicate this is a bug. https://forums.developer.apple.com/thread/6775

Expression was to complex to be solved in reasonable time...xcode 7 beta

Just got Xcode 7 beta a couple days ago and my code that was working in the previous Xcode is "to complex" for the new Xcode...boggles my mind...any suggestions on how to correct this error or shorten up this variable??
using parse as my backend and just creating search texts for search bars..
thanks!
let searchText = (fullName.text + "" + customerEmail.text + "" + address.text + "" + customerPhone.text).lowercaseString
parseClass["searchText"] = searchText
error* Expression was too complex to be solved in reasonable time; consider breaking up the expression into distinct sub-expressions
As for why you're getting this error, I believe this SO answer describes it nicely.
And as a solution to the problem, I would suggest using String Interpolation:
let searchText = ("\(fullName.text) \(customerEmail.text) \(address.text) \(customerPhone.text)").lowercaseString

In Swift, how do you convert a String to Int64?

I am loading in lines from a text file with very large numbers. String has the toInt method, but how do you convert a string to an Int64 which will be able to handle the large numbers?
I don't see a toInt64 method or a toLong etc. There must be a way, but I haven't found anything by searching yet.
Alternatively I guess I could read the numbers in the string digit by digit (or by groups of digits) and then add them together with appropriate factors of ten, but that seems like overkill. Or maybe the proper way is to read the data in a binary form and convert it that way?
Thanks
As of Swift 2.1.1 you can simply initialize Int64 with String
let number: Int64? = Int64("42")
If you don't mind using NSString, you can do this:
let str = "\(LLONG_MAX)"
let strAsNSString = str as NSString
let value = strAsNSString.longLongValue
Note that unlike toInt(), longLongValue will return 0 if the string is not a legal number whereas toInt() will return nil in that case.
import Darwin
let strBase10 = "9223372036854775807"
let i64FromBase10 = strtoll(strBase10, nil, 10)
let strBase16 = "0xFFFFFFFFFFFFFFFF"
let i64FromBase16 = strtoll(strBase16, nil, 16)
strtoll means STRingTOLongLong
http://www.freebsd.org/cgi/man.cgi?query=strtoll
import Darwin
let str = ...
let i = strtoll(str, nil, 10)

VB6 multidimensional array weird statement

While reviewing a very old VB6 working code I get a very strange statement.
aryValue = aryPersons(8, i)
Where aryValue and aryPersons are multidimensional array and declared as
dim aryPersons, aryValue
Anyone having any idea what is does?
I tried the same in test application but it is giving Type mismatch (Error 13)
ANSWER:
It is my bad to understand the VB code as I was expecting it will be strongly data type language. Actually at aryPersons(8, i) a two dimension array were getting stored and while fetching it gives use a 2D array data that can be easily assigned to aryValue as it is also a 2D array.
It is strange to me that in 2D array at any position you store a any kind of data even another 2D data.
It seems likely that aryStepPersonOptions has an array as its value:
Dim SomeArray(8, 8) As String
Dim aryStepPersonOptions, aryValue
Dim i As Long
SomeArray(8, 8) = "Hello"
aryStepPersonOptions = SomeArray
i = 8
aryValue = aryStepPersonOptions(8, i)
MsgBox aryValue
Of course the pseudo-hungarian ary prefix used seems to do more to add confusion than otherwise. Sadly far too much code contains cargo-culted messes like this. Lets hope nobody copy/pastes my throwaway example SomeArray too.

Resources