A structure used to describe a portion of a series—such as characters in a string or objects in an NSArray object.
Questions tagged [nsrange]
343 questions
10
votes
2 answers
encodedOffset deprecation
In my application, I have some code to fetch the range of the host in a URL. It looks like this:
private func rangeOfHost(text: String) -> NSRange? {
let url = URL(string: text)
if let host: String = url?.host {
if let range =…

user4992124
- 1,574
- 1
- 17
- 35
10
votes
1 answer
What is the equivalent value for NSRange.location on the Range Object within Swift 3?
Did someone can give me a hint which property of a Range is the equivalent property to the location property of an NSRange.
Especially I'm interested how I would migrate the following line of code from Swift 2.3 -> Swift 3.0
if myRange.location !=…

matzino
- 3,544
- 1
- 18
- 37
10
votes
2 answers
How to create and use temp NSRange in lldb?
NSRange is just a C struct. I want to create a temporary one in lldb in Xcode at a breakpoint.
Specifically for use in NSArray method objectAtIndex:inRange:
This does not work.
(lldb) expr NSRange $tmpRange = (NSRange){0,4}
(lldb) expr…

Jeff
- 3,829
- 1
- 31
- 49
10
votes
4 answers
Find the number of characters that fits in a UITextView with constant width and height with a given string?
In my application I need to set Read more into the text view if text input is large.so my approach is to find the range of string that would fit in the text view and append See More to it.Is there any way to achieve it in Swift.The requirement is to…

Jacob Davis Cherussery
- 467
- 9
- 24
9
votes
1 answer
NSRangeException from removeObjectsInRange: but passed range is within bounds
I am getting the error below, which makes no sense.
* Terminating app due to uncaught exception 'NSRangeException', reason: '* -[NSMutableArray removeObjectsInRange:]: range {11, 15} extends beyond bounds [0 .. 15]'
What am I doing wrong here? I…

adrian.coroian
- 582
- 4
- 13
9
votes
3 answers
NSRangeException: Call Stack Not Showing Line Number
I am getting the following index out of bounds error:
*** Terminating app due to uncaught exception 'NSRangeException', reason:
'*** -[__NSArrayM objectAtIndex:]: index 0 beyond bounds for empty array'
*** First throw call stack:
(0x2263052…

llama591
- 453
- 5
- 15
9
votes
1 answer
How can I select a string from the beginning until a specified character?
How can I select a string from the beginning until a specified character?
For example, in the following a news headline...
someString = @"Los Angeles, California - Apple announces something, stock prices change."
How do I select just Los Angeles,…

Moshe
- 57,511
- 78
- 272
- 425
9
votes
5 answers
Convert Range to Range
The below function given a NSString, removes the HTML tags from that string and returns the result also as a NSString.
private func removeHTMLTags(source: NSString) -> NSString {
var range = NSMakeRange(0, 0)
let HTMLTags = "<[^>]*>"
…

Isuru
- 30,617
- 60
- 187
- 303
8
votes
3 answers
Match NSArray of characters Objective-C
I have to match the number of occurrences of n special characters in a string.
I thought to create an array with all these chars (they are 20+) and create a function to match each of them.
I just have the total amount of special characters in the…

pasine
- 11,311
- 10
- 49
- 81
8
votes
0 answers
NSMakeRange: Legacy Constructor Violation: Swift constructors are preferred over legacy convenience functions.
I am using the following code for providing with the line spacing between the UILabel text.
let daysAttrString = NSMutableAttributedString(string: allDays)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 7
…

Chelsea Shawra
- 1,593
- 4
- 22
- 43
8
votes
3 answers
Is there an easy method to check is an NSRange passed to substringWithRange on NSString exists (so not to cause an error)?
Say I pass the NSRange of (location: 5, length: 50) on the NSString "foo", that range obviously doesn't exist.
Is there a way to say [string rangeExists:NSRange] for instance, or do we have to manually validate the input?

Doug Smith
- 29,668
- 57
- 204
- 388
7
votes
4 answers
Looping using NSRange
I'm trying to use NSRange to hold a range of years, such as
NSRange years = NSMakeRange(2011, 5);
I know NSRange is used mostly for filtering, however I want to loop over the elements in the range. Is that possible without converting the NSRange…

aporat
- 5,922
- 5
- 32
- 54
7
votes
1 answer
Swift: NSAttributedString & emojis
I use a UITextView with text attributes editing enabled and I am having issue with getting the attributes when there are emojis in the text.
Here is the code I use:
var textAttributes = [(attributes: [NSAttributedString.Key: Any], range:…

Florentin
- 1,433
- 2
- 13
- 22
7
votes
1 answer
How to use range.map in swift 3/4?
I have the code below, which works in swift 2.3. I am struggling to understand how to convert it to swift 3/4 - the issue it those is
Value of type 'Range' has no member 'map'
let grainSize = CGFloat(0.01)
let min = CGFloat(-3)
…

stktrc
- 1,599
- 18
- 30
7
votes
1 answer
How do I uses addAttribute and NSRANGE in swift 3
Hi I'm trying to use addAttribute in Swift3.
I want set bold only IDNAME.
Here is what I am trying to this.
let boldUsername = NSMutableAttributedString(string: "IDNAME hi nice 2 meet you :D #HELLOW")
// …

Shawn Baek
- 1,928
- 3
- 20
- 34