0

if I have a class like

@interface myclass:NSString
   {
       double b;
   }

now if I want to change both the value b and string value of myclass, how should I proceed after alloc init?

Cœur
  • 37,241
  • 25
  • 195
  • 267
neel8986
  • 1
  • 3
  • 2
    You probably want to build a new class that *has* a double and a NSString, not one that *is* an NSString. – Eiko Aug 01 '11 at 07:07
  • I've given an example of Eiko's suggesting in my answer below. – Craig Stanford Aug 01 '11 at 08:00
  • Are you the same user as http://stackoverflow.com/users/872302/user872302? If not, could you explain briefly why you have the same IP address and is posting similar questions? – Lasse V. Karlsen Aug 01 '11 at 08:45

4 Answers4

7

Note, it's not a good idea to subclass NSString.

NSString is not actually such a simple class. And it shouldn't be subclassed without a good reason.

For NSString it is much better to add methods through Categories or object composition.

But note, adding an ivar in a category is not allowed. You can add properties, though, and use associative references in Objective-C 2.0 to access this private data.

There's a good note about subclassing NSStrings right in NSString class reference:

It is possible to subclass NSString (and NSMutableString), but doing so requires providing storage facilities for the string (which is not inherited by subclasses) and implementing two primitive methods. The abstract NSString and NSMutableString classes are the public interface of a class cluster consisting mostly of private, concrete classes that create and return a string object appropriate for a given situation. Making your own concrete subclass of this cluster imposes certain requirements (discussed in “Methods to Override”).

Make sure your reasons for subclassing NSString are valid. Instances of your subclass should represent a string and not something else. Thus the only attributes the subclass should have are the length of the character buffer it’s managing and access to individual characters in the buffer. Valid reasons for making a subclass of NSString include providing a different backing store (perhaps for better performance) or implementing some aspect of object behavior differently, such as memory management. If your purpose is to add non-essential attributes or metadata to your subclass of NSString, a better alternative would be object composition (see “Alternatives to Subclassing”). Cocoa already provides an example of this with the NSAttributedString class.

And on the bottom of the class reference there's another section on NSString subclassing alternatives.

Jacob Gorban
  • 1,431
  • 1
  • 9
  • 15
  • Note that in recent runtimes and compilers, it *is* possible to add properties (and their memory). Still I wouldn't do it as it would bloat *every* instance of an NSString. Subclassing NSString is probably not what makes the most sense here, though. :) – Eiko Aug 01 '11 at 07:06
1

You'll need to expose your new double as a property like so:

@interface MyClass : NSString
{
    double b;
}

@property double b;

@end

then in the .m file:

@implementation MyClass

@synthesize b;

@end

then when you create an instance, you can access the double property:

MyClass* myClassInstance = [[MyClass alloc] init];
myClassInstance.b = 1.2;

EDIT: The above won't work! See Jacob's answer as to why...

As a consolation prize, here's a wrapper class that will give you a String & Double

@interface MyString : NSObject

@property (nonatomic, retain) NSString* string;
@property double b;

- (id)initWithString:(NSString *)aString andDouble:(double)aDouble;

@end

and implementation

@implementation MyString

@synthesize string, b;

- (id)initWithString:(NSString *)aString andDouble:(double)aDouble
{
    self = [super init];
    if (self) {
        self.string = aString;
        self.b = aDouble;
    }

    return self;
}

- (void)dealloc
{
    [string release];
    [super dealloc];
}

@end
Craig Stanford
  • 1,803
  • 1
  • 13
  • 8
1

Jacob Gorban's answer is correct: you probably don't need to subclass the string. In terms of storing extra data, you might consider using objc_setAssociatedObject.

jtbandes
  • 115,675
  • 35
  • 233
  • 266
0

You could see my example with NSString subclassing here:https://stackoverflow.com/a/21331422/1891772

PS. Use it only in case when it's necessary to subclass NSString and you can't use wrappers

Community
  • 1
  • 1
Vlad Papko
  • 13,184
  • 4
  • 41
  • 57