Cocoa: Testing to find if an NSString is immutable or mutable?

后端 未结 3 1813
伪装坚强ぢ
伪装坚强ぢ 2020-12-18 03:26

This produces an immutable string object:

NSString* myStringA = @\"A\";  //CORRECTED FROM: NSMutableString* myStringA = @\"A\";

This produc

相关标签:
3条回答
  • 2020-12-18 03:49

    If you want to check for debugging purposes the following code should work. Copy on immutable object is itself, while it's a true copy for mutable types, that's what the code is based on. Note that since it's calling copy it's slow, but should be fine for debugging. If you'd like to check for any other reasons than debugging see Rob answer (and forget about it).

    BOOL isMutable(id object)
    {
       id copy = [object copy];
       BOOL copyIsADifferentObject = (copy != object);
       [copy release];
       return copyIsADifferentObject;
    }
    

    Disclaimer: of course there is no guarantee that copy is equivalent with retain for immutable types. You can be sure that if isMutable returns NO then it's not mutable so the function should be probably named canBeMutable. In the real world however, it's a pretty safe assumption that immutable types (NSString,NSArray) will implement this optimization. There is a lot of code out including basic things like NSDictionary that expects fast copy from immutable types.

    0 讨论(0)
  • 2020-12-18 04:07

    The docs include a fairly long explanation on why Apple doesn't want you to do this and why they explicitly do not support it in Receiving Mutable Objects. The summary is:

    So don’t make a decision on object mutability based on what introspection tells you about an object. Treat objects as mutable or not based on what you are handed at the API boundaries (that is, based on the return type). If you need to unambiguously mark an object as mutable or immutable when you pass it to clients, pass that information as a flag along with the object.

    I find their NSView example the easiest to understand, and it illustrates a basic Cocoa problem. You have an NSMutableArray called "elements" that you want to expose as an array, but don't want callers to mess with. You have several options:

    1. Expose your NSMutableArray as an NSArray.
    2. Always make a non-mutable copy when requested
    3. Store elements as an NSArray and create a new array every time it mutates.

    I've done all of these at various points. #1 is by far the simplest and fastest solution. It's also dangerous, since the array might mutate behind the caller's back. But Apple indicates it's what they do in some cases (note the warning for -subviews in NSView). I can confirm that while #2 and #3 are much safer, they can create major performance problems, which is probably why Apple has chosen not to use them on oft-accessed members like -subviews.

    The upshot of all of this is that if you use #1, then introspection will mislead you. You have an NSMutableArray cast as an NSArray, and introspection will indicate that it's mutable (introspection has no way to know otherwise). But you must not mutate it. Only the compile-time type check can tell you that, and so it's the only thing you can trust.

    The fix for this would be some kind of fast copy-on-write immutable version of a mutable data structure. That way #2 could possibly be done with decent performance. I can imagine changes to the NSArray cluster that would allow this, but it doesn't exist in Cocoa today (and could impact NSArray performance in the normal case, making it a non-starter). Even if we had it, there's probably too much code out there that relies on the current behavior to ever allow mutability introspection to be trusted.

    0 讨论(0)
  • 2020-12-18 04:14

    There's no (documented) way to determine if a string is mutable at runtime or not.

    You would expect one of the following would work, but none of them work:

    [[s class] isKindOfClass:[NSMutableString class]]; // always returns false
    [s isMemberOfClass:[NSMutableString class]]; // always returns false
    [s respondsToSelector:@selector(appendString)]; // always returns true
    

    More info here, although it doesn't help you with the problem:

    http://www.cocoabuilder.com/archive/cocoa/111173-mutability.html

    0 讨论(0)
提交回复
热议问题