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 *firs开发者_JAVA百科tName1 = (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;
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.
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.
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];
NSString itself has functions to get bool and float values.
See reference.
To get date, u need to look at NSDateFormatter.
精彩评论