[myNSArray description]

Discussion in 'Mac Programming' started by North Bronson, Oct 30, 2008.

  1. North Bronson macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #1
    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?
     
  2. SydneyDev macrumors 6502

    Joined:
    Sep 15, 2008
    #2
    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]);
     

Share This Page