[Pyobjc-dev] NSString & mutability
Brought to you by:
ronaldoussoren
From: Just v. R. <ju...@le...> - 2003-02-04 20:22:16
|
Someone on comp.sys.mac.programmer.help suggested a trick to find out whether an NS[CF]String is mutable or not: by comparing classForCoder to NSString. Nifty. This allowed me to patch objc_support.m so it only converts immutable strings to Python strings: Index: objc_support.m =================================================================== RCS file: /cvsroot/pyobjc/pyobjc/Modules/objc/objc_support.m,v retrieving revision 1.20 diff -c -r1.20 objc_support.m *** objc_support.m 31 Jan 2003 10:35:29 -0000 1.20 --- objc_support.m 4 Feb 2003 20:11:36 -0000 *************** *** 614,620 **** retobject = [(OC_PythonDictionary *)obj pyObject]; Py_INCREF(retobject); } ! else if ([obj isKindOfClass:[NSString class]]) { /* All string classes seem to be subclasses of NSString. * We convert to a python string where possible, and a python --- 614,621 ---- retobject = [(OC_PythonDictionary *)obj pyObject]; Py_INCREF(retobject); } ! else if ([obj isKindOfClass:[NSString class]] && ! [obj classForCoder] == [NSString class]) { /* All string classes seem to be subclasses of NSString. * We convert to a python string where possible, and a python This works nicely: >>> from Foundation import * >>> x = NSMutableString.stringWithString_("asdasd") >>> x <NSCFString objective-c instance 0x430840> >>> x.appendString_("aaa") >>> repr(x) '<NSCFString objective-c instance 0x430840>' However, str() fails: >>> str(x) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: __str__ returned non-string (type NSCFString) >>> I can't seem to find where I can repair this. Bill, this is not to say I'm _completely_ opposed to wrapping instead of converting strings, but more that I'd like to see how "partial conversion" (ie. don't convert if the string is mutable) works in practice. Just |