Professional Web Applications Themes

Help: Can someone tell me the right way to use an NSString? - Mac Programming

Can someone tell me if this is the wrong way to use a NSString? Let's say I want to draw 3 lines. Is this the wrong way to do it? NSString *name; name = "Line 1"; point.origin.x += point.size.width; [name drawInRect:point withAttributes:attr]; name = "Line 2"; point.origin.x += point.size.width; [name drawInRect:point withAttributes:attr]; name = "Line 3"; point.origin.x += point.size.width; [name drawInRect:point withAttributes:attr]; Do I have to do it this way? name = [[NSString alloc] initWithFormat:"%s", "Line 1"]; point.origin.x += point.size.width; [name drawInRect:point withAttributes:attr]; [name release]; name = [[NSString alloc] initWithFormat:"%s", "Line 2"]; point.origin.x += point.size.width; [name drawInRect:point withAttributes:attr]; [name release]; ...

  1. #1

    Default Help: Can someone tell me the right way to use an NSString?

    Can someone tell me if this is the wrong way to use a NSString?

    Let's say I want to draw 3 lines.

    Is this the wrong way to do it?

    NSString *name;

    name = "Line 1";
    point.origin.x += point.size.width;
    [name drawInRect:point withAttributes:attr];

    name = "Line 2";
    point.origin.x += point.size.width;
    [name drawInRect:point withAttributes:attr];

    name = "Line 3";
    point.origin.x += point.size.width;
    [name drawInRect:point withAttributes:attr];


    Do I have to do it this way?

    name = [[NSString alloc] initWithFormat:"%s", "Line 1"];
    point.origin.x += point.size.width;
    [name drawInRect:point withAttributes:attr];
    [name release];

    name = [[NSString alloc] initWithFormat:"%s", "Line 2"];
    point.origin.x += point.size.width;
    [name drawInRect:point withAttributes:attr];
    [name release];

    name = [[NSString alloc] initWithFormat:"%s", "Line 3"];
    point.origin.x += point.size.width;
    [name drawInRect:point withAttributes:attr];
    [name release];

    Thanks
    Toan Guest

  2. #2

    Default Re: Help: Can someone tell me the right way to use an NSString?

    Toan <com> wrote:
     

    No. In fact, if you're going to use literals, you can save two lines of
    code:

    point.whatever...;
    ["Line 1" drawInRect:...];

    m.

    --
    matt neuburg, phd = com, http://www.tidbits.com/matt/
    AppleScript: The Definitive Guide
    http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt
    Read TidBITS! It's free and smart. http://www.tidbits.com
    matt Guest

  3. #3

    Default Re: Help: Can someone tell me the right way to use an NSString?

    On Fri, 19 Dec 2003 00:45:32 GMT, com (matt neuburg)
    wrote:
     
    >
    >No. In fact, if you're going to use literals, you can save two lines of
    >code:
    >
    >point.whatever...;
    >["Line 1" drawInRect:...];
    >
    >m.
    >
    >--
    >matt neuburg, phd = com, http://www.tidbits.com/matt/
    >AppleScript: The Definitive Guide
    >http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt
    >Read TidBITS! It's free and smart. http://www.tidbits.com[/ref]

    So, what if I need to do this?

    name = [theRecord objectForKey:"column1"];
    [name drawInRect:point withAttributes:attr];

    name = [theRecord objectForKey:"column2"];
    [name drawInRect:point withAttributes:attr];

    Somehow, something is freezing up on me when I tried to print with
    this code.

    Toan


    Toan Guest

  4. #4

    Default Re: Help: Can someone tell me the right way to use an NSString?

    Toan <com> wrote:
     

    Well, objectForKey:"column1" could be anything, so I'm not going to
    commit myself on that. And "somehow something is freezing up" is kind of
    vague. You've got a debugger; you should know at exactly what point the
    problem is occurring and what the problem is.

    By the way, I do wish you wouldn't call your rect "point". A rect is not
    a point; it's a rect! :) m.

    --
    matt neuburg, phd = com, http://www.tidbits.com/matt/
    AppleScript: The Definitive Guide
    http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt
    Read TidBITS! It's free and smart. http://www.tidbits.com
    matt Guest

  5. #5

    Default Re: Help: Can someone tell me the right way to use an NSString?

    In article <dslindiana.com>,
    com (Toan) wrote:
     

    Err, you realize that drawInRect: will clip the string along the bounds
    of the rect that you pass it. So if "point" is indeed a point, then
    you'll see nothing.

    --
    |\/| /| |2 |<
    mehaase(at)sas(dot)upenn(dot)edu
    Mark Guest

  6. #6

    Default Re: Help: Can someone tell me the right way to use an NSString?

    Mark Haase <net> wrote:
     
    >
    > Err, you realize that drawInRect: will clip the string along the bounds
    > of the rect that you pass it. So if "point" is indeed a point, then
    > you'll see nothing.[/ref]

    Obviously it is not a point, since it has point.origin.x and
    point.size.width... :)

    But the name is misleading. And if the rect has a value of zero in any
    direction, size.width or size.height, then you'll see nothing.

    OP: Why didn't you use NSTableView? Sure, it's a bit more tricky to
    learn, but if you're not doing something NSTableView can't, you'll
    probably save youself much trouble in the future.

    Per
    Per Guest

Similar Threads

  1. NSString in NSDateFormat'ed cell
    By matt in forum Mac Programming
    Replies: 0
    Last Post: September 10th, 04:50 AM
  2. Cocoa: put returns in NSString to keep it to a certain width
    By Simon Slavin in forum Mac Programming
    Replies: 2
    Last Post: September 8th, 10:33 PM
  3. NSString -> c string warning
    By matt neuburg in forum Mac Programming
    Replies: 3
    Last Post: August 17th, 04:55 PM
  4. Escape sequence for unicode characters in NSString
    By Eric Raas in forum Mac Programming
    Replies: 2
    Last Post: July 9th, 02:03 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139