James Ayech
James Ayech

Reputation: 31

conversion Nsstring

Hi all guys ,I am new in objective c and I need help, I read from my xml file and I want convert my NSString to bool and NSString to date and Nsstring to long

NSArray *names = [partyMember elementsForName:@"price"];
        if (names.count > 0) {
            GDataXMLElement *firstName = (GDataXMLElement *) [names objectAtIndex:0];
            price = firstName.stringValue;
        } else continue;

        NSArray *names1 = [partyMember elementsForName:@"date"];
        if (names1.count > 0) {
            GDataXMLElement *firstName1 = (GDataXMLElement *) [names1 objectAtIndex:0];
            date = firstName1.stringValue;

    NSArray *names1 = [partyMember elementsForName:@"test"];
        if (names1.count > 0) {
            GDataXMLElement *firstName1 = (GDataXMLElement *) [names1 objectAtIndex:0];
            test = firstName1.stringValue;

Upvotes: 2

Views: 676

Answers (4)

Ben Baron
Ben Baron

Reputation: 14815

In the future, please first look at Apple's documentation. It is very thorough. In the page on NSString, you can see there is a boolValue method and a longLongValue method. You can read the specifics in the documentation, but those will handle your bool and long cases.

As for converting a date, there are many stackoverflow questions on that topic, this one here should answer your question.

I'm usually not one to say RTFM, but in this case the information was very easily found with a couple quick searches.

Upvotes: 2

Ilanchezhian
Ilanchezhian

Reputation: 17478

NSString itself has functions to get bool and float values.

See reference.

To get date, u need to look at NSDateFormatter.

Upvotes: 0

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31722

To convert NSString to BOOL use below method.

BOOL boolValue = [myString boolValue];

For converting to long use longLongValue: method of NSString.

For NSString to NSDate , use below as reference code.

NSString *dateString = @"2011-07-13";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];

Upvotes: 0

Warren Burton
Warren Burton

Reputation: 17372

For the BOOL A string is NO if its either nil or length 0. Otherwise its YES.

NSDateFormatter's dateFromString will convert strings to dates. You set it up with a c style formatter.

For the long use longValue as in long long myval = [mystring longLongValue];

NSString has several converters – doubleValue – floatValue – intValue – integerValue – longLongValue – boolValue

Use as required.

Upvotes: 3

Related Questions