Ask a Question related to Mac Programming, Design and Development.
-
matt neuburg #1
Re: NSString -> c string warning
In <clund-E17CA6.11533517082003@amstwist00.chello.com> C Lund wrote:
Because you didn't say const. m.> The following:
>
> char *textString;
> textString = [@"abcdefghijklmnopqrstuvwxyz" cString];
>
> gives me an "assignment discards qualifiers from pointer target type"
> warning.
--
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
-
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... -
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... -
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... -
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... -
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... -
Michael Ash #2
Re: NSString -> c string warning
In article <clund-E17CA6.11533517082003@amstwist00.chello.com>,
C Lund <clund@NOSPAMnotam02.no> wrote:
As another poster said, you need your variable declared const, because> 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.
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
-
C Lund #3
Re: NSString -> c string warning
In article <mail-52536A.10050317082003@localhost>,
Michael Ash <mail@mikeash.com> wrote:
> > char *textString;
> > textString = [@"abcdefghijklmnopqrstuvwxyz" cString];textString was to be sent to another class. Recieving it as a "const> 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.
char *" did the trick.
Makes sense. It makes no difference for my current use, but I might as> 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.
well make a habit of it.
--
C Lund, [url]www.notam02.no/~clund[/url]
C Lund Guest
-
matt neuburg #4
Re: NSString -> c string warning
In <clund-136B47.18324217082003@amstwist00.chello.com> C Lund wrote:
Not so. Listen more attentively to what Michael is trying to tell you.> 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
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



Reply With Quote

