Can anyone tell me why the code below works in Swift 2, but somehow breaks in Swift 3?
let range: Range = 0...2
However it can simply be fixed by doing this
let range: Range = 0..<3
Anyone knows what is the reason behind this?
Operators ... and ..< used to produce the same type, Range, in Swift 2.x. Now they produce different types (migration guide):
Range
CountableRange
ClosedRange
CountableClosedRange
Changing the type in the first assignment to ClosedRange should fix the problem. Better yet, let Swift infer the type for you:
let range = 0...2
Related
I can't understand the in label of utf16Offset(in:) statement
what the use of this label.
And can anyone explain that,
Is the following statement is right or wrong?
let location = textString.range(of: hyperText)?.lowerBound.utf16Offset(in: textString)
you code is correct.it means the offset into a string's code units for this index.
for example:
var textString = "niaddjkdskjdsjdsk"
let location = textString.range(of: "dd")?.lowerBound.utf16Offset(in: textString)
as we know, the bound is (3,_),so the utf16Offset is 3.
Before Swift 4.2,we use encodedOffset,now it replace by utf16Offset, they are same behavior
This question already has answers here:
'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range upto' operator
(2 answers)
Closed 3 years ago.
My code was as below
var theReview = addReview.text
let len2 = addReview.text.utf16.count
if len2 > 3000 {
theReview = theReview?.substring(to: (theReview?.index((theReview?.startIndex)!, offsetBy: 3000))!)
}
My aim was to get the first 3000 characters of the text if it is longer than 3000 characters.
However, I get the warning below:
'substring(to:)' is deprecated: Please use String slicing subscript
with a 'partial range upto' operator
What can be an alternative to my code. I am not a very professional coder. So any help would be great.
Simply call prefix(_:) on theReview with the required length.
func prefix(_ maxLength: Int) -> Substring
Returns a subsequence, up to the specified maximum length, containing
the initial elements of the collection.
If the maximum length exceeds the number of elements in the
collection, the result contains all the elements in the collection
var theReview = addReview.text
theReview = String(theReview.prefix(3000))
Note: There is no need to check if the theReview's length exceeds 3000. It will be handled by prefix(_:) itself.
might this could help you
var theReview = addReview.text
theReview = String(theReview.prefix(3000))
Use String init with slice.
let index = theReview.index(theReview.startIndex, offsetBy: 3000)
theReview = String(theReview[theReview.startIndex..<index])
or prefix as mentioned in previous answers
I'm trying to create a substring of the first 4 characters entered in a textField in Swift 4 on my iOS app.
Since the change to Swift 4 I'm struggling with basic String parsing.
So based on Apple documentation I'm assuming I need to use the substring.index function and I understand the second parameter (offsetBy) is the number of characters to create a substring with. I'm just unsure how I tell Swift to start at the beginning of the string.
This is the code so far:
let postcode = textFieldPostcode.text
let newPostcode = postcode?.index(STARTATTHEBEGININGOFTHESTRING, offsetBy: 4)
I hope my explanation makes sense, happy to answer any questions on this.
Thanks,
In Swift 4 you can use
let string = "Hello World"
let first4 = string.prefix(4) // Hell
The type of the result is a new type Substring which behaves very similar to String. However if first4 is supposed to leave the current scope – for example as a return value of a function – it's recommended to create a String explicitly:
let first4 = String(string.prefix(4)) // Hell
See also SE 0163 String Revision 1
In Swift 4:
let postcode = textFieldPostcode.text!
let index = postcode.index(postcode.startIndex, offsetBy: 4)
let newPostCode = String(postcode[..<index])
I have this method in a Regex class:
func test(input:String) -> Bool
{
let matches = expression.matchesInString(input, options: nil, range: NSMakeRange(0, count(input)))
return matches.count > 0
}
Swift 2.1 tells me:
Nil is not compatible with expected argument type 'NSMatchingOptions'
Can somebody tell me how to fix this properly? NSMatchingOptions doesn't seem to offer any default empty property.
If you do not want to pass any options to the regex, use options: [].
In Swift 2 an empty OptionSetType can be represented with <Type>()
NSMatchingOptions()
or just with a pair of square brackets
[]
NSMatchingOptions is an enum, not a class, so nil makes no sense. You have to use one of the enum values, the most likely of which is Anchored. It's really not a Swift issue at all.
I have a subclass of NSTextStorage and I'm trying to remove the foreground color of a paragraph the following way:
var paragraphRange = self.string.paragraphRangeForRange(
advance(self.string.startIndex, theRange.location)..advance(self.string.startIndex, theRange.location + theRange.length))
self.removeAttribute(NSForegroundColorAttributeName, range: paragraphRange)
However, I get the following error Cannot invoke 'removeAttribute' with an argument list of type '(String, range: (Range<String.Index>))'
Help Please. I think TextKit on Swift is a mess. Some methods receive/return NSRange but String works with Range<String.Index> making it a hell to work with.
The problem here is that the NSString returned by self.string
is automatically bridged to a Swift String. A possible solution is
to convert it back to NSString explicitly:
func removeColorForRange(theRange : NSRange) {
let paragraphRange = (self.string as NSString).paragraphRangeForRange(theRange)
self.removeAttribute(NSForegroundColorAttributeName, range: paragraphRange)
}
Note also that the range operator .. has been replaced by ..<
in newer Swift versions (to avoid confusion with ... and to
emphasize that the upper bound is not included).