How can I convince an NSBigMutableString to become a mere NSAttributedString ?

  • I'm working with the text from a multiline NSTextField. When it arrives in the delegate it is represented in an NSBigMutableString. Casting this to a NSAttributedString seems to have no effect on the actual class being used by the NSString cluster.

    I can't seem to find any documentation on NSBigMutableString.

    Can someone with deep experience tell me if I can _safely_ assume that NSBMS will respect all of NSAttributedString's method calls ?

    Also if I have a convenience function in a category, should I rewrite it as a category on NSString ? Or directly on the NSBMS, which seems to be a hidden class? Writing a category on a hidden class just doesn't feel right.

    ~ Erik
  • On Jul 17, 2012, at 4:58 PM, Erik Stainsby <erik.stainsby...> wrote:

    > I'm working with the text from a multiline NSTextField. When it arrives in the delegate it is represented in an NSBigMutableString. Casting this to a NSAttributedString seems to have no effect on the actual class being used by the NSString cluster.
    >
    > I can't seem to find any documentation on NSBigMutableString.
    >
    > Can someone with deep experience tell me if I can _safely_ assume that NSBMS will respect all of NSAttributedString's method calls ?
    >
    > Also if I have a convenience function in a category, should I rewrite it as a category on NSString ? Or directly on the NSBMS, which seems to be a hidden class? Writing a category on a hidden class just doesn't feel right.

    Your assuming you should care that it is an NSBigMutableString in the first place. From what I can see, the delegates all declare that you are receiving an NSString, and as such you should be treating the value you get as an NSString.
    --
    David Duncan
  • Sorry I should have said a little more about my context.  I'm looking at the object passed to the delegate method -control:isValidObject:
    Documentation states: "In validating, the delegate should check the value in the object parameter…"  I am however unable to assess the value since it is being rep'd by an impenetrable class clustered object.

    ~ Erik

    On 2012-07-17, at 5:11 PM, David Duncan <david.duncan...> wrote:

    > On Jul 17, 2012, at 4:58 PM, Erik Stainsby <erik.stainsby...> wrote:
    >
    >> I'm working with the text from a multiline NSTextField. When it arrives in the delegate it is represented in an NSBigMutableString. Casting this to a NSAttributedString seems to have no effect on the actual class being used by the NSString cluster.
    >>
    >> I can't seem to find any documentation on NSBigMutableString.
    >>
    >> Can someone with deep experience tell me if I can _safely_ assume that NSBMS will respect all of NSAttributedString's method calls ?
    >>
    >> Also if I have a convenience function in a category, should I rewrite it as a category on NSString ? Or directly on the NSBMS, which seems to be a hidden class? Writing a category on a hidden class just doesn't feel right.
    >
    >
    > Your assuming you should care that it is an NSBigMutableString in the first place. From what I can see, the delegates all declare that you are receiving an NSString, and as such you should be treating the value you get as an NSString.
    > --
    > David Duncan
    >
  • On Jul 17, 2012, at 6:58 PM, Erik Stainsby wrote:

    > I'm working with the text from a multiline NSTextField. When it arrives in the delegate it is represented in an NSBigMutableString.

    The fact that you felt compelled to investigate its actual dynamic class is a sign that you're on the wrong track.  You should only rely on the documented static type.

    > Casting this to a NSAttributedString seems to have no effect on the actual class being used by the NSString cluster.

    Those are two completely different classes.  An NSAttributedString *is not* an NSString or any variety thereof.  It's a different thing that has-a NSString (not is-a NSString).

    > I can't seem to find any documentation on NSBigMutableString.

    That means it's private.  You should not write any code which embodies any knowledge of its existence.

    > Can someone with deep experience tell me if I can _safely_ assume that NSBMS will respect all of NSAttributedString's method calls?

    No, you can't.  As I said above, an NSAttributeString is not an NSString or any variety of NSString.  They are not interchangeable.

    You can construct an NSAttributedString from an NSString, but then it won't be the same attributed string as the text field is using, so it's unlikely to be helpful.  What are you actually trying to do?  What delegate method are you implementing?

    > Also if I have a convenience function in a category, should I rewrite it as a category on NSString ? Or directly on the NSBMS, which seems to be a hidden class? Writing a category on a hidden class just doesn't feel right.

    You definitely shouldn't write a category on a private class.  I suspect you're on the wrong path in writing a category on NSString for this situation, too.

    Regards,
    Ken
  • On Jul 17, 2012, at 5:19 PM, Erik Stainsby <erik.stainsby...> wrote:

    > Sorry I should have said a little more about my context.  I'm looking at the object passed to the delegate method -control:isValidObject:
    > Documentation states: "In validating, the delegate should check the value in the object parameter…"  I am however unable to assess the value since it is being rep'd by an impenetrable class clustered object.

    Well, you don't know what an "NSBigMutableString" is, so use something you do know. As if it is an NSString or NSAttributedString and work from there. Thats what -isKindOfClass: is for.
    --
    David Duncan
  • Thanks for filling in my knowledge gap, Ken.

    I think between what you and Dave have said, I've realised that I am trying to do too much with the object passed in to -control:isValidObject:
    I was on my way to line-parsing the attributed string to determine if the user-added content is validly formatted.  I think now that would be better done/ought to be done in the -control:textShouldEndEditing: which actually gives me the fieldEditor to work with.

    Thanks for your time.

    On 2012-07-17, at 5:20 PM, Ken Thomases <ken...> wrote:

    > On Jul 17, 2012, at 6:58 PM, Erik Stainsby wrote:
    >
    >> I'm working with the text from a multiline NSTextField. When it arrives in the delegate it is represented in an NSBigMutableString.
    >
    > The fact that you felt compelled to investigate its actual dynamic class is a sign that you're on the wrong track.  You should only rely on the documented static type.
    >
    >> Casting this to a NSAttributedString seems to have no effect on the actual class being used by the NSString cluster.
    >
    > Those are two completely different classes.  An NSAttributedString *is not* an NSString or any variety thereof.  It's a different thing that has-a NSString (not is-a NSString).
    >
    >> I can't seem to find any documentation on NSBigMutableString.
    >
    > That means it's private.  You should not write any code which embodies any knowledge of its existence.
    >
    >> Can someone with deep experience tell me if I can _safely_ assume that NSBMS will respect all of NSAttributedString's method calls?
    >
    > No, you can't.  As I said above, an NSAttributeString is not an NSString or any variety of NSString.  They are not interchangeable.
    >
    > You can construct an NSAttributedString from an NSString, but then it won't be the same attributed string as the text field is using, so it's unlikely to be helpful.  What are you actually trying to do?  What delegate method are you implementing?
    >
    >> Also if I have a convenience function in a category, should I rewrite it as a category on NSString ? Or directly on the NSBMS, which seems to be a hidden class? Writing a category on a hidden class just doesn't feel right.
    >
    > You definitely shouldn't write a category on a private class.  I suspect you're on the wrong path in writing a category on NSString for this situation, too.
    >
    > Regards,
    > Ken
    >
  • On 18 Jul 2012, at 01:19, Erik Stainsby <erik.stainsby...> wrote:

    > Sorry I should have said a little more about my context.  I'm looking at the object passed to the delegate method -control:isValidObject:
    > Documentation states: "In validating, the delegate should check the value in the object parameter…"  I am however unable to assess the value since it is being rep'd by an impenetrable class clustered object.

    Of course you are able to assess the value. If you expect it to be an NSString, inspect its value like any other string. By default, the object value of text fields is an NSString. It'll only be different if you're deliberately working with attributed strings or applied a formatter.
  • On Jul 17, 2012, at 6:58 PM, Erik Stainsby wrote:

    > I'm working with the text from a multiline NSTextField. When it arrives in the delegate it is represented in an NSBigMutableString. Casting this to a NSAttributedString seems to have no effect on the actual class being used by the NSString cluster.
    >
    > I can't seem to find any documentation on NSBigMutableString.
    >
    > Can someone with deep experience tell me if I can _safely_ assume that NSBMS will respect all of NSAttributedString's method calls ?
    >
    > Also if I have a convenience function in a category, should I rewrite it as a category on NSString ? Or directly on the NSBMS, which seems to be a hidden class? Writing a category on a hidden class just doesn't feel right.

    NSBigMutableString is a private subclass of NSString. NSAttributedString is *not* a subclass of NSString; however, you can easily make an NSAttributedString from an NSString (which is what you have). Something like this:

    - (BOOL)control:(NSControl *)control isValidObject:(id)object {
    NSAttributedString *aStr = nil;

    if ([object isKindOfClass:[NSAttributedString class]]) {
      aStr = [object copy]; // or mutableCopy, or just use the given instance, etc.
    } else if ([object isKindOfClass:[NSString class]]) {
      aStr = [[NSAttributedString alloc] initWithString:object];
    }

    // do whatever with your attributed string
    }

    and you should be good. You don’t care whether the class is NSBigMutableString or some other hidden subclass; all you care about is that it’s some kind of NSString (or NSAttributedString).

    Charles
previous month july 2012 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today