Forum Archive

objc_util Problem

AtomBombed

I am making a highlighter app. It stores everything you've highlighted, and what file you've highlighted it from.

I have a problem, however. I know that all my syntax is correct and that the program should be working. I've tested everything I can think of to try and isolate the problem, and the best conclusion I could come to was that whenever I do setAttributes_range_(styleshere,NSRange(?,?)) it crashes because I am using the currently selected text for the start and ending index ranges in the NSRange object. I don't know why this affects it, but when I use predefined indexes and run the program, it does everything just fine.

omz

How exactly are you getting the selected range?

AtomBombed

Through ui.TextView().selected_range the attribute for my TextView.

JonB

TextView.selected_range
A 2-tuple (start, end) that represents the current user selection. When setting this, a ValueError is thrown for ranges that are out of bounds.

Whereas NSrange takes a start, length.

AtomBombed

TextView.attr_text.addAttributes_range_(d,NSRange(TextView.selected_range[0],len(TextView.text)-(len(TextView.text)-TextView.selected_range[1])))

Where attr_text is an attribute I set myself that stores the atrributed text of the TextView. Just a simpler way to find it when I need it.

And also where d is a dictionary of attributes I have.

This doesn't work either. I tried to use the little bit of math in the second paramter of NSRange to grab the length of which I need.

JonB

the second param should just be selected_range[2]-selected_range[1].

try creating the NSRange, and check the length is valid before you call the set attribute method. Perhaps you have an invalid selected range? (-1 for example)

Perhaps you could post a minimal example of something which works for hard coded values, but not for programattically generated values?

Also... you have a mutable attributed string, not a concrete one, right? and you have enabled editing?

AtomBombed

Yes to the last couple questions. Let me look through it real quick. Thanks so much!

JonB

By the way, here is a simple example showing a similar problem, though using a regexp range instead of selected_text, but same basic idea.