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.
I've searched through lots of docs without luck. There is probably a
simple way to do this, but damn if I can find it!
_______________________________________________
cocoa-dev mailing list | <cocoa-dev...>
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored. -
Hmm, kooky.
Normally I just say:
if ([mystring length])
{
// so, if the length is not zero...
}
On occasions if I've already been working with a string that might be
null, I may have to use something like:
if (![[myString description] isNotEqualToString:@"(null)"])
Have a little experiment with these and if you're still getting an
error you don't understand, post the error message that Xcode is
giving you.
Cheers,
john
On Thu, 24 Jun 2004 16:22:49 -0500, John Spicer <johnspicer...> wrote:
>_______________________________________________
> 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.
>
> I've searched through lots of docs without luck. There is probably a
> simple way to do this, but damn if I can find it!
> _______________________________________________
> cocoa-dev mailing list | <cocoa-dev...>
> 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 | <cocoa-dev...>
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored. -
On Jun 24, 2004, at 4:22 PM, John Spicer wrote:
> I know it's a stupid question, and I'm embarased to ask, but I've beenYou mentioned using web services and CFNull, which means you're
> 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.
>
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.
So (again, assuming this is the case) if you want to use it as an
NSString, try: NSString newString = [NSString
stringWithString:mystring];
Then use [newString length].
Or, use the Core Foundation functions for CFString instead.
CFStringGetLength(mystring);
hth
- mark
_______________________________________________
cocoa-dev mailing list | <cocoa-dev...>
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored. -
On 25/6/04 7:51 am, Mark A. Stratman <mark...> wrote:
> 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.
No, it works both ways:
<URL:http://developer.apple.com/documentation/CoreFoundation/Reference/CFStr
ingRef/index.html>
---
CFString is 3toll-free bridged2 with its Cocoa Foundation counterpart,
NSString. This means that the Core Foundation type is interchangeable in
function or method calls with the bridged Foundation object. Therefore, in a
method where you see an NSString * parameter, you can pass in a CFStringRef,
and in a function where you see a CFStringRef parameter, you can pass in an
NSString instance. This fact also applies to concrete subclasses of
NSString. See Integrating Carbon and Cocoa in Your Application for more
information on toll-free bridging.
---
Cheers,
Chris
_______________________________________________
cocoa-dev mailing list | <cocoa-dev...>
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored. -
Thanks. :) That's good to know.
And sorry to the original poster for the mis-information (albeit marked
as possibly such ;).
So since that's definitely not the problem then, i think we'd need to
see specific error messages for certain then.
- mark
On Jun 25, 2004, at 1:57 AM, Chris Ridd wrote:
> On 25/6/04 7:51 am, Mark A. Stratman <mark...> wrote:_______________________________________________
>
>> 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.
>
> No, it works both ways:
>
> <URL:http://developer.apple.com/documentation/CoreFoundation/
> Reference/CFStr
> ingRef/index.html>
>
> ---
> CFString is 3toll-free bridged2 with its Cocoa Foundation counterpart,
> NSString. This means that the Core Foundation type is interchangeable
> in
> function or method calls with the bridged Foundation object.
> Therefore, in a
> method where you see an NSString * parameter, you can pass in a
> CFStringRef,
> and in a function where you see a CFStringRef parameter, you can pass
> in an
> NSString instance. This fact also applies to concrete subclasses of
> NSString. See Integrating Carbon and Cocoa in Your Application for more
> information on toll-free bridging.
> ---
>
> Cheers,
>
> Chris
> _______________________________________________
> cocoa-dev mailing list | <cocoa-dev...>
> 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 | <cocoa-dev...>
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored. -
Hello...
There is a single unique instance of CFNull, so you can use pointer equality...
http://developer.apple.com/documentation/CoreFoundation/Reference/CFNullRef
/index.html
if (someString == kCFNull) {return @"";}
else {return someString;}
Hope that helps,
Louis
> 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.
cocoa-dev mailing list | <cocoa-dev...>
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored. -
Am 25.06.2004 um 11:21 schrieb Louis C. Sacha:
> There is a single unique instance of CFNull,
... which is identical to [NSNull null] fortunately, ...
> so you can use pointer equality...
> if (someString == kCFNull) {return @"";}
or
if (someString == [NSNull null])
Andreas
_______________________________________________
cocoa-dev mailing list | <cocoa-dev...>
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored. -
>> 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 | <cocoa-dev...>
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored. -
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 <ignem...>_______________________________________________
> Date: July 5, 2004 1:54:17 AM PDT
> To: <cocoa-dev...>
> 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 | <cocoa-dev...>
> 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 | <cocoa-dev...>
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


