Questions tagged [nsrange]
A structure used to describe a portion of a series—such as characters in a string or objects in an NSArray object.
nsrange
343
questions
295
votes
16
answers
144k
views
NSRange to Range<String.Index>
How can I convert NSRange to Range<String.Index> in Swift?
I want to use the following UITextFieldDelegate method:
func textField(textField: UITextField!,
...
219
votes
13
answers
171k
views
NSRange from Swift Range?
Problem: NSAttributedString takes an NSRange while I'm using a Swift String that uses Range
let text = "Long paragraph saying something goes here!"
let textRange = text.startIndex..<text.endIndex
...
96
votes
10
answers
100k
views
How shouldChangeCharactersInRange works in Swift?
I'm using shouldChangeCharactersInRange as a way of using on-the-fly type search.
However I'm having a problem, shouldChangeCharactersInRange gets called before the text field actually updates:
In ...
72
votes
6
answers
39k
views
Shortcut to generate an NSRange for entire length of NSString?
Is there a short way to say "entire string" rather than typing out:
NSMakeRange(0, myString.length)]
It seems silly that the longest part of this kind of code is the least important (because I ...
63
votes
9
answers
67k
views
Make part of a UILabel bold in Swift
I have a UILabel I've made programmatically as:
var label = UILabel()
I've then declared some styling for the label, including a font, such as:
label.frame = CGRect(x: 20, y: myHeaderView.frame....
59
votes
4
answers
20k
views
How do I store an NSRange in a NSMutableArray or other container?
Here's what I want to do:
NSRange r = NSMakeRange(0,5);
id a = [NSMutableArray a];
[a addObject: r]; // but NSRange is not a NSObject *
With a boolean, I'd use code like this:
[a addObject: [...
56
votes
7
answers
35k
views
Create UITextRange from NSRange
I need to find the pixel-frame for different ranges in a textview. I'm using the - (CGRect)firstRectForRange:(UITextRange *)range; to do it. However I can't find out how to actually create a ...
46
votes
2
answers
3k
views
NSRangeException Out of Bounds error
I am setting the attributed text of a label, and I am getting this strange error: Terminating app due to uncaught exception 'NSRangeException', reason: 'NSMutableRLEArray replaceObjectsInRange:...
35
votes
3
answers
26k
views
How to capture last 4 characters from NSString
I am accepting an NSString of random size from a UITextField and passing it over to a method that I am creating that will capture only the last 4 characters entered in the string.
I have looked ...
33
votes
4
answers
20k
views
Convert selectedTextRange UITextRange to NSRange
How can I convert a UITextRange object to an NSRange? I've seen plenty of SO posts about going the other direction but that's the opposite of what I need. I'm using the UITextRange selectedTextRange ...
31
votes
2
answers
5k
views
Difference between NSRange and NSMakeRange
Is there any difference between:
NSRange(location: 0, length: 5)
and:
NSMakeRange(0, 5)
Because Swiftlint throws a warning when I use NSMakeRange, but I don't know why.
Thanks for the Help :-)
30
votes
2
answers
57k
views
Extracting a string with substringWithRange: gives "index out of bounds"
When I try to extract a string from a larger string it gives me a range or index out of bounds error. I might be overlooking something really obvious here. Thanks.
NSString *title = [TBXML ...
26
votes
2
answers
7k
views
How to print a NSRange in NSLog
Say my range is created as
NSRange myRange = {0,100};
How do I print myRange in NSLog? The following is not working
NSLog(@"my range is %@",myRange);
22
votes
1
answer
40k
views
Getting index of a character in NSString with offset & using substring in Objective-C
I have a string!
NSString *myString=[NSString stringWithFormat:@"This is my lovely string"];
What I want to do is:
Assuming the first character in the string is at index 0. Go to the 11th ...
22
votes
1
answer
4k
views
NSAttributedString and emojis: issue with positions and lengths
I'm coloring some parts of a text coming from an API (think "@mention" as on Twitter) using NSAttributedString.
The API gives me the text and an array of entities representing the parts of the text ...
21
votes
3
answers
30k
views
How to create a subarray of NSArray using NSRange?
I have an Array with content. as usual it contain 20 objects. I want the same array split into 2 sections in Tableview.
I am trying to implement it with NSMake in current array. For example I need get ...
20
votes
4
answers
19k
views
How to get all NSRange of a particular character in a NSString?
I have two NSStrings: orgText and searchLetter.
I want to highlight every occurrences of the searchLetter in the orgText with a red color.
How can I get the NSRange of all occurrences of the ...
20
votes
2
answers
11k
views
Find Range of substring in NSMutableAttributedString
I have AttributedString with emoji like this "🤣🤣🤣 @Mervin tester 🤣🤣🤣"
Now I need to find a range of Mervin in this attributed String.
let attributedString = NSMutableAttributedString(string: "...
19
votes
1
answer
5k
views
lint Legacy Constructor Violation: (legacy_constructor) NSMakeRange in Swift?
What is the Swift constructor for the code below?
NSMakeRange(0, textfield.text!.characters.count)
18
votes
1
answer
23k
views
NSMutableRLEArray objectAtIndex:effectiveRange:: Out of bounds
I try to assign attributes to 3 last chars of newClock string, which is @"3:33:23".
However I get an error when construct NSRange:
NSMutableAttributedString *mas = [[NSMutableAttributedString alloc]...
18
votes
2
answers
11k
views
Cannot convert value of type 'NSRange' (aka '_NSRange') to expected type 'Range<Index>'(aka 'Range<String.CharacterView.Index>')
Getting this error when checking the range for string characters...
@objc func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> ...
16
votes
2
answers
21k
views
String, substring, Range, NSRange in Swift 4
I am using the following code to get a String substring from an NSRange:
func substring(with nsrange: NSRange) -> String? {
guard let range = Range.init(nsrange)
else { return nil }
...
16
votes
3
answers
22k
views
NSRange: range.location != NSNotFound vs. range.length > 0
I'm going through some older code in one of my apps and fixing up the code in areas that could be potentially problematic.
I'm seeing a lot of old code using...
NSRange range = //determine range ...
15
votes
3
answers
21k
views
Cannot convert value of type 'NSRange' (aka '_NSRange') to expected argument type 'Range<Index>' (aka 'Range<String.CharacterView.Index>')
I Have a error in my code like "Cannot convert value of type 'NSRange' (aka '_NSRange') to expected argument type 'Range' (aka 'Range')" but I don't know how to solve this please any one help me?
...
15
votes
3
answers
17k
views
Create an NSRange with a given minimal and maximal value
I have a collection of int64_t values that I need to make an index set out of. I previously have used:
NSIndexSet *set = [NSIndexSet indexSetWithRange:NSMakeRange(location, length)];
to make index ...
13
votes
4
answers
8k
views
swift range greater than lower bound
I need to implement experience filter like this
0 to 2 years
2+ to 4 years
How to express it in swift range?
Problem is I can't express more than 2 to 4 years. While I can do less than upper ...
12
votes
2
answers
24k
views
Problem with NSRange
I'm having a problem with NSRange. Here is my code:
NSRange range = [[[NSHTTPCookie requestHeaderFieldsWithCookies:[[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[NSURL URLWithString:...
11
votes
2
answers
14k
views
NSMutableAttributedStrings - objectAtIndex:effectiveRange:: Out of bounds
I'm trying to add some fancy text to a label, but I've run into some problems with the NSMutableAttributedString class. I was trying to achieve four this: 1. Change font, 2. Underline range, 3. Change ...
11
votes
1
answer
7k
views
Get a range from a string
I want to check if a string contains only numerals. I came across this answer written in Objective-C.
NSRange range = [myTextField.text rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet]];
...
11
votes
4
answers
6k
views
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 ...
11
votes
2
answers
8k
views
Using Range<Index> with NSRange in the NSAttributedString API
I'm attempting to determine the indexes of occurrences of a given string in a String, then generate an NSRange using those indexes in order to add attributes to an NSMutableAttributedString. The ...
10
votes
2
answers
7k
views
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 ...
10
votes
2
answers
549
views
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 $...
10
votes
1
answer
3k
views
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 ...
9
votes
1
answer
8k
views
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 ...
9
votes
3
answers
6k
views
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 ...
9
votes
1
answer
10k
views
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 ...
9
votes
3
answers
5k
views
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 ...
9
votes
5
answers
7k
views
Convert Range<Int> to Range<String.Index>
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 = ...
8
votes
1
answer
3k
views
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: [...
8
votes
1
answer
9k
views
How to use NSRange [duplicate]
I am using NSRange with an Attributed string, but I don't fully understand which parts of the text will be bolded and which are not. This is what I tried:
NSRange boldedRange = NSMakeRange(2, 4);
...
8
votes
3
answers
806
views
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 ...
8
votes
0
answers
2k
views
NSMakeRange: Legacy Constructor Violation: Swift constructors are preferred over legacy convenience functions. [duplicate]
I am using the following code for providing with the line spacing between the UILabel text.
let daysAttrString = NSMutableAttributedString(string: allDays)
let paragraphStyle = ...
7
votes
1
answer
9k
views
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 #...
7
votes
4
answers
8k
views
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. ...
7
votes
1
answer
9k
views
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<Int>' has no member 'map'
let ...
7
votes
2
answers
5k
views
How to pull an integer value from NSRange?
I have an NSRange and need to break a string into two substrings on either side of this NSRange. How do I get an integer value (like the index) out of an NSRange?
6
votes
4
answers
22k
views
How can I use NSRange with integers to simplify my code?
I've just started learning Objective-C and made a little compass app that will display a direction when it falls into a range of headings. It works just fine, but I wonder if there is a more concise ...
5
votes
1
answer
5k
views
indexSetWithIndexesInRange is not doing what expected
I want to select some objects from an array. Therefore I'm using begin and end indexes of my selection.
NSLog(@"start:%d\nend:%d", startIndex, endIndex);
NSIndexSet *myIndexes = [NSIndexSet ...
5
votes
3
answers
2k
views
How can I remove any substring that has parentheses () in my NSString?
I have the following NSString:
Hello (my name is) John
How do I remove the (my name is) part quickly and easily? I want to make sure it handles situations where there is only a ( and then a ...