| Index | Submit class | Submit snippet | Submission feed | List |

NSString (ComBelkadanUtilsKVO)

Language: Objective-C, Author: Belkadan Software
License: MIT/X11

A category for the rare instances where you need to use KVO with NSString/NSMutableString. This allows you to use the key "string" for KVO or Cocoa bindings.

Example usage: binding to an NSArrayController that refers to an array of mutable strings. (Without a key path you get an error.)

Note: full name of the category is ComBelkadanUtils_NSStringKVO to avoid name collisions. You can change this if you want.

NSString (ComBelkadanUtilsKVO) source preview

/*******************************************************************************
 Copyright (c) 2005-2006 Jordy Rose
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
 
 Except as contained in this notice, the name(s) of the above copyright holders
 shall not be used in advertising or otherwise to promote the sale, use or other
 dealings in this Software without prior authorization.
*******************************************************************************/

//
//  NSStringKVO.m
//  Dockyard
//

#import "NSString+ComBelkadanUtilsKVO.h"

#ifndef MAC_OS_X_VERSION_10_4
  #define MAC_OS_X_VERSION_10_4 1040
#endif

/*!
 * NSString now supports KVO! Using the key @"string" will work for both
 * mutable and immutable strings, thanks to this simple category. Works
 * with Cocoa Bindings too!
 */
@implementation NSString (ComBelkadanUtils_NSStringKVO)
/*!
 * Returns an autoreleased copy of this string, suitable for KVC purposes.
 *
 * @functiongroup Accessing the string
 */
- (NSString *)string
{
  return [[self copy] autorelease];
}

#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_4

/*!
 * The normal KVO methods change the class of the observed object as soon as
 * an observer is added. Because NSString is toll-free bridged with CFString,
 * this causes <em>serious</em> problems on pre-10.4 systems (I don't really
 * know why). This makes sure that any string's class will stay the same even
 * after an observer has been added.
 *
 * @functiongroup Overriden KVO methods
 */
- (void)addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context
{
  Class strClass = self->isa;
  [super addObserver:observer forKeyPath:keyPath options:options context:context];
  self->isa = strClass;
}

#endif

@end

NSString (ComBelkadanUtilsKVO) header preview

/*******************************************************************************
 Copyright (c) 2005-2006 Jordy Rose
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
 
 Except as contained in this notice, the name(s) of the above copyright holders
 shall not be used in advertising or otherwise to promote the sale, use or other
 dealings in this Software without prior authorization.
*******************************************************************************/

//
//  NSStringKVO.h
//  Dockyard
//

#import <Foundation/Foundation.h>

@interface NSString (ComBelkadanUtils_NSStringKVO)
- (NSString *)string;
@end
Download Archive

Compatible with:


Comments

Name

Website

Do you hate spammers? (Answer "Yes")