Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

North Bronson

macrumors 6502
Original poster
Oct 31, 2007
395
1
San José
Does anyone have experience over-riding the NSArray's description method?

I'm doing something like this:

NSLog (@"%@", myNSArray);

And it's printing something like this:
(
(
A,
B
),
(
C,
D
)
)

where A, B, C, and D are the entries of my 2x2 array.

The problem is that I'm going to input this array into Mathematica. I would like this to print with curly braces instead -- something like this:

{
{
A,
B
},
{
C,
D
}
}

would be fine.

Problem two: A, B, C, and D are actually NSStrings. I'm getting this:

(
(
"stringA",
"stringB"
),
(
"stringC",
"stringD"
)
)

The entries are fractions and I'm representing them with strings. I guess I could either find a way to tell description to print my NSString without double-quotes or I could just declare a Fraction object and build my own description there. Any ideas?
 
You could use Categories here, this is a feature of Objective-C that lets you add methods to classes you didn't write and don't have the source code for.

http://developer.apple.com/document...//apple_ref/doc/uid/TP30001163-CH20-TPXREF139

Code:
@interface NSArray ( MyNSArrayMethods )

- (NSString *)myDescription;

@end 


@implementation NSArray ( MyNSArrayMethods )

- (NSString *)myDescription;
{
    NSString *desc = [self description];
    // change the string how you want

    return desc;
}

@end

The use NSLog (@"%@", [myNSArray myDescription]);
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.