2

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;
Baby Groot
  • 4,637
  • 39
  • 52
  • 71

4 Answers4

3

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.

Warren Burton
  • 17,451
  • 3
  • 53
  • 73
2

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.

Community
  • 1
  • 1
Ben Baron
  • 14,496
  • 12
  • 55
  • 65
  • I too was tempted to say RTFM. Maybe more a discussion for meta.stackoverflow but I wonder if it would be possible to get a review/warning for questions which come up with a certain weight of similars – Warren Burton Jul 13 '11 at 13:43
0

NSString itself has functions to get bool and float values.

See reference.

To get date, u need to look at NSDateFormatter.

Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55
0

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];
Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76