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
7
votes
1 answer
How to use NSRange
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);
What exactly does the 2 stand for and the 4 stand…

Ameya Vaidya
- 117
- 1
- 9
7
votes
2 answers
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?

user1486548
- 1,201
- 4
- 15
- 22
6
votes
4 answers
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 way of writing it using NSRange. After a lot of…

Matt Miller
- 63
- 1
- 1
- 3
5
votes
1 answer
About pointers in Objective-C
I stumbled across the following and can't seem to work out why this works. Please can you explain why I don't need to use a pointer before range?
NSString *d = @"The quick brown fox";
NSRange range = [d rangeOfString:@"brown"];

vboombatz
- 409
- 6
- 17
5
votes
1 answer
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 indexSetWithIndexesInRange:NSMakeRange(startIndex,…

testing
- 19,681
- 50
- 236
- 417
5
votes
1 answer
Get current paragraph in UITextView?
I want to detect the current paragraph, this is my code so far, but it doesn't work so well. Lets say I have 3 paragraphs and when the cursor is between them, it selects the next one, which is not right. Is there a better way to do this?
With this…

Elida
- 117
- 9
5
votes
3 answers
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 following ) and doesn't crash on anything else.

Ethan Allen
- 14,425
- 24
- 101
- 194
5
votes
2 answers
Text string with EMOJI causing issues with NSRange
I am using TTTAttributedLabel to apply formatting to text, however it seems to crash because I am trying to apply formatting to a range which includes emoji. Example:
NSString *text = @"@user1234 #hashtag"; // text.length reported as 22 by NSLog as…

mootymoots
- 4,545
- 9
- 46
- 74
5
votes
2 answers
Convert NSRange contents to NSString?
I am working on a Cocoa Mac OSX app, and I am wondering if it is possible to present the contents of an NSRange found by:
NSRange range;
range.location = 4;
range.length = 4;
as an NSString?
e.g. in the example above, if I had a string with…

Conor Taylor
- 2,998
- 7
- 37
- 69
4
votes
2 answers
Remove Part of an NSString and copy it into another NSString
So I have an NSString, lets say it: NSString *mainString = @"My Name is Andrew (I like computers)";
And I want remove everything from the "(" to the ")" from mainString. And I want to put everything in between the "()" into a subString.
For…

Andrew
- 3,874
- 5
- 39
- 67
4
votes
1 answer
Unknown escape sequence - Regular expression
I want to validate email text for which I am using RegexKitLite.h. I am doing things as follows :
NSString *strEmail = [txtEmail text];
NSRange range = [strEmail…

Nitish
- 13,845
- 28
- 135
- 263
4
votes
2 answers
Get the NSRange for the visible text after scroll in UITextView
I'm trying to save the location of scrolled text in a UITextView so that I can return to that location upon loading the ViewController again. I have very long strings, so I want the user to be able to scroll to a specific location and then return to…

L.EXE
- 93
- 6
4
votes
1 answer
Extract table from NSAttributedString
Suppose I have read NSAttributedString from a file (html or rtf) and in that file I clearly see several tables. Is there a way to extract those tables or at least find an NSRange that corresponds to the table? It would be ideal if I could somehow…

alagris
- 1,838
- 16
- 31
4
votes
4 answers
Fastest way to get array of NSRange objects for all uppercase letters in an NSString?
I need NSRange objects for the position of each uppercase letter in a given NSString for input into a method for a custom attributed string class.
There are of course quite a few ways to accomplish this such as rangeOfString:options:…

Bama91
- 884
- 2
- 10
- 24
4
votes
2 answers
Treat a single integer value as a range in Swift
I need to validate the length of a string. The allowed values for the character count are:
6 – 9 characters
12 characters
15 characters
All strings with a different character count are invalid. Thus, I would like to create a Swift function that…

Mischa
- 15,816
- 8
- 59
- 117