Re: figuring out the length of a string
Re: figuring out the length of a string
- Subject: Re: figuring out the length of a string
- From: Mike Ferris <email@hidden>
- Date: Mon, 5 Jul 2004 09:04:36 -0700
A couple things.
First, CFNull is not a string and it does not have a "length". To test
something against CFNull just compare (==) it to kCFNull (or
equivalently [NSNull null]). There is only one instance of the "null"
object, so pointer comparison is what you want.
Second... Warning! Unicode nitpicking ahead! You should never use
([someStr length] == 0) to test for an "empty string". Always use
something like [someStr isEqualToString:@""]. Unicode allows for
various characters that do not really "count" as semantic string
content, and there are definitely strings that have length > 0 which
are still equivalent to the empty string. As was noted in the original
post, sometimes Apple's examples do not quite do the right thing
here... But any of the canonical Cocoa text system examples (ie those
written by the text guys) should get this right.
Finally, NSStrings can be cross-cast to and from CFString (along with
various other CF/Foundation types including CF/NSData CF/NSArray,
CF/NSDictionary, and, of course, CF/NSNull).
Mike
Begin forwarded message:
>
From: H}kan Johansson <email@hidden>
>
Date: July 5, 2004 1:54:17 AM PDT
>
To: email@hidden
>
Subject: Re: figuring out the length of a string
>
>
>> I know it's a stupid question, and I'm embarased to ask, but I've
>
>> been
>
>> going through the docs for an hour and can't find it.
>
>>
>
>> I'm loading some data from a database via web services, some of the
>
>> fields are nulls. The string is set to a CFNull in that case.
>
>>
>
>> I'd like to filter on this somehow and return a blank string instead.
>
>>
>
>> I have found an example on apple's site that using the [mystring
>
>> length] == 0 to test for this, but xcode does not like this.
>
>>
>
> You mentioned using web services and CFNull, which means you're
>
> probably working with CFString's?
>
> If so, you won't (as far as I know, somebody correct me if I'm wrong -
>
> too lazy to test atm) be able to treat it as an NSString ("[mystring
>
> length]" is objective C, calling the length method on an NSString).
>
> The toll-free bindings work as far as treating an NSString as a
>
> CFString, but I don't think it works the other way around.
>
>
>
>
You can use it both ways, but you have to cast it.
>
int len = [(NSString *)aCFString length];
>
>
The compiler cannot see that CFString and NSString is essentially the
>
same.
>
>
--
>
The universe hates you. Deal with it.
>
/Hakan Johansson
>
_______________________________________________
>
cocoa-dev mailing list | email@hidden
>
Help/Unsubscribe/Archives:
>
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.