NSString -> c string warning

Ask a Question related to Mac Programming, Design and Development.

  1. #1

    Default Re: NSString -> c string warning

    In <clund-E17CA6.11533517082003@amstwist00.chello.com> C Lund wrote:
    > The following:
    >
    > char *textString;
    > textString = [@"abcdefghijklmnopqrstuvwxyz" cString];
    >
    > gives me an "assignment discards qualifiers from pointer target type"
    > warning.
    Because you didn't say const. m.


    --
    matt neuburg, phd = [email]matt@tidbits.com[/email], [url]http://www.tidbits.com/matt[/url]
    REALbasic: The Definitive Guide! 2nd edition!
    [url]http://www.amazon.com/exec/obidos/ASIN/0596001770/somethingsbymatt[/url]
    Subscribe to TidBITS. It's free and smart.
    matt neuburg Guest

  2. Similar Questions and Discussions

    1. new warning in 1.8 from String#split
      I've got a little email filtering program that I've been using for a while that has the line: msg.header.split("\r\n").grep(/^From: /) When I...
    2. NSString in NSDateFormat'ed cell
      Hi, can anyone suggest a quick way to show an NSString in an NSTableView cell that has been formatted as an NSDateFormat ? It's a dynamic special...
    3. Cocoa: put returns in NSString to keep it to a certain width
      I have an NSString. Sometimes it contains only a tiny bit of text but sometimes it's more than that. If it'll be wider than a certain amount I...
    4. regexp warning in String.split?
      irb(main):001:0> 'a,b'.split(',') => irb(main):004:0> 'a => b'.split(' => ') (irb):4: warning: string pattern instead of regexp; metacharacters...
    5. Escape sequence for unicode characters in NSString
      What is the escape sequence for a unicode character in an NSString? I want to do something like NSString *stuff = @"The Greek letter Beta looks...
  3. #2

    Default Re: NSString -> c string warning

    In article <clund-E17CA6.11533517082003@amstwist00.chello.com>,
    C Lund <clund@NOSPAMnotam02.no> wrote:
    > The following:
    >
    > char *textString;
    > textString = [@"abcdefghijklmnopqrstuvwxyz" cString];
    >
    > gives me an "assignment discards qualifiers from pointer target type"
    > warning.
    >
    > It works the way it's supposed to, but I *am* getting a warning. Is this
    > about a loss of information I don't need or a symptom of something more
    > serious? Should I just ignore it?
    >
    > "lossyCString" gives the same result, btw.
    As another poster said, you need your variable declared const, because
    these methods return const strings. In other words, you're not allowed
    to change the string they return.

    These methods are fairly strange. They return a string pointer that is
    effectively autoreleased, even though you can't autorelease things that
    aren't objects. I think they make an NSData object that contains the
    string, and give you the pointer to NSDatat's bytes. The point of this
    is that your textString will get pulled out from underneath you whenever
    the autorelease pool is popped. This may be ok for your application, but
    if it's not, copy the returned string so you have control over how long
    it stays.

    Also, most of the time, [string UTF8String] is what you'll want to use.
    This will give you ASCII codes for ASCII characters, and use a non-lossy
    unicode encoding for non-ASCII characters that won't interfere with any
    C-string handling routines. This lets you correctly store non-english
    scripts in C strings, should you need to do so. And you never know what
    the user might type into a text box.

    If you know for sure that you don't have any non-ASCII, then UTF8String
    and cString will be the same except possibly for performance. So why not
    just use UTF8String everywhere, and you won't accidentally use cString
    for something the user has control over.

    Ok, this post is probably about 50% too long, so I'll stop now. :P
    Michael Ash Guest

  4. #3

    Default Re: NSString -> c string warning

    In article <mail-52536A.10050317082003@localhost>,
    Michael Ash <mail@mikeash.com> wrote:
    > > char *textString;
    > > textString = [@"abcdefghijklmnopqrstuvwxyz" cString];
    > As another poster said, you need your variable declared const, because
    > these methods return const strings. In other words, you're not allowed
    > to change the string they return.
    textString was to be sent to another class. Recieving it as a "const
    char *" did the trick.
    > Also, most of the time, [string UTF8String] is what you'll want to use.
    > This will give you ASCII codes for ASCII characters, and use a non-lossy
    > unicode encoding for non-ASCII characters that won't interfere with any
    > C-string handling routines. This lets you correctly store non-english
    > scripts in C strings, should you need to do so. And you never know what
    > the user might type into a text box.
    Makes sense. It makes no difference for my current use, but I might as
    well make a habit of it.

    --
    C Lund, [url]www.notam02.no/~clund[/url]
    C Lund Guest

  5. #4

    Default Re: NSString -> c string warning

    In <clund-136B47.18324217082003@amstwist00.chello.com> C Lund wrote:
    > In article <mail-52536A.10050317082003@localhost>,
    > Michael Ash <mail@mikeash.com> wrote:
    >> Also, most of the time, [string UTF8String] is what you'll want to
    >> use. This will give you ASCII codes for ASCII characters, and use a
    >> non-lossy unicode encoding for non-ASCII characters that won't
    >> interfere with any C-string handling routines. This lets you
    >> correctly store non-english scripts in C strings, should you need to
    >> do so. And you never know what the user might type into a text box.
    >
    > Makes sense. It makes no difference for my current use
    Not so. Listen more attentively to what Michael is trying to tell you.
    cString has been deprecated for a year now, with a warning that it may
    not even be supported in future versions of the system. Never use it. It
    has a dangerous feature: it relies upon the "default encoding", and you
    have no way of controlling this. So cString can have different results
    on different machines. m.

    --
    matt neuburg, phd = [email]matt@tidbits.com[/email], [url]http://www.tidbits.com/matt[/url]
    REALbasic: The Definitive Guide! 2nd edition!
    [url]http://www.amazon.com/exec/obidos/ASIN/0596001770/somethingsbymatt[/url]
    Subscribe to TidBITS. It's free and smart.
    matt neuburg Guest

Posting Permissions

  • You may not post new threads
  • You may 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