objective c - Exception raised- NSTextView delegate- textViewDidChangeSelection: notification -


i creating first mac app, text editor. document-based, , document.xib has nstextview. have made document class delegate of textview. implementing method:

-(void)textviewdidchangeselection:(nsnotification *)notification { nsrange range=self.textview.selectedrange; nslog(@" %@ ",[[self.textview textstorage] attributesatindex: range.location                                               effectiverange: &range]);  

i using method call inside nslog attributes of selected text , update notification method underline button(pressed or not). problem when app runs , press key exception raised: uncaught exception raised

*** -[nsconcretetextstorage attributesatindex:effectiverange:]: range or index    out of bounds 

i tried debugging @try: @catch: block , seems method above throws exception. if replace:

range.location 

with

(range.location-1) 

it throws exception when cursor @ index 0.

does know happening?

effectiverange not range used method determine range scan attributes. range contain, upon return, range on attributes , values same @ index attributeatindex (i.e. method assign different value range variable).

if want limit range used method attributes selected range, use following method rangelimit set selected range:

(nsdictionary *)attributesatindex:(nsuinteger)index longesteffectiverange:(nsrangepointer)arange inrange:(nsrange)rangelimit

-(void)textviewdidchangeselection:(nsnotification *)notification {     nsrange selectedrange=self.textview.selectedrange;     nsrange effectiverange;     if(selectedrange.length > 0) {         nslog(@" %@ ",[[self.textview textstorage] attributesatindex:selectedrange.location        longesteffectiverange:&effectiverange inrange:selectedrange]);      } } 

it appears method not work when selectedrange length 0. why selectedrange.length checked before calling it.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -