Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a way to get an NSArray of NSRanges for an occurrence of an NSString inside of another NSString?
    primarykey
    data
    text
    <p>I have been recently working on an easy to use Syntax Highlighting system for a personal project. So far, I have everything working properly by finding the range of specific strings and highlighting that range in the NSTextView. Everything works until you try to type a highlighted word twice, which causes the error demonstrated below:</p> <pre><code>&lt;div class="container"&gt; &lt;div&gt; &lt;!-- This &lt;div&gt; would not get highlighted --&gt; Hello World &lt;/div&gt; &lt;/div&gt; &lt;!-- this &lt;/div&gt; would not get highlighted --&gt; </code></pre> <p>I believe this is occurring because I am only getting the first occurrence of an NSString using the rangeOfString method, and for that reason, only the first highlighted item is being highlighted.</p> <p>Sorry for the long explanation! Anyway, I was wondering if there was an rangesOfString method or something similar that can give me an NSArray of NSRanges for each occurrence of an NSString inside of another NSString. Below is the line of code I am using to get my range currently:</p> <pre><code>NSString *textViewString = [textView string]; NSString *textToHighlight = @"&lt;div&gt;"; NSRange area; area.location = 0; area.length = [textViewString length]; NSRange range = [textViewString rangeOfString: textToHighlight options: NSCaseInsensitiveSearch range: area]; </code></pre> <p>What I would like is something like this:</p> <pre><code>NSString *textViewString = [textView string]; NSString *textToHighlight = @"&lt;div&gt;"; NSRange area; area.location = 0; area.length = [textViewString length]; NSArray *ranges = [textViewString rangesOfString: textToHighlight options: NSCaseInsensitiveSearch range: area]; int i; int total = [ranges count]; for (i = 0; i &lt; total; i++) { NSRange occurrence = [ranges objectAtIndex: i]; // Apply color highlighting here } </code></pre> <p>If this is not possible, could someone please point me in the right direction or suggest an alternative way of doing syntax highlighting? Thank you!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload